PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/usr/src/uts/common/fs/zfs/zfs_vfsops.c

https://bitbucket.org/0xffea/illumos-dccp
C | 2317 lines | 1513 code | 329 blank | 475 comment | 398 complexity | 98755ed360f2f2c3ab0cc974ddf7e243 MD5 | raw file
Possible License(s): LGPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, MPL-2.0-no-copyleft-exception, AGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.1, LGPL-3.0, AGPL-1.0, GPL-3.0, 0BSD, BSD-2-Clause
  1. /*
  2. * CDDL HEADER START
  3. *
  4. * The contents of this file are subject to the terms of the
  5. * Common Development and Distribution License (the "License").
  6. * You may not use this file except in compliance with the License.
  7. *
  8. * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  9. * or http://www.opensolaris.org/os/licensing.
  10. * See the License for the specific language governing permissions
  11. * and limitations under the License.
  12. *
  13. * When distributing Covered Code, include this CDDL HEADER in each
  14. * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15. * If applicable, add the following below this CDDL HEADER, with the
  16. * fields enclosed by brackets "[]" replaced with your own identifying
  17. * information: Portions Copyright [yyyy] [name of copyright owner]
  18. *
  19. * CDDL HEADER END
  20. */
  21. /*
  22. * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23. * Copyright (c) 2012 by Delphix. All rights reserved.
  24. */
  25. /* Portions Copyright 2010 Robert Milkowski */
  26. #include <sys/types.h>
  27. #include <sys/param.h>
  28. #include <sys/systm.h>
  29. #include <sys/sysmacros.h>
  30. #include <sys/kmem.h>
  31. #include <sys/pathname.h>
  32. #include <sys/vnode.h>
  33. #include <sys/vfs.h>
  34. #include <sys/vfs_opreg.h>
  35. #include <sys/mntent.h>
  36. #include <sys/mount.h>
  37. #include <sys/cmn_err.h>
  38. #include "fs/fs_subr.h"
  39. #include <sys/zfs_znode.h>
  40. #include <sys/zfs_dir.h>
  41. #include <sys/zil.h>
  42. #include <sys/fs/zfs.h>
  43. #include <sys/dmu.h>
  44. #include <sys/dsl_prop.h>
  45. #include <sys/dsl_dataset.h>
  46. #include <sys/dsl_deleg.h>
  47. #include <sys/spa.h>
  48. #include <sys/zap.h>
  49. #include <sys/sa.h>
  50. #include <sys/varargs.h>
  51. #include <sys/policy.h>
  52. #include <sys/atomic.h>
  53. #include <sys/mkdev.h>
  54. #include <sys/modctl.h>
  55. #include <sys/refstr.h>
  56. #include <sys/zfs_ioctl.h>
  57. #include <sys/zfs_ctldir.h>
  58. #include <sys/zfs_fuid.h>
  59. #include <sys/bootconf.h>
  60. #include <sys/sunddi.h>
  61. #include <sys/dnlc.h>
  62. #include <sys/dmu_objset.h>
  63. #include <sys/spa_boot.h>
  64. #include <sys/sa.h>
  65. #include "zfs_comutil.h"
  66. int zfsfstype;
  67. vfsops_t *zfs_vfsops = NULL;
  68. static major_t zfs_major;
  69. static minor_t zfs_minor;
  70. static kmutex_t zfs_dev_mtx;
  71. extern int sys_shutdown;
  72. static int zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr);
  73. static int zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr);
  74. static int zfs_mountroot(vfs_t *vfsp, enum whymountroot);
  75. static int zfs_root(vfs_t *vfsp, vnode_t **vpp);
  76. static int zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp);
  77. static int zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp);
  78. static void zfs_freevfs(vfs_t *vfsp);
  79. static const fs_operation_def_t zfs_vfsops_template[] = {
  80. VFSNAME_MOUNT, { .vfs_mount = zfs_mount },
  81. VFSNAME_MOUNTROOT, { .vfs_mountroot = zfs_mountroot },
  82. VFSNAME_UNMOUNT, { .vfs_unmount = zfs_umount },
  83. VFSNAME_ROOT, { .vfs_root = zfs_root },
  84. VFSNAME_STATVFS, { .vfs_statvfs = zfs_statvfs },
  85. VFSNAME_SYNC, { .vfs_sync = zfs_sync },
  86. VFSNAME_VGET, { .vfs_vget = zfs_vget },
  87. VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs },
  88. NULL, NULL
  89. };
  90. static const fs_operation_def_t zfs_vfsops_eio_template[] = {
  91. VFSNAME_FREEVFS, { .vfs_freevfs = zfs_freevfs },
  92. NULL, NULL
  93. };
  94. /*
  95. * We need to keep a count of active fs's.
  96. * This is necessary to prevent our module
  97. * from being unloaded after a umount -f
  98. */
  99. static uint32_t zfs_active_fs_count = 0;
  100. static char *noatime_cancel[] = { MNTOPT_ATIME, NULL };
  101. static char *atime_cancel[] = { MNTOPT_NOATIME, NULL };
  102. static char *noxattr_cancel[] = { MNTOPT_XATTR, NULL };
  103. static char *xattr_cancel[] = { MNTOPT_NOXATTR, NULL };
  104. /*
  105. * MO_DEFAULT is not used since the default value is determined
  106. * by the equivalent property.
  107. */
  108. static mntopt_t mntopts[] = {
  109. { MNTOPT_NOXATTR, noxattr_cancel, NULL, 0, NULL },
  110. { MNTOPT_XATTR, xattr_cancel, NULL, 0, NULL },
  111. { MNTOPT_NOATIME, noatime_cancel, NULL, 0, NULL },
  112. { MNTOPT_ATIME, atime_cancel, NULL, 0, NULL }
  113. };
  114. static mntopts_t zfs_mntopts = {
  115. sizeof (mntopts) / sizeof (mntopt_t),
  116. mntopts
  117. };
  118. /*ARGSUSED*/
  119. int
  120. zfs_sync(vfs_t *vfsp, short flag, cred_t *cr)
  121. {
  122. /*
  123. * Data integrity is job one. We don't want a compromised kernel
  124. * writing to the storage pool, so we never sync during panic.
  125. */
  126. if (panicstr)
  127. return (0);
  128. /*
  129. * SYNC_ATTR is used by fsflush() to force old filesystems like UFS
  130. * to sync metadata, which they would otherwise cache indefinitely.
  131. * Semantically, the only requirement is that the sync be initiated.
  132. * The DMU syncs out txgs frequently, so there's nothing to do.
  133. */
  134. if (flag & SYNC_ATTR)
  135. return (0);
  136. if (vfsp != NULL) {
  137. /*
  138. * Sync a specific filesystem.
  139. */
  140. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  141. dsl_pool_t *dp;
  142. ZFS_ENTER(zfsvfs);
  143. dp = dmu_objset_pool(zfsvfs->z_os);
  144. /*
  145. * If the system is shutting down, then skip any
  146. * filesystems which may exist on a suspended pool.
  147. */
  148. if (sys_shutdown && spa_suspended(dp->dp_spa)) {
  149. ZFS_EXIT(zfsvfs);
  150. return (0);
  151. }
  152. if (zfsvfs->z_log != NULL)
  153. zil_commit(zfsvfs->z_log, 0);
  154. ZFS_EXIT(zfsvfs);
  155. } else {
  156. /*
  157. * Sync all ZFS filesystems. This is what happens when you
  158. * run sync(1M). Unlike other filesystems, ZFS honors the
  159. * request by waiting for all pools to commit all dirty data.
  160. */
  161. spa_sync_allpools();
  162. }
  163. return (0);
  164. }
  165. static int
  166. zfs_create_unique_device(dev_t *dev)
  167. {
  168. major_t new_major;
  169. do {
  170. ASSERT3U(zfs_minor, <=, MAXMIN32);
  171. minor_t start = zfs_minor;
  172. do {
  173. mutex_enter(&zfs_dev_mtx);
  174. if (zfs_minor >= MAXMIN32) {
  175. /*
  176. * If we're still using the real major
  177. * keep out of /dev/zfs and /dev/zvol minor
  178. * number space. If we're using a getudev()'ed
  179. * major number, we can use all of its minors.
  180. */
  181. if (zfs_major == ddi_name_to_major(ZFS_DRIVER))
  182. zfs_minor = ZFS_MIN_MINOR;
  183. else
  184. zfs_minor = 0;
  185. } else {
  186. zfs_minor++;
  187. }
  188. *dev = makedevice(zfs_major, zfs_minor);
  189. mutex_exit(&zfs_dev_mtx);
  190. } while (vfs_devismounted(*dev) && zfs_minor != start);
  191. if (zfs_minor == start) {
  192. /*
  193. * We are using all ~262,000 minor numbers for the
  194. * current major number. Create a new major number.
  195. */
  196. if ((new_major = getudev()) == (major_t)-1) {
  197. cmn_err(CE_WARN,
  198. "zfs_mount: Can't get unique major "
  199. "device number.");
  200. return (-1);
  201. }
  202. mutex_enter(&zfs_dev_mtx);
  203. zfs_major = new_major;
  204. zfs_minor = 0;
  205. mutex_exit(&zfs_dev_mtx);
  206. } else {
  207. break;
  208. }
  209. /* CONSTANTCONDITION */
  210. } while (1);
  211. return (0);
  212. }
  213. static void
  214. atime_changed_cb(void *arg, uint64_t newval)
  215. {
  216. zfsvfs_t *zfsvfs = arg;
  217. if (newval == TRUE) {
  218. zfsvfs->z_atime = TRUE;
  219. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME);
  220. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_ATIME, NULL, 0);
  221. } else {
  222. zfsvfs->z_atime = FALSE;
  223. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_ATIME);
  224. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOATIME, NULL, 0);
  225. }
  226. }
  227. static void
  228. xattr_changed_cb(void *arg, uint64_t newval)
  229. {
  230. zfsvfs_t *zfsvfs = arg;
  231. if (newval == TRUE) {
  232. /* XXX locking on vfs_flag? */
  233. zfsvfs->z_vfs->vfs_flag |= VFS_XATTR;
  234. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR);
  235. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_XATTR, NULL, 0);
  236. } else {
  237. /* XXX locking on vfs_flag? */
  238. zfsvfs->z_vfs->vfs_flag &= ~VFS_XATTR;
  239. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_XATTR);
  240. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOXATTR, NULL, 0);
  241. }
  242. }
  243. static void
  244. blksz_changed_cb(void *arg, uint64_t newval)
  245. {
  246. zfsvfs_t *zfsvfs = arg;
  247. if (newval < SPA_MINBLOCKSIZE ||
  248. newval > SPA_MAXBLOCKSIZE || !ISP2(newval))
  249. newval = SPA_MAXBLOCKSIZE;
  250. zfsvfs->z_max_blksz = newval;
  251. zfsvfs->z_vfs->vfs_bsize = newval;
  252. }
  253. static void
  254. readonly_changed_cb(void *arg, uint64_t newval)
  255. {
  256. zfsvfs_t *zfsvfs = arg;
  257. if (newval) {
  258. /* XXX locking on vfs_flag? */
  259. zfsvfs->z_vfs->vfs_flag |= VFS_RDONLY;
  260. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RW);
  261. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RO, NULL, 0);
  262. } else {
  263. /* XXX locking on vfs_flag? */
  264. zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
  265. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_RO);
  266. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_RW, NULL, 0);
  267. }
  268. }
  269. static void
  270. devices_changed_cb(void *arg, uint64_t newval)
  271. {
  272. zfsvfs_t *zfsvfs = arg;
  273. if (newval == FALSE) {
  274. zfsvfs->z_vfs->vfs_flag |= VFS_NODEVICES;
  275. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES);
  276. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES, NULL, 0);
  277. } else {
  278. zfsvfs->z_vfs->vfs_flag &= ~VFS_NODEVICES;
  279. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NODEVICES);
  280. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_DEVICES, NULL, 0);
  281. }
  282. }
  283. static void
  284. setuid_changed_cb(void *arg, uint64_t newval)
  285. {
  286. zfsvfs_t *zfsvfs = arg;
  287. if (newval == FALSE) {
  288. zfsvfs->z_vfs->vfs_flag |= VFS_NOSETUID;
  289. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_SETUID);
  290. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID, NULL, 0);
  291. } else {
  292. zfsvfs->z_vfs->vfs_flag &= ~VFS_NOSETUID;
  293. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOSETUID);
  294. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_SETUID, NULL, 0);
  295. }
  296. }
  297. static void
  298. exec_changed_cb(void *arg, uint64_t newval)
  299. {
  300. zfsvfs_t *zfsvfs = arg;
  301. if (newval == FALSE) {
  302. zfsvfs->z_vfs->vfs_flag |= VFS_NOEXEC;
  303. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_EXEC);
  304. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC, NULL, 0);
  305. } else {
  306. zfsvfs->z_vfs->vfs_flag &= ~VFS_NOEXEC;
  307. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NOEXEC);
  308. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_EXEC, NULL, 0);
  309. }
  310. }
  311. /*
  312. * The nbmand mount option can be changed at mount time.
  313. * We can't allow it to be toggled on live file systems or incorrect
  314. * behavior may be seen from cifs clients
  315. *
  316. * This property isn't registered via dsl_prop_register(), but this callback
  317. * will be called when a file system is first mounted
  318. */
  319. static void
  320. nbmand_changed_cb(void *arg, uint64_t newval)
  321. {
  322. zfsvfs_t *zfsvfs = arg;
  323. if (newval == FALSE) {
  324. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND);
  325. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND, NULL, 0);
  326. } else {
  327. vfs_clearmntopt(zfsvfs->z_vfs, MNTOPT_NONBMAND);
  328. vfs_setmntopt(zfsvfs->z_vfs, MNTOPT_NBMAND, NULL, 0);
  329. }
  330. }
  331. static void
  332. snapdir_changed_cb(void *arg, uint64_t newval)
  333. {
  334. zfsvfs_t *zfsvfs = arg;
  335. zfsvfs->z_show_ctldir = newval;
  336. }
  337. static void
  338. vscan_changed_cb(void *arg, uint64_t newval)
  339. {
  340. zfsvfs_t *zfsvfs = arg;
  341. zfsvfs->z_vscan = newval;
  342. }
  343. static void
  344. acl_mode_changed_cb(void *arg, uint64_t newval)
  345. {
  346. zfsvfs_t *zfsvfs = arg;
  347. zfsvfs->z_acl_mode = newval;
  348. }
  349. static void
  350. acl_inherit_changed_cb(void *arg, uint64_t newval)
  351. {
  352. zfsvfs_t *zfsvfs = arg;
  353. zfsvfs->z_acl_inherit = newval;
  354. }
  355. static int
  356. zfs_register_callbacks(vfs_t *vfsp)
  357. {
  358. struct dsl_dataset *ds = NULL;
  359. objset_t *os = NULL;
  360. zfsvfs_t *zfsvfs = NULL;
  361. uint64_t nbmand;
  362. int readonly, do_readonly = B_FALSE;
  363. int setuid, do_setuid = B_FALSE;
  364. int exec, do_exec = B_FALSE;
  365. int devices, do_devices = B_FALSE;
  366. int xattr, do_xattr = B_FALSE;
  367. int atime, do_atime = B_FALSE;
  368. int error = 0;
  369. ASSERT(vfsp);
  370. zfsvfs = vfsp->vfs_data;
  371. ASSERT(zfsvfs);
  372. os = zfsvfs->z_os;
  373. /*
  374. * The act of registering our callbacks will destroy any mount
  375. * options we may have. In order to enable temporary overrides
  376. * of mount options, we stash away the current values and
  377. * restore them after we register the callbacks.
  378. */
  379. if (vfs_optionisset(vfsp, MNTOPT_RO, NULL) ||
  380. !spa_writeable(dmu_objset_spa(os))) {
  381. readonly = B_TRUE;
  382. do_readonly = B_TRUE;
  383. } else if (vfs_optionisset(vfsp, MNTOPT_RW, NULL)) {
  384. readonly = B_FALSE;
  385. do_readonly = B_TRUE;
  386. }
  387. if (vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL)) {
  388. devices = B_FALSE;
  389. setuid = B_FALSE;
  390. do_devices = B_TRUE;
  391. do_setuid = B_TRUE;
  392. } else {
  393. if (vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL)) {
  394. devices = B_FALSE;
  395. do_devices = B_TRUE;
  396. } else if (vfs_optionisset(vfsp, MNTOPT_DEVICES, NULL)) {
  397. devices = B_TRUE;
  398. do_devices = B_TRUE;
  399. }
  400. if (vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL)) {
  401. setuid = B_FALSE;
  402. do_setuid = B_TRUE;
  403. } else if (vfs_optionisset(vfsp, MNTOPT_SETUID, NULL)) {
  404. setuid = B_TRUE;
  405. do_setuid = B_TRUE;
  406. }
  407. }
  408. if (vfs_optionisset(vfsp, MNTOPT_NOEXEC, NULL)) {
  409. exec = B_FALSE;
  410. do_exec = B_TRUE;
  411. } else if (vfs_optionisset(vfsp, MNTOPT_EXEC, NULL)) {
  412. exec = B_TRUE;
  413. do_exec = B_TRUE;
  414. }
  415. if (vfs_optionisset(vfsp, MNTOPT_NOXATTR, NULL)) {
  416. xattr = B_FALSE;
  417. do_xattr = B_TRUE;
  418. } else if (vfs_optionisset(vfsp, MNTOPT_XATTR, NULL)) {
  419. xattr = B_TRUE;
  420. do_xattr = B_TRUE;
  421. }
  422. if (vfs_optionisset(vfsp, MNTOPT_NOATIME, NULL)) {
  423. atime = B_FALSE;
  424. do_atime = B_TRUE;
  425. } else if (vfs_optionisset(vfsp, MNTOPT_ATIME, NULL)) {
  426. atime = B_TRUE;
  427. do_atime = B_TRUE;
  428. }
  429. /*
  430. * nbmand is a special property. It can only be changed at
  431. * mount time.
  432. *
  433. * This is weird, but it is documented to only be changeable
  434. * at mount time.
  435. */
  436. if (vfs_optionisset(vfsp, MNTOPT_NONBMAND, NULL)) {
  437. nbmand = B_FALSE;
  438. } else if (vfs_optionisset(vfsp, MNTOPT_NBMAND, NULL)) {
  439. nbmand = B_TRUE;
  440. } else {
  441. char osname[MAXNAMELEN];
  442. dmu_objset_name(os, osname);
  443. if (error = dsl_prop_get_integer(osname, "nbmand", &nbmand,
  444. NULL)) {
  445. return (error);
  446. }
  447. }
  448. /*
  449. * Register property callbacks.
  450. *
  451. * It would probably be fine to just check for i/o error from
  452. * the first prop_register(), but I guess I like to go
  453. * overboard...
  454. */
  455. ds = dmu_objset_ds(os);
  456. error = dsl_prop_register(ds, "atime", atime_changed_cb, zfsvfs);
  457. error = error ? error : dsl_prop_register(ds,
  458. "xattr", xattr_changed_cb, zfsvfs);
  459. error = error ? error : dsl_prop_register(ds,
  460. "recordsize", blksz_changed_cb, zfsvfs);
  461. error = error ? error : dsl_prop_register(ds,
  462. "readonly", readonly_changed_cb, zfsvfs);
  463. error = error ? error : dsl_prop_register(ds,
  464. "devices", devices_changed_cb, zfsvfs);
  465. error = error ? error : dsl_prop_register(ds,
  466. "setuid", setuid_changed_cb, zfsvfs);
  467. error = error ? error : dsl_prop_register(ds,
  468. "exec", exec_changed_cb, zfsvfs);
  469. error = error ? error : dsl_prop_register(ds,
  470. "snapdir", snapdir_changed_cb, zfsvfs);
  471. error = error ? error : dsl_prop_register(ds,
  472. "aclmode", acl_mode_changed_cb, zfsvfs);
  473. error = error ? error : dsl_prop_register(ds,
  474. "aclinherit", acl_inherit_changed_cb, zfsvfs);
  475. error = error ? error : dsl_prop_register(ds,
  476. "vscan", vscan_changed_cb, zfsvfs);
  477. if (error)
  478. goto unregister;
  479. /*
  480. * Invoke our callbacks to restore temporary mount options.
  481. */
  482. if (do_readonly)
  483. readonly_changed_cb(zfsvfs, readonly);
  484. if (do_setuid)
  485. setuid_changed_cb(zfsvfs, setuid);
  486. if (do_exec)
  487. exec_changed_cb(zfsvfs, exec);
  488. if (do_devices)
  489. devices_changed_cb(zfsvfs, devices);
  490. if (do_xattr)
  491. xattr_changed_cb(zfsvfs, xattr);
  492. if (do_atime)
  493. atime_changed_cb(zfsvfs, atime);
  494. nbmand_changed_cb(zfsvfs, nbmand);
  495. return (0);
  496. unregister:
  497. /*
  498. * We may attempt to unregister some callbacks that are not
  499. * registered, but this is OK; it will simply return ENOMSG,
  500. * which we will ignore.
  501. */
  502. (void) dsl_prop_unregister(ds, "atime", atime_changed_cb, zfsvfs);
  503. (void) dsl_prop_unregister(ds, "xattr", xattr_changed_cb, zfsvfs);
  504. (void) dsl_prop_unregister(ds, "recordsize", blksz_changed_cb, zfsvfs);
  505. (void) dsl_prop_unregister(ds, "readonly", readonly_changed_cb, zfsvfs);
  506. (void) dsl_prop_unregister(ds, "devices", devices_changed_cb, zfsvfs);
  507. (void) dsl_prop_unregister(ds, "setuid", setuid_changed_cb, zfsvfs);
  508. (void) dsl_prop_unregister(ds, "exec", exec_changed_cb, zfsvfs);
  509. (void) dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb, zfsvfs);
  510. (void) dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb, zfsvfs);
  511. (void) dsl_prop_unregister(ds, "aclinherit", acl_inherit_changed_cb,
  512. zfsvfs);
  513. (void) dsl_prop_unregister(ds, "vscan", vscan_changed_cb, zfsvfs);
  514. return (error);
  515. }
  516. static int
  517. zfs_space_delta_cb(dmu_object_type_t bonustype, void *data,
  518. uint64_t *userp, uint64_t *groupp)
  519. {
  520. znode_phys_t *znp = data;
  521. int error = 0;
  522. /*
  523. * Is it a valid type of object to track?
  524. */
  525. if (bonustype != DMU_OT_ZNODE && bonustype != DMU_OT_SA)
  526. return (ENOENT);
  527. /*
  528. * If we have a NULL data pointer
  529. * then assume the id's aren't changing and
  530. * return EEXIST to the dmu to let it know to
  531. * use the same ids
  532. */
  533. if (data == NULL)
  534. return (EEXIST);
  535. if (bonustype == DMU_OT_ZNODE) {
  536. *userp = znp->zp_uid;
  537. *groupp = znp->zp_gid;
  538. } else {
  539. int hdrsize;
  540. ASSERT(bonustype == DMU_OT_SA);
  541. hdrsize = sa_hdrsize(data);
  542. if (hdrsize != 0) {
  543. *userp = *((uint64_t *)((uintptr_t)data + hdrsize +
  544. SA_UID_OFFSET));
  545. *groupp = *((uint64_t *)((uintptr_t)data + hdrsize +
  546. SA_GID_OFFSET));
  547. } else {
  548. /*
  549. * This should only happen for newly created
  550. * files that haven't had the znode data filled
  551. * in yet.
  552. */
  553. *userp = 0;
  554. *groupp = 0;
  555. }
  556. }
  557. return (error);
  558. }
  559. static void
  560. fuidstr_to_sid(zfsvfs_t *zfsvfs, const char *fuidstr,
  561. char *domainbuf, int buflen, uid_t *ridp)
  562. {
  563. uint64_t fuid;
  564. const char *domain;
  565. fuid = strtonum(fuidstr, NULL);
  566. domain = zfs_fuid_find_by_idx(zfsvfs, FUID_INDEX(fuid));
  567. if (domain)
  568. (void) strlcpy(domainbuf, domain, buflen);
  569. else
  570. domainbuf[0] = '\0';
  571. *ridp = FUID_RID(fuid);
  572. }
  573. static uint64_t
  574. zfs_userquota_prop_to_obj(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type)
  575. {
  576. switch (type) {
  577. case ZFS_PROP_USERUSED:
  578. return (DMU_USERUSED_OBJECT);
  579. case ZFS_PROP_GROUPUSED:
  580. return (DMU_GROUPUSED_OBJECT);
  581. case ZFS_PROP_USERQUOTA:
  582. return (zfsvfs->z_userquota_obj);
  583. case ZFS_PROP_GROUPQUOTA:
  584. return (zfsvfs->z_groupquota_obj);
  585. }
  586. return (0);
  587. }
  588. int
  589. zfs_userspace_many(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
  590. uint64_t *cookiep, void *vbuf, uint64_t *bufsizep)
  591. {
  592. int error;
  593. zap_cursor_t zc;
  594. zap_attribute_t za;
  595. zfs_useracct_t *buf = vbuf;
  596. uint64_t obj;
  597. if (!dmu_objset_userspace_present(zfsvfs->z_os))
  598. return (ENOTSUP);
  599. obj = zfs_userquota_prop_to_obj(zfsvfs, type);
  600. if (obj == 0) {
  601. *bufsizep = 0;
  602. return (0);
  603. }
  604. for (zap_cursor_init_serialized(&zc, zfsvfs->z_os, obj, *cookiep);
  605. (error = zap_cursor_retrieve(&zc, &za)) == 0;
  606. zap_cursor_advance(&zc)) {
  607. if ((uintptr_t)buf - (uintptr_t)vbuf + sizeof (zfs_useracct_t) >
  608. *bufsizep)
  609. break;
  610. fuidstr_to_sid(zfsvfs, za.za_name,
  611. buf->zu_domain, sizeof (buf->zu_domain), &buf->zu_rid);
  612. buf->zu_space = za.za_first_integer;
  613. buf++;
  614. }
  615. if (error == ENOENT)
  616. error = 0;
  617. ASSERT3U((uintptr_t)buf - (uintptr_t)vbuf, <=, *bufsizep);
  618. *bufsizep = (uintptr_t)buf - (uintptr_t)vbuf;
  619. *cookiep = zap_cursor_serialize(&zc);
  620. zap_cursor_fini(&zc);
  621. return (error);
  622. }
  623. /*
  624. * buf must be big enough (eg, 32 bytes)
  625. */
  626. static int
  627. id_to_fuidstr(zfsvfs_t *zfsvfs, const char *domain, uid_t rid,
  628. char *buf, boolean_t addok)
  629. {
  630. uint64_t fuid;
  631. int domainid = 0;
  632. if (domain && domain[0]) {
  633. domainid = zfs_fuid_find_by_domain(zfsvfs, domain, NULL, addok);
  634. if (domainid == -1)
  635. return (ENOENT);
  636. }
  637. fuid = FUID_ENCODE(domainid, rid);
  638. (void) sprintf(buf, "%llx", (longlong_t)fuid);
  639. return (0);
  640. }
  641. int
  642. zfs_userspace_one(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
  643. const char *domain, uint64_t rid, uint64_t *valp)
  644. {
  645. char buf[32];
  646. int err;
  647. uint64_t obj;
  648. *valp = 0;
  649. if (!dmu_objset_userspace_present(zfsvfs->z_os))
  650. return (ENOTSUP);
  651. obj = zfs_userquota_prop_to_obj(zfsvfs, type);
  652. if (obj == 0)
  653. return (0);
  654. err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_FALSE);
  655. if (err)
  656. return (err);
  657. err = zap_lookup(zfsvfs->z_os, obj, buf, 8, 1, valp);
  658. if (err == ENOENT)
  659. err = 0;
  660. return (err);
  661. }
  662. int
  663. zfs_set_userquota(zfsvfs_t *zfsvfs, zfs_userquota_prop_t type,
  664. const char *domain, uint64_t rid, uint64_t quota)
  665. {
  666. char buf[32];
  667. int err;
  668. dmu_tx_t *tx;
  669. uint64_t *objp;
  670. boolean_t fuid_dirtied;
  671. if (type != ZFS_PROP_USERQUOTA && type != ZFS_PROP_GROUPQUOTA)
  672. return (EINVAL);
  673. if (zfsvfs->z_version < ZPL_VERSION_USERSPACE)
  674. return (ENOTSUP);
  675. objp = (type == ZFS_PROP_USERQUOTA) ? &zfsvfs->z_userquota_obj :
  676. &zfsvfs->z_groupquota_obj;
  677. err = id_to_fuidstr(zfsvfs, domain, rid, buf, B_TRUE);
  678. if (err)
  679. return (err);
  680. fuid_dirtied = zfsvfs->z_fuid_dirty;
  681. tx = dmu_tx_create(zfsvfs->z_os);
  682. dmu_tx_hold_zap(tx, *objp ? *objp : DMU_NEW_OBJECT, B_TRUE, NULL);
  683. if (*objp == 0) {
  684. dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
  685. zfs_userquota_prop_prefixes[type]);
  686. }
  687. if (fuid_dirtied)
  688. zfs_fuid_txhold(zfsvfs, tx);
  689. err = dmu_tx_assign(tx, TXG_WAIT);
  690. if (err) {
  691. dmu_tx_abort(tx);
  692. return (err);
  693. }
  694. mutex_enter(&zfsvfs->z_lock);
  695. if (*objp == 0) {
  696. *objp = zap_create(zfsvfs->z_os, DMU_OT_USERGROUP_QUOTA,
  697. DMU_OT_NONE, 0, tx);
  698. VERIFY(0 == zap_add(zfsvfs->z_os, MASTER_NODE_OBJ,
  699. zfs_userquota_prop_prefixes[type], 8, 1, objp, tx));
  700. }
  701. mutex_exit(&zfsvfs->z_lock);
  702. if (quota == 0) {
  703. err = zap_remove(zfsvfs->z_os, *objp, buf, tx);
  704. if (err == ENOENT)
  705. err = 0;
  706. } else {
  707. err = zap_update(zfsvfs->z_os, *objp, buf, 8, 1, &quota, tx);
  708. }
  709. ASSERT(err == 0);
  710. if (fuid_dirtied)
  711. zfs_fuid_sync(zfsvfs, tx);
  712. dmu_tx_commit(tx);
  713. return (err);
  714. }
  715. boolean_t
  716. zfs_fuid_overquota(zfsvfs_t *zfsvfs, boolean_t isgroup, uint64_t fuid)
  717. {
  718. char buf[32];
  719. uint64_t used, quota, usedobj, quotaobj;
  720. int err;
  721. usedobj = isgroup ? DMU_GROUPUSED_OBJECT : DMU_USERUSED_OBJECT;
  722. quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
  723. if (quotaobj == 0 || zfsvfs->z_replay)
  724. return (B_FALSE);
  725. (void) sprintf(buf, "%llx", (longlong_t)fuid);
  726. err = zap_lookup(zfsvfs->z_os, quotaobj, buf, 8, 1, &quota);
  727. if (err != 0)
  728. return (B_FALSE);
  729. err = zap_lookup(zfsvfs->z_os, usedobj, buf, 8, 1, &used);
  730. if (err != 0)
  731. return (B_FALSE);
  732. return (used >= quota);
  733. }
  734. boolean_t
  735. zfs_owner_overquota(zfsvfs_t *zfsvfs, znode_t *zp, boolean_t isgroup)
  736. {
  737. uint64_t fuid;
  738. uint64_t quotaobj;
  739. quotaobj = isgroup ? zfsvfs->z_groupquota_obj : zfsvfs->z_userquota_obj;
  740. fuid = isgroup ? zp->z_gid : zp->z_uid;
  741. if (quotaobj == 0 || zfsvfs->z_replay)
  742. return (B_FALSE);
  743. return (zfs_fuid_overquota(zfsvfs, isgroup, fuid));
  744. }
  745. int
  746. zfsvfs_create(const char *osname, zfsvfs_t **zfvp)
  747. {
  748. objset_t *os;
  749. zfsvfs_t *zfsvfs;
  750. uint64_t zval;
  751. int i, error;
  752. uint64_t sa_obj;
  753. zfsvfs = kmem_zalloc(sizeof (zfsvfs_t), KM_SLEEP);
  754. /*
  755. * We claim to always be readonly so we can open snapshots;
  756. * other ZPL code will prevent us from writing to snapshots.
  757. */
  758. error = dmu_objset_own(osname, DMU_OST_ZFS, B_TRUE, zfsvfs, &os);
  759. if (error) {
  760. kmem_free(zfsvfs, sizeof (zfsvfs_t));
  761. return (error);
  762. }
  763. /*
  764. * Initialize the zfs-specific filesystem structure.
  765. * Should probably make this a kmem cache, shuffle fields,
  766. * and just bzero up to z_hold_mtx[].
  767. */
  768. zfsvfs->z_vfs = NULL;
  769. zfsvfs->z_parent = zfsvfs;
  770. zfsvfs->z_max_blksz = SPA_MAXBLOCKSIZE;
  771. zfsvfs->z_show_ctldir = ZFS_SNAPDIR_VISIBLE;
  772. zfsvfs->z_os = os;
  773. error = zfs_get_zplprop(os, ZFS_PROP_VERSION, &zfsvfs->z_version);
  774. if (error) {
  775. goto out;
  776. } else if (zfsvfs->z_version >
  777. zfs_zpl_version_map(spa_version(dmu_objset_spa(os)))) {
  778. (void) printf("Can't mount a version %lld file system "
  779. "on a version %lld pool\n. Pool must be upgraded to mount "
  780. "this file system.", (u_longlong_t)zfsvfs->z_version,
  781. (u_longlong_t)spa_version(dmu_objset_spa(os)));
  782. error = ENOTSUP;
  783. goto out;
  784. }
  785. if ((error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &zval)) != 0)
  786. goto out;
  787. zfsvfs->z_norm = (int)zval;
  788. if ((error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &zval)) != 0)
  789. goto out;
  790. zfsvfs->z_utf8 = (zval != 0);
  791. if ((error = zfs_get_zplprop(os, ZFS_PROP_CASE, &zval)) != 0)
  792. goto out;
  793. zfsvfs->z_case = (uint_t)zval;
  794. /*
  795. * Fold case on file systems that are always or sometimes case
  796. * insensitive.
  797. */
  798. if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
  799. zfsvfs->z_case == ZFS_CASE_MIXED)
  800. zfsvfs->z_norm |= U8_TEXTPREP_TOUPPER;
  801. zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
  802. zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
  803. if (zfsvfs->z_use_sa) {
  804. /* should either have both of these objects or none */
  805. error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SA_ATTRS, 8, 1,
  806. &sa_obj);
  807. if (error)
  808. return (error);
  809. } else {
  810. /*
  811. * Pre SA versions file systems should never touch
  812. * either the attribute registration or layout objects.
  813. */
  814. sa_obj = 0;
  815. }
  816. error = sa_setup(os, sa_obj, zfs_attr_table, ZPL_END,
  817. &zfsvfs->z_attr_table);
  818. if (error)
  819. goto out;
  820. if (zfsvfs->z_version >= ZPL_VERSION_SA)
  821. sa_register_update_callback(os, zfs_sa_upgrade);
  822. error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_ROOT_OBJ, 8, 1,
  823. &zfsvfs->z_root);
  824. if (error)
  825. goto out;
  826. ASSERT(zfsvfs->z_root != 0);
  827. error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_UNLINKED_SET, 8, 1,
  828. &zfsvfs->z_unlinkedobj);
  829. if (error)
  830. goto out;
  831. error = zap_lookup(os, MASTER_NODE_OBJ,
  832. zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA],
  833. 8, 1, &zfsvfs->z_userquota_obj);
  834. if (error && error != ENOENT)
  835. goto out;
  836. error = zap_lookup(os, MASTER_NODE_OBJ,
  837. zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA],
  838. 8, 1, &zfsvfs->z_groupquota_obj);
  839. if (error && error != ENOENT)
  840. goto out;
  841. error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_FUID_TABLES, 8, 1,
  842. &zfsvfs->z_fuid_obj);
  843. if (error && error != ENOENT)
  844. goto out;
  845. error = zap_lookup(os, MASTER_NODE_OBJ, ZFS_SHARES_DIR, 8, 1,
  846. &zfsvfs->z_shares_dir);
  847. if (error && error != ENOENT)
  848. goto out;
  849. mutex_init(&zfsvfs->z_znodes_lock, NULL, MUTEX_DEFAULT, NULL);
  850. mutex_init(&zfsvfs->z_lock, NULL, MUTEX_DEFAULT, NULL);
  851. list_create(&zfsvfs->z_all_znodes, sizeof (znode_t),
  852. offsetof(znode_t, z_link_node));
  853. rrw_init(&zfsvfs->z_teardown_lock);
  854. rw_init(&zfsvfs->z_teardown_inactive_lock, NULL, RW_DEFAULT, NULL);
  855. rw_init(&zfsvfs->z_fuid_lock, NULL, RW_DEFAULT, NULL);
  856. for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
  857. mutex_init(&zfsvfs->z_hold_mtx[i], NULL, MUTEX_DEFAULT, NULL);
  858. *zfvp = zfsvfs;
  859. return (0);
  860. out:
  861. dmu_objset_disown(os, zfsvfs);
  862. *zfvp = NULL;
  863. kmem_free(zfsvfs, sizeof (zfsvfs_t));
  864. return (error);
  865. }
  866. static int
  867. zfsvfs_setup(zfsvfs_t *zfsvfs, boolean_t mounting)
  868. {
  869. int error;
  870. error = zfs_register_callbacks(zfsvfs->z_vfs);
  871. if (error)
  872. return (error);
  873. /*
  874. * Set the objset user_ptr to track its zfsvfs.
  875. */
  876. mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
  877. dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
  878. mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
  879. zfsvfs->z_log = zil_open(zfsvfs->z_os, zfs_get_data);
  880. /*
  881. * If we are not mounting (ie: online recv), then we don't
  882. * have to worry about replaying the log as we blocked all
  883. * operations out since we closed the ZIL.
  884. */
  885. if (mounting) {
  886. boolean_t readonly;
  887. /*
  888. * During replay we remove the read only flag to
  889. * allow replays to succeed.
  890. */
  891. readonly = zfsvfs->z_vfs->vfs_flag & VFS_RDONLY;
  892. if (readonly != 0)
  893. zfsvfs->z_vfs->vfs_flag &= ~VFS_RDONLY;
  894. else
  895. zfs_unlinked_drain(zfsvfs);
  896. /*
  897. * Parse and replay the intent log.
  898. *
  899. * Because of ziltest, this must be done after
  900. * zfs_unlinked_drain(). (Further note: ziltest
  901. * doesn't use readonly mounts, where
  902. * zfs_unlinked_drain() isn't called.) This is because
  903. * ziltest causes spa_sync() to think it's committed,
  904. * but actually it is not, so the intent log contains
  905. * many txg's worth of changes.
  906. *
  907. * In particular, if object N is in the unlinked set in
  908. * the last txg to actually sync, then it could be
  909. * actually freed in a later txg and then reallocated
  910. * in a yet later txg. This would write a "create
  911. * object N" record to the intent log. Normally, this
  912. * would be fine because the spa_sync() would have
  913. * written out the fact that object N is free, before
  914. * we could write the "create object N" intent log
  915. * record.
  916. *
  917. * But when we are in ziltest mode, we advance the "open
  918. * txg" without actually spa_sync()-ing the changes to
  919. * disk. So we would see that object N is still
  920. * allocated and in the unlinked set, and there is an
  921. * intent log record saying to allocate it.
  922. */
  923. if (spa_writeable(dmu_objset_spa(zfsvfs->z_os))) {
  924. if (zil_replay_disable) {
  925. zil_destroy(zfsvfs->z_log, B_FALSE);
  926. } else {
  927. zfsvfs->z_replay = B_TRUE;
  928. zil_replay(zfsvfs->z_os, zfsvfs,
  929. zfs_replay_vector);
  930. zfsvfs->z_replay = B_FALSE;
  931. }
  932. }
  933. zfsvfs->z_vfs->vfs_flag |= readonly; /* restore readonly bit */
  934. }
  935. return (0);
  936. }
  937. void
  938. zfsvfs_free(zfsvfs_t *zfsvfs)
  939. {
  940. int i;
  941. extern krwlock_t zfsvfs_lock; /* in zfs_znode.c */
  942. /*
  943. * This is a barrier to prevent the filesystem from going away in
  944. * zfs_znode_move() until we can safely ensure that the filesystem is
  945. * not unmounted. We consider the filesystem valid before the barrier
  946. * and invalid after the barrier.
  947. */
  948. rw_enter(&zfsvfs_lock, RW_READER);
  949. rw_exit(&zfsvfs_lock);
  950. zfs_fuid_destroy(zfsvfs);
  951. mutex_destroy(&zfsvfs->z_znodes_lock);
  952. mutex_destroy(&zfsvfs->z_lock);
  953. list_destroy(&zfsvfs->z_all_znodes);
  954. rrw_destroy(&zfsvfs->z_teardown_lock);
  955. rw_destroy(&zfsvfs->z_teardown_inactive_lock);
  956. rw_destroy(&zfsvfs->z_fuid_lock);
  957. for (i = 0; i != ZFS_OBJ_MTX_SZ; i++)
  958. mutex_destroy(&zfsvfs->z_hold_mtx[i]);
  959. kmem_free(zfsvfs, sizeof (zfsvfs_t));
  960. }
  961. static void
  962. zfs_set_fuid_feature(zfsvfs_t *zfsvfs)
  963. {
  964. zfsvfs->z_use_fuids = USE_FUIDS(zfsvfs->z_version, zfsvfs->z_os);
  965. if (zfsvfs->z_vfs) {
  966. if (zfsvfs->z_use_fuids) {
  967. vfs_set_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
  968. vfs_set_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
  969. vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
  970. vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
  971. vfs_set_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
  972. vfs_set_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
  973. } else {
  974. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_XVATTR);
  975. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_SYSATTR_VIEWS);
  976. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACEMASKONACCESS);
  977. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACLONCREATE);
  978. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_ACCESS_FILTER);
  979. vfs_clear_feature(zfsvfs->z_vfs, VFSFT_REPARSE);
  980. }
  981. }
  982. zfsvfs->z_use_sa = USE_SA(zfsvfs->z_version, zfsvfs->z_os);
  983. }
  984. static int
  985. zfs_domount(vfs_t *vfsp, char *osname)
  986. {
  987. dev_t mount_dev;
  988. uint64_t recordsize, fsid_guid;
  989. int error = 0;
  990. zfsvfs_t *zfsvfs;
  991. ASSERT(vfsp);
  992. ASSERT(osname);
  993. error = zfsvfs_create(osname, &zfsvfs);
  994. if (error)
  995. return (error);
  996. zfsvfs->z_vfs = vfsp;
  997. /* Initialize the generic filesystem structure. */
  998. vfsp->vfs_bcount = 0;
  999. vfsp->vfs_data = NULL;
  1000. if (zfs_create_unique_device(&mount_dev) == -1) {
  1001. error = ENODEV;
  1002. goto out;
  1003. }
  1004. ASSERT(vfs_devismounted(mount_dev) == 0);
  1005. if (error = dsl_prop_get_integer(osname, "recordsize", &recordsize,
  1006. NULL))
  1007. goto out;
  1008. vfsp->vfs_dev = mount_dev;
  1009. vfsp->vfs_fstype = zfsfstype;
  1010. vfsp->vfs_bsize = recordsize;
  1011. vfsp->vfs_flag |= VFS_NOTRUNC;
  1012. vfsp->vfs_data = zfsvfs;
  1013. /*
  1014. * The fsid is 64 bits, composed of an 8-bit fs type, which
  1015. * separates our fsid from any other filesystem types, and a
  1016. * 56-bit objset unique ID. The objset unique ID is unique to
  1017. * all objsets open on this system, provided by unique_create().
  1018. * The 8-bit fs type must be put in the low bits of fsid[1]
  1019. * because that's where other Solaris filesystems put it.
  1020. */
  1021. fsid_guid = dmu_objset_fsid_guid(zfsvfs->z_os);
  1022. ASSERT((fsid_guid & ~((1ULL<<56)-1)) == 0);
  1023. vfsp->vfs_fsid.val[0] = fsid_guid;
  1024. vfsp->vfs_fsid.val[1] = ((fsid_guid>>32) << 8) |
  1025. zfsfstype & 0xFF;
  1026. /*
  1027. * Set features for file system.
  1028. */
  1029. zfs_set_fuid_feature(zfsvfs);
  1030. if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
  1031. vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
  1032. vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
  1033. vfs_set_feature(vfsp, VFSFT_NOCASESENSITIVE);
  1034. } else if (zfsvfs->z_case == ZFS_CASE_MIXED) {
  1035. vfs_set_feature(vfsp, VFSFT_DIRENTFLAGS);
  1036. vfs_set_feature(vfsp, VFSFT_CASEINSENSITIVE);
  1037. }
  1038. vfs_set_feature(vfsp, VFSFT_ZEROCOPY_SUPPORTED);
  1039. if (dmu_objset_is_snapshot(zfsvfs->z_os)) {
  1040. uint64_t pval;
  1041. atime_changed_cb(zfsvfs, B_FALSE);
  1042. readonly_changed_cb(zfsvfs, B_TRUE);
  1043. if (error = dsl_prop_get_integer(osname, "xattr", &pval, NULL))
  1044. goto out;
  1045. xattr_changed_cb(zfsvfs, pval);
  1046. zfsvfs->z_issnap = B_TRUE;
  1047. zfsvfs->z_os->os_sync = ZFS_SYNC_DISABLED;
  1048. mutex_enter(&zfsvfs->z_os->os_user_ptr_lock);
  1049. dmu_objset_set_user(zfsvfs->z_os, zfsvfs);
  1050. mutex_exit(&zfsvfs->z_os->os_user_ptr_lock);
  1051. } else {
  1052. error = zfsvfs_setup(zfsvfs, B_TRUE);
  1053. }
  1054. if (!zfsvfs->z_issnap)
  1055. zfsctl_create(zfsvfs);
  1056. out:
  1057. if (error) {
  1058. dmu_objset_disown(zfsvfs->z_os, zfsvfs);
  1059. zfsvfs_free(zfsvfs);
  1060. } else {
  1061. atomic_add_32(&zfs_active_fs_count, 1);
  1062. }
  1063. return (error);
  1064. }
  1065. void
  1066. zfs_unregister_callbacks(zfsvfs_t *zfsvfs)
  1067. {
  1068. objset_t *os = zfsvfs->z_os;
  1069. struct dsl_dataset *ds;
  1070. /*
  1071. * Unregister properties.
  1072. */
  1073. if (!dmu_objset_is_snapshot(os)) {
  1074. ds = dmu_objset_ds(os);
  1075. VERIFY(dsl_prop_unregister(ds, "atime", atime_changed_cb,
  1076. zfsvfs) == 0);
  1077. VERIFY(dsl_prop_unregister(ds, "xattr", xattr_changed_cb,
  1078. zfsvfs) == 0);
  1079. VERIFY(dsl_prop_unregister(ds, "recordsize", blksz_changed_cb,
  1080. zfsvfs) == 0);
  1081. VERIFY(dsl_prop_unregister(ds, "readonly", readonly_changed_cb,
  1082. zfsvfs) == 0);
  1083. VERIFY(dsl_prop_unregister(ds, "devices", devices_changed_cb,
  1084. zfsvfs) == 0);
  1085. VERIFY(dsl_prop_unregister(ds, "setuid", setuid_changed_cb,
  1086. zfsvfs) == 0);
  1087. VERIFY(dsl_prop_unregister(ds, "exec", exec_changed_cb,
  1088. zfsvfs) == 0);
  1089. VERIFY(dsl_prop_unregister(ds, "snapdir", snapdir_changed_cb,
  1090. zfsvfs) == 0);
  1091. VERIFY(dsl_prop_unregister(ds, "aclmode", acl_mode_changed_cb,
  1092. zfsvfs) == 0);
  1093. VERIFY(dsl_prop_unregister(ds, "aclinherit",
  1094. acl_inherit_changed_cb, zfsvfs) == 0);
  1095. VERIFY(dsl_prop_unregister(ds, "vscan",
  1096. vscan_changed_cb, zfsvfs) == 0);
  1097. }
  1098. }
  1099. /*
  1100. * Convert a decimal digit string to a uint64_t integer.
  1101. */
  1102. static int
  1103. str_to_uint64(char *str, uint64_t *objnum)
  1104. {
  1105. uint64_t num = 0;
  1106. while (*str) {
  1107. if (*str < '0' || *str > '9')
  1108. return (EINVAL);
  1109. num = num*10 + *str++ - '0';
  1110. }
  1111. *objnum = num;
  1112. return (0);
  1113. }
  1114. /*
  1115. * The boot path passed from the boot loader is in the form of
  1116. * "rootpool-name/root-filesystem-object-number'. Convert this
  1117. * string to a dataset name: "rootpool-name/root-filesystem-name".
  1118. */
  1119. static int
  1120. zfs_parse_bootfs(char *bpath, char *outpath)
  1121. {
  1122. char *slashp;
  1123. uint64_t objnum;
  1124. int error;
  1125. if (*bpath == 0 || *bpath == '/')
  1126. return (EINVAL);
  1127. (void) strcpy(outpath, bpath);
  1128. slashp = strchr(bpath, '/');
  1129. /* if no '/', just return the pool name */
  1130. if (slashp == NULL) {
  1131. return (0);
  1132. }
  1133. /* if not a number, just return the root dataset name */
  1134. if (str_to_uint64(slashp+1, &objnum)) {
  1135. return (0);
  1136. }
  1137. *slashp = '\0';
  1138. error = dsl_dsobj_to_dsname(bpath, objnum, outpath);
  1139. *slashp = '/';
  1140. return (error);
  1141. }
  1142. /*
  1143. * zfs_check_global_label:
  1144. * Check that the hex label string is appropriate for the dataset
  1145. * being mounted into the global_zone proper.
  1146. *
  1147. * Return an error if the hex label string is not default or
  1148. * admin_low/admin_high. For admin_low labels, the corresponding
  1149. * dataset must be readonly.
  1150. */
  1151. int
  1152. zfs_check_global_label(const char *dsname, const char *hexsl)
  1153. {
  1154. if (strcasecmp(hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
  1155. return (0);
  1156. if (strcasecmp(hexsl, ADMIN_HIGH) == 0)
  1157. return (0);
  1158. if (strcasecmp(hexsl, ADMIN_LOW) == 0) {
  1159. /* must be readonly */
  1160. uint64_t rdonly;
  1161. if (dsl_prop_get_integer(dsname,
  1162. zfs_prop_to_name(ZFS_PROP_READONLY), &rdonly, NULL))
  1163. return (EACCES);
  1164. return (rdonly ? 0 : EACCES);
  1165. }
  1166. return (EACCES);
  1167. }
  1168. /*
  1169. * zfs_mount_label_policy:
  1170. * Determine whether the mount is allowed according to MAC check.
  1171. * by comparing (where appropriate) label of the dataset against
  1172. * the label of the zone being mounted into. If the dataset has
  1173. * no label, create one.
  1174. *
  1175. * Returns:
  1176. * 0 : access allowed
  1177. * >0 : error code, such as EACCES
  1178. */
  1179. static int
  1180. zfs_mount_label_policy(vfs_t *vfsp, char *osname)
  1181. {
  1182. int error, retv;
  1183. zone_t *mntzone = NULL;
  1184. ts_label_t *mnt_tsl;
  1185. bslabel_t *mnt_sl;
  1186. bslabel_t ds_sl;
  1187. char ds_hexsl[MAXNAMELEN];
  1188. retv = EACCES; /* assume the worst */
  1189. /*
  1190. * Start by getting the dataset label if it exists.
  1191. */
  1192. error = dsl_prop_get(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
  1193. 1, sizeof (ds_hexsl), &ds_hexsl, NULL);
  1194. if (error)
  1195. return (EACCES);
  1196. /*
  1197. * If labeling is NOT enabled, then disallow the mount of datasets
  1198. * which have a non-default label already. No other label checks
  1199. * are needed.
  1200. */
  1201. if (!is_system_labeled()) {
  1202. if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0)
  1203. return (0);
  1204. return (EACCES);
  1205. }
  1206. /*
  1207. * Get the label of the mountpoint. If mounting into the global
  1208. * zone (i.e. mountpoint is not within an active zone and the
  1209. * zoned property is off), the label must be default or
  1210. * admin_low/admin_high only; no other checks are needed.
  1211. */
  1212. mntzone = zone_find_by_any_path(refstr_value(vfsp->vfs_mntpt), B_FALSE);
  1213. if (mntzone->zone_id == GLOBAL_ZONEID) {
  1214. uint64_t zoned;
  1215. zone_rele(mntzone);
  1216. if (dsl_prop_get_integer(osname,
  1217. zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL))
  1218. return (EACCES);
  1219. if (!zoned)
  1220. return (zfs_check_global_label(osname, ds_hexsl));
  1221. else
  1222. /*
  1223. * This is the case of a zone dataset being mounted
  1224. * initially, before the zone has been fully created;
  1225. * allow this mount into global zone.
  1226. */
  1227. return (0);
  1228. }
  1229. mnt_tsl = mntzone->zone_slabel;
  1230. ASSERT(mnt_tsl != NULL);
  1231. label_hold(mnt_tsl);
  1232. mnt_sl = label2bslabel(mnt_tsl);
  1233. if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) == 0) {
  1234. /*
  1235. * The dataset doesn't have a real label, so fabricate one.
  1236. */
  1237. char *str = NULL;
  1238. if (l_to_str_internal(mnt_sl, &str) == 0 &&
  1239. dsl_prop_set(osname, zfs_prop_to_name(ZFS_PROP_MLSLABEL),
  1240. ZPROP_SRC_LOCAL, 1, strlen(str) + 1, str) == 0)
  1241. retv = 0;
  1242. if (str != NULL)
  1243. kmem_free(str, strlen(str) + 1);
  1244. } else if (hexstr_to_label(ds_hexsl, &ds_sl) == 0) {
  1245. /*
  1246. * Now compare labels to complete the MAC check. If the
  1247. * labels are equal then allow access. If the mountpoint
  1248. * label dominates the dataset label, allow readonly access.
  1249. * Otherwise, access is denied.
  1250. */
  1251. if (blequal(mnt_sl, &ds_sl))
  1252. retv = 0;
  1253. else if (bldominates(mnt_sl, &ds_sl)) {
  1254. vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
  1255. retv = 0;
  1256. }
  1257. }
  1258. label_rele(mnt_tsl);
  1259. zone_rele(mntzone);
  1260. return (retv);
  1261. }
  1262. static int
  1263. zfs_mountroot(vfs_t *vfsp, enum whymountroot why)
  1264. {
  1265. int error = 0;
  1266. static int zfsrootdone = 0;
  1267. zfsvfs_t *zfsvfs = NULL;
  1268. znode_t *zp = NULL;
  1269. vnode_t *vp = NULL;
  1270. char *zfs_bootfs;
  1271. char *zfs_devid;
  1272. ASSERT(vfsp);
  1273. /*
  1274. * The filesystem that we mount as root is defined in the
  1275. * boot property "zfs-bootfs" with a format of
  1276. * "poolname/root-dataset-objnum".
  1277. */
  1278. if (why == ROOT_INIT) {
  1279. if (zfsrootdone++)
  1280. return (EBUSY);
  1281. /*
  1282. * the process of doing a spa_load will require the
  1283. * clock to be set before we could (for example) do
  1284. * something better by looking at the timestamp on
  1285. * an uberblock, so just set it to -1.
  1286. */
  1287. clkset(-1);
  1288. if ((zfs_bootfs = spa_get_bootprop("zfs-bootfs")) == NULL) {
  1289. cmn_err(CE_NOTE, "spa_get_bootfs: can not get "
  1290. "bootfs name");
  1291. return (EINVAL);
  1292. }
  1293. zfs_devid = spa_get_bootprop("diskdevid");
  1294. error = spa_import_rootpool(rootfs.bo_name, zfs_devid);
  1295. if (zfs_devid)
  1296. spa_free_bootprop(zfs_devid);
  1297. if (error) {
  1298. spa_free_bootprop(zfs_bootfs);
  1299. cmn_err(CE_NOTE, "spa_import_rootpool: error %d",
  1300. error);
  1301. return (error);
  1302. }
  1303. if (error = zfs_parse_bootfs(zfs_bootfs, rootfs.bo_name)) {
  1304. spa_free_bootprop(zfs_bootfs);
  1305. cmn_err(CE_NOTE, "zfs_parse_bootfs: error %d",
  1306. error);
  1307. return (error);
  1308. }
  1309. spa_free_bootprop(zfs_bootfs);
  1310. if (error = vfs_lock(vfsp))
  1311. return (error);
  1312. if (error = zfs_domount(vfsp, rootfs.bo_name)) {
  1313. cmn_err(CE_NOTE, "zfs_domount: error %d", error);
  1314. goto out;
  1315. }
  1316. zfsvfs = (zfsvfs_t *)vfsp->vfs_data;
  1317. ASSERT(zfsvfs);
  1318. if (error = zfs_zget(zfsvfs, zfsvfs->z_root, &zp)) {
  1319. cmn_err(CE_NOTE, "zfs_zget: error %d", error);
  1320. goto out;
  1321. }
  1322. vp = ZTOV(zp);
  1323. mutex_enter(&vp->v_lock);
  1324. vp->v_flag |= VROOT;
  1325. mutex_exit(&vp->v_lock);
  1326. rootvp = vp;
  1327. /*
  1328. * Leave rootvp held. The root file system is never unmounted.
  1329. */
  1330. vfs_add((struct vnode *)0, vfsp,
  1331. (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
  1332. out:
  1333. vfs_unlock(vfsp);
  1334. return (error);
  1335. } else if (why == ROOT_REMOUNT) {
  1336. readonly_changed_cb(vfsp->vfs_data, B_FALSE);
  1337. vfsp->vfs_flag |= VFS_REMOUNT;
  1338. /* refresh mount options */
  1339. zfs_unregister_callbacks(vfsp->vfs_data);
  1340. return (zfs_register_callbacks(vfsp));
  1341. } else if (why == ROOT_UNMOUNT) {
  1342. zfs_unregister_callbacks((zfsvfs_t *)vfsp->vfs_data);
  1343. (void) zfs_sync(vfsp, 0, 0);
  1344. return (0);
  1345. }
  1346. /*
  1347. * if "why" is equal to anything else other than ROOT_INIT,
  1348. * ROOT_REMOUNT, or ROOT_UNMOUNT, we do not support it.
  1349. */
  1350. return (ENOTSUP);
  1351. }
  1352. /*ARGSUSED*/
  1353. static int
  1354. zfs_mount(vfs_t *vfsp, vnode_t *mvp, struct mounta *uap, cred_t *cr)
  1355. {
  1356. char *osname;
  1357. pathname_t spn;
  1358. int error = 0;
  1359. uio_seg_t fromspace = (uap->flags & MS_SYSSPACE) ?
  1360. UIO_SYSSPACE : UIO_USERSPACE;
  1361. int canwrite;
  1362. if (mvp->v_type != VDIR)
  1363. return (ENOTDIR);
  1364. mutex_enter(&mvp->v_lock);
  1365. if ((uap->flags & MS_REMOUNT) == 0 &&
  1366. (uap->flags & MS_OVERLAY) == 0 &&
  1367. (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
  1368. mutex_exit(&mvp->v_lock);
  1369. return (EBUSY);
  1370. }
  1371. mutex_exit(&mvp->v_lock);
  1372. /*
  1373. * ZFS does not support passing unparsed data in via MS_DATA.
  1374. * Users should use the MS_OPTIONSTR interface; this means
  1375. * that all option parsing is already done and the options struct
  1376. * can be interrogated.
  1377. */
  1378. if ((uap->flags & MS_DATA) && uap->datalen > 0)
  1379. return (EINVAL);
  1380. /*
  1381. * Get the objset name (the "special" mount argument).
  1382. */
  1383. if (error = pn_get(uap->spec, fromspace, &spn))
  1384. return (error);
  1385. osname = spn.pn_path;
  1386. /*
  1387. * Check for mount privilege?
  1388. *
  1389. * If we don't have privilege then see if
  1390. * we have local permission to allow it
  1391. */
  1392. error = secpolicy_fs_mount(cr, mvp, vfsp);
  1393. if (error) {
  1394. if (dsl_deleg_access(osname, ZFS_DELEG_PERM_MOUNT, cr) == 0) {
  1395. vattr_t vattr;
  1396. /*
  1397. * Make sure user is the owner of the mount point
  1398. * or has sufficient privileges.
  1399. */
  1400. vattr.va_mask = AT_UID;
  1401. if (VOP_GETATTR(mvp, &vattr, 0, cr, NULL)) {
  1402. goto out;
  1403. }
  1404. if (secpolicy_vnode_owner(cr, vattr.va_uid) != 0 &&
  1405. VOP_ACCESS(mvp, VWRITE, 0, cr, NULL) != 0) {
  1406. goto out;
  1407. }
  1408. secpolicy_fs_mount_clearopts(cr, vfsp);
  1409. } else {
  1410. goto out;
  1411. }
  1412. }
  1413. /*
  1414. * Refuse to mount a filesystem if we are in a local zone and the
  1415. * dataset is not visible.
  1416. */
  1417. if (!INGLOBALZONE(curproc) &&
  1418. (!zone_dataset_visible(osname, &canwrite) || !canwrite)) {
  1419. error = EPERM;
  1420. goto out;
  1421. }
  1422. error = zfs_mount_label_policy(vfsp, osname);
  1423. if (error)
  1424. goto out;
  1425. /*
  1426. * When doing a remount, we simply refresh our temporary properties
  1427. * according to those options set in the current VFS options.
  1428. */
  1429. if (uap->flags & MS_REMOUNT) {
  1430. /* refresh mount options */
  1431. zfs_unregister_callbacks(vfsp->vfs_data);
  1432. error = zfs_register_callbacks(vfsp);
  1433. goto out;
  1434. }
  1435. error = zfs_domount(vfsp, osname);
  1436. /*
  1437. * Add an extra VFS_HOLD on our parent vfs so that it can't
  1438. * disappear due to a forced unmount.
  1439. */
  1440. if (error == 0 && ((zfsvfs_t *)vfsp->vfs_data)->z_issnap)
  1441. VFS_HOLD(mvp->v_vfsp);
  1442. out:
  1443. pn_free(&spn);
  1444. return (error);
  1445. }
  1446. static int
  1447. zfs_statvfs(vfs_t *vfsp, struct statvfs64 *statp)
  1448. {
  1449. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  1450. dev32_t d32;
  1451. uint64_t refdbytes, availbytes, usedobjs, availobjs;
  1452. ZFS_ENTER(zfsvfs);
  1453. dmu_objset_space(zfsvfs->z_os,
  1454. &refdbytes, &availbytes, &usedobjs, &availobjs);
  1455. /*
  1456. * The underlying storage pool actually uses multiple block sizes.
  1457. * We report the fragsize as the smallest block size we support,
  1458. * and we report our blocksize as the filesystem's maximum blocksize.
  1459. */
  1460. statp->f_frsize = 1UL << SPA_MINBLOCKSHIFT;
  1461. statp->f_bsize = zfsvfs->z_max_blksz;
  1462. /*
  1463. * The following report "total" blocks of various kinds in the
  1464. * file system, but reported in terms of f_frsize - the
  1465. * "fragment" size.
  1466. */
  1467. statp->f_blocks = (refdbytes + availbytes) >> SPA_MINBLOCKSHIFT;
  1468. statp->f_bfree = availbytes >> SPA_MINBLOCKSHIFT;
  1469. statp->f_bavail = statp->f_bfree; /* no root reservation */
  1470. /*
  1471. * statvfs() should really be called statufs(), because it assumes
  1472. * static metadata. ZFS doesn't preallocate files, so the best
  1473. * we can do is report the max that could possibly fit in f_files,
  1474. * and that minus the number actually used in f_ffree.
  1475. * For f_ffree, report the smaller of the number of object available
  1476. * and the number of blocks (each object will take at least a block).
  1477. */
  1478. statp->f_ffree = MIN(availobjs, statp->f_bfree);
  1479. statp->f_favail = statp->f_ffree; /* no "root reservation" */
  1480. statp->f_files = statp->f_ffree + usedobjs;
  1481. (void) cmpldev(&d32, vfsp->vfs_dev);
  1482. statp->f_fsid = d32;
  1483. /*
  1484. * We're a zfs filesystem.
  1485. */
  1486. (void) strcpy(statp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
  1487. statp->f_flag = vf_to_stf(vfsp->vfs_flag);
  1488. statp->f_namemax = ZFS_MAXNAMELEN;
  1489. /*
  1490. * We have all of 32 characters to stuff a string here.
  1491. * Is there anything useful we could/should provide?
  1492. */
  1493. bzero(statp->f_fstr, sizeof (statp->f_fstr));
  1494. ZFS_EXIT(zfsvfs);
  1495. return (0);
  1496. }
  1497. static int
  1498. zfs_root(vfs_t *vfsp, vnode_t **vpp)
  1499. {
  1500. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  1501. znode_t *rootzp;
  1502. int error;
  1503. ZFS_ENTER(zfsvfs);
  1504. error = zfs_zget(zfsvfs, zfsvfs->z_root, &rootzp);
  1505. if (error == 0)
  1506. *vpp = ZTOV(rootzp);
  1507. ZFS_EXIT(zfsvfs);
  1508. return (error);
  1509. }
  1510. /*
  1511. * Teardown the zfsvfs::z_os.
  1512. *
  1513. * Note, if 'unmounting' if FALSE, we return with the 'z_teardown_lock'
  1514. * and 'z_teardown_inactive_lock' held.
  1515. */
  1516. static int
  1517. zfsvfs_teardown(zfsvfs_t *zfsvfs, boolean_t unmounting)
  1518. {
  1519. znode_t *zp;
  1520. rrw_enter(&zfsvfs->z_teardown_lock, RW_WRITER, FTAG);
  1521. if (!unmounting) {
  1522. /*
  1523. * We purge the parent filesystem's vfsp as the parent
  1524. * filesystem and all of its snapshots have their vnode's
  1525. * v_vfsp set to the parent's filesystem's vfsp. Note,
  1526. * 'z_parent' is self referential for non-snapshots.
  1527. */
  1528. (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
  1529. }
  1530. /*
  1531. * Close the zil. NB: Can't close the zil while zfs_inactive
  1532. * threads are blocked as zil_close can call zfs_inactive.
  1533. */
  1534. if (zfsvfs->z_log) {
  1535. zil_close(zfsvfs->z_log);
  1536. zfsvfs->z_log = NULL;
  1537. }
  1538. rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_WRITER);
  1539. /*
  1540. * If we are not unmounting (ie: online recv) and someone already
  1541. * unmounted this file system while we were doing the switcheroo,
  1542. * or a reopen of z_os failed then just bail out now.
  1543. */
  1544. if (!unmounting && (zfsvfs->z_unmounted || zfsvfs->z_os == NULL)) {
  1545. rw_exit(&zfsvfs->z_teardown_inactive_lock);
  1546. rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
  1547. return (EIO);
  1548. }
  1549. /*
  1550. * At this point there are no vops active, and any new vops will
  1551. * fail with EIO since we have z_teardown_lock for writer (only
  1552. * relavent for forced unmount).
  1553. *
  1554. * Release all holds on dbufs.
  1555. */
  1556. mutex_enter(&zfsvfs->z_znodes_lock);
  1557. for (zp = list_head(&zfsvfs->z_all_znodes); zp != NULL;
  1558. zp = list_next(&zfsvfs->z_all_znodes, zp))
  1559. if (zp->z_sa_hdl) {
  1560. ASSERT(ZTOV(zp)->v_count > 0);
  1561. zfs_znode_dmu_fini(zp);
  1562. }
  1563. mutex_exit(&zfsvfs->z_znodes_lock);
  1564. /*
  1565. * If we are unmounting, set the unmounted flag and let new vops
  1566. * unblock. zfs_inactive will have the unmounted behavior, and all
  1567. * other vops will fail with EIO.
  1568. */
  1569. if (unmounting) {
  1570. zfsvfs->z_unmounted = B_TRUE;
  1571. rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
  1572. rw_exit(&zfsvfs->z_teardown_inactive_lock);
  1573. }
  1574. /*
  1575. * z_os will be NULL if there was an error in attempting to reopen
  1576. * zfsvfs, so just return as the properties had already been
  1577. * unregistered and cached data had been evicted before.
  1578. */
  1579. if (zfsvfs->z_os == NULL)
  1580. return (0);
  1581. /*
  1582. * Unregister properties.
  1583. */
  1584. zfs_unregister_callbacks(zfsvfs);
  1585. /*
  1586. * Evict cached data
  1587. */
  1588. if (dmu_objset_is_dirty_anywhere(zfsvfs->z_os))
  1589. if (!(zfsvfs->z_vfs->vfs_flag & VFS_RDONLY))
  1590. txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
  1591. (void) dmu_objset_evict_dbufs(zfsvfs->z_os);
  1592. return (0);
  1593. }
  1594. /*ARGSUSED*/
  1595. static int
  1596. zfs_umount(vfs_t *vfsp, int fflag, cred_t *cr)
  1597. {
  1598. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  1599. objset_t *os;
  1600. int ret;
  1601. ret = secpolicy_fs_unmount(cr, vfsp);
  1602. if (ret) {
  1603. if (dsl_deleg_access((char *)refstr_value(vfsp->vfs_resource),
  1604. ZFS_DELEG_PERM_MOUNT, cr))
  1605. return (ret);
  1606. }
  1607. /*
  1608. * We purge the parent filesystem's vfsp as the parent filesystem
  1609. * and all of its snapshots have their vnode's v_vfsp set to the
  1610. * parent's filesystem's vfsp. Note, 'z_parent' is self
  1611. * referential for non-snapshots.
  1612. */
  1613. (void) dnlc_purge_vfsp(zfsvfs->z_parent->z_vfs, 0);
  1614. /*
  1615. * Unmount any snapshots mounted under .zfs before unmounting the
  1616. * dataset itself.
  1617. */
  1618. if (zfsvfs->z_ctldir != NULL &&
  1619. (ret = zfsctl_umount_snapshots(vfsp, fflag, cr)) != 0) {
  1620. return (ret);
  1621. }
  1622. if (!(fflag & MS_FORCE)) {
  1623. /*
  1624. * Check the number of active vnodes in the file system.
  1625. * Our count is maintained in the vfs structure, but the
  1626. * number is off by 1 to indicate a hold on the vfs
  1627. * structure itself.
  1628. *
  1629. * The '.zfs' directory maintains a reference of its
  1630. * own, and any active references underneath are
  1631. * reflected in the vnode count.
  1632. */
  1633. if (zfsvfs->z_ctldir == NULL) {
  1634. if (vfsp->vfs_count > 1)
  1635. return (EBUSY);
  1636. } else {
  1637. if (vfsp->vfs_count > 2 ||
  1638. zfsvfs->z_ctldir->v_count > 1)
  1639. return (EBUSY);
  1640. }
  1641. }
  1642. vfsp->vfs_flag |= VFS_UNMOUNTED;
  1643. VERIFY(zfsvfs_teardown(zfsvfs, B_TRUE) == 0);
  1644. os = zfsvfs->z_os;
  1645. /*
  1646. * z_os will be NULL if there was an error in
  1647. * attempting to reopen zfsvfs.
  1648. */
  1649. if (os != NULL) {
  1650. /*
  1651. * Unset the objset user_ptr.
  1652. */
  1653. mutex_enter(&os->os_user_ptr_lock);
  1654. dmu_objset_set_user(os, NULL);
  1655. mutex_exit(&os->os_user_ptr_lock);
  1656. /*
  1657. * Finally release the objset
  1658. */
  1659. dmu_objset_disown(os, zfsvfs);
  1660. }
  1661. /*
  1662. * We can now safely destroy the '.zfs' directory node.
  1663. */
  1664. if (zfsvfs->z_ctldir != NULL)
  1665. zfsctl_destroy(zfsvfs);
  1666. return (0);
  1667. }
  1668. static int
  1669. zfs_vget(vfs_t *vfsp, vnode_t **vpp, fid_t *fidp)
  1670. {
  1671. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  1672. znode_t *zp;
  1673. uint64_t object = 0;
  1674. uint64_t fid_gen = 0;
  1675. uint64_t gen_mask;
  1676. uint64_t zp_gen;
  1677. int i, err;
  1678. *vpp = NULL;
  1679. ZFS_ENTER(zfsvfs);
  1680. if (fidp->fid_len == LONG_FID_LEN) {
  1681. zfid_long_t *zlfid = (zfid_long_t *)fidp;
  1682. uint64_t objsetid = 0;
  1683. uint64_t setgen = 0;
  1684. for (i = 0; i < sizeof (zlfid->zf_setid); i++)
  1685. objsetid |= ((uint64_t)zlfid->zf_setid[i]) << (8 * i);
  1686. for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
  1687. setgen |= ((uint64_t)zlfid->zf_setgen[i]) << (8 * i);
  1688. ZFS_EXIT(zfsvfs);
  1689. err = zfsctl_lookup_objset(vfsp, objsetid, &zfsvfs);
  1690. if (err)
  1691. return (EINVAL);
  1692. ZFS_ENTER(zfsvfs);
  1693. }
  1694. if (fidp->fid_len == SHORT_FID_LEN || fidp->fid_len == LONG_FID_LEN) {
  1695. zfid_short_t *zfid = (zfid_short_t *)fidp;
  1696. for (i = 0; i < sizeof (zfid->zf_object); i++)
  1697. object |= ((uint64_t)zfid->zf_object[i]) << (8 * i);
  1698. for (i = 0; i < sizeof (zfid->zf_gen); i++)
  1699. fid_gen |= ((uint64_t)zfid->zf_gen[i]) << (8 * i);
  1700. } else {
  1701. ZFS_EXIT(zfsvfs);
  1702. return (EINVAL);
  1703. }
  1704. /* A zero fid_gen means we are in the .zfs control directories */
  1705. if (fid_gen == 0 &&
  1706. (object == ZFSCTL_INO_ROOT || object == ZFSCTL_INO_SNAPDIR)) {
  1707. *vpp = zfsvfs->z_ctldir;
  1708. ASSERT(*vpp != NULL);
  1709. if (object == ZFSCTL_INO_SNAPDIR) {
  1710. VERIFY(zfsctl_root_lookup(*vpp, "snapshot", vpp, NULL,
  1711. 0, NULL, NULL, NULL, NULL, NULL) == 0);
  1712. } else {
  1713. VN_HOLD(*vpp);
  1714. }
  1715. ZFS_EXIT(zfsvfs);
  1716. return (0);
  1717. }
  1718. gen_mask = -1ULL >> (64 - 8 * i);
  1719. dprintf("getting %llu [%u mask %llx]\n", object, fid_gen, gen_mask);
  1720. if (err = zfs_zget(zfsvfs, object, &zp)) {
  1721. ZFS_EXIT(zfsvfs);
  1722. return (err);
  1723. }
  1724. (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen,
  1725. sizeof (uint64_t));
  1726. zp_gen = zp_gen & gen_mask;
  1727. if (zp_gen == 0)
  1728. zp_gen = 1;
  1729. if (zp->z_unlinked || zp_gen != fid_gen) {
  1730. dprintf("znode gen (%u) != fid gen (%u)\n", zp_gen, fid_gen);
  1731. VN_RELE(ZTOV(zp));
  1732. ZFS_EXIT(zfsvfs);
  1733. return (EINVAL);
  1734. }
  1735. *vpp = ZTOV(zp);
  1736. ZFS_EXIT(zfsvfs);
  1737. return (0);
  1738. }
  1739. /*
  1740. * Block out VOPs and close zfsvfs_t::z_os
  1741. *
  1742. * Note, if successful, then we return with the 'z_teardown_lock' and
  1743. * 'z_teardown_inactive_lock' write held.
  1744. */
  1745. int
  1746. zfs_suspend_fs(zfsvfs_t *zfsvfs)
  1747. {
  1748. int error;
  1749. if ((error = zfsvfs_teardown(zfsvfs, B_FALSE)) != 0)
  1750. return (error);
  1751. dmu_objset_disown(zfsvfs->z_os, zfsvfs);
  1752. return (0);
  1753. }
  1754. /*
  1755. * Reopen zfsvfs_t::z_os and release VOPs.
  1756. */
  1757. int
  1758. zfs_resume_fs(zfsvfs_t *zfsvfs, const char *osname)
  1759. {
  1760. int err;
  1761. ASSERT(RRW_WRITE_HELD(&zfsvfs->z_teardown_lock));
  1762. ASSERT(RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock));
  1763. err = dmu_objset_own(osname, DMU_OST_ZFS, B_FALSE, zfsvfs,
  1764. &zfsvfs->z_os);
  1765. if (err) {
  1766. zfsvfs->z_os = NULL;
  1767. } else {
  1768. znode_t *zp;
  1769. uint64_t sa_obj = 0;
  1770. /*
  1771. * Make sure version hasn't changed
  1772. */
  1773. err = zfs_get_zplprop(zfsvfs->z_os, ZFS_PROP_VERSION,
  1774. &zfsvfs->z_version);
  1775. if (err)
  1776. goto bail;
  1777. err = zap_lookup(zfsvfs->z_os, MASTER_NODE_OBJ,
  1778. ZFS_SA_ATTRS, 8, 1, &sa_obj);
  1779. if (err && zfsvfs->z_version >= ZPL_VERSION_SA)
  1780. goto bail;
  1781. if ((err = sa_setup(zfsvfs->z_os, sa_obj,
  1782. zfs_attr_table, ZPL_END, &zfsvfs->z_attr_table)) != 0)
  1783. goto bail;
  1784. if (zfsvfs->z_version >= ZPL_VERSION_SA)
  1785. sa_register_update_callback(zfsvfs->z_os,
  1786. zfs_sa_upgrade);
  1787. VERIFY(zfsvfs_setup(zfsvfs, B_FALSE) == 0);
  1788. zfs_set_fuid_feature(zfsvfs);
  1789. /*
  1790. * Attempt to re-establish all the active znodes with
  1791. * their dbufs. If a zfs_rezget() fails, then we'll let
  1792. * any potential callers discover that via ZFS_ENTER_VERIFY_VP
  1793. * when they try to use their znode.
  1794. */
  1795. mutex_enter(&zfsvfs->z_znodes_lock);
  1796. for (zp = list_head(&zfsvfs->z_all_znodes); zp;
  1797. zp = list_next(&zfsvfs->z_all_znodes, zp)) {
  1798. (void) zfs_rezget(zp);
  1799. }
  1800. mutex_exit(&zfsvfs->z_znodes_lock);
  1801. }
  1802. bail:
  1803. /* release the VOPs */
  1804. rw_exit(&zfsvfs->z_teardown_inactive_lock);
  1805. rrw_exit(&zfsvfs->z_teardown_lock, FTAG);
  1806. if (err) {
  1807. /*
  1808. * Since we couldn't reopen zfsvfs::z_os, or
  1809. * setup the sa framework force unmount this file system.
  1810. */
  1811. if (vn_vfswlock(zfsvfs->z_vfs->vfs_vnodecovered) == 0)
  1812. (void) dounmount(zfsvfs->z_vfs, MS_FORCE, CRED());
  1813. }
  1814. return (err);
  1815. }
  1816. static void
  1817. zfs_freevfs(vfs_t *vfsp)
  1818. {
  1819. zfsvfs_t *zfsvfs = vfsp->vfs_data;
  1820. /*
  1821. * If this is a snapshot, we have an extra VFS_HOLD on our parent
  1822. * from zfs_mount(). Release it here. If we came through
  1823. * zfs_mountroot() instead, we didn't grab an extra hold, so
  1824. * skip the VFS_RELE for rootvfs.
  1825. */
  1826. if (zfsvfs->z_issnap && (vfsp != rootvfs))
  1827. VFS_RELE(zfsvfs->z_parent->z_vfs);
  1828. zfsvfs_free(zfsvfs);
  1829. atomic_add_32(&zfs_active_fs_count, -1);
  1830. }
  1831. /*
  1832. * VFS_INIT() initialization. Note that there is no VFS_FINI(),
  1833. * so we can't safely do any non-idempotent initialization here.
  1834. * Leave that to zfs_init() and zfs_fini(), which are called
  1835. * from the module's _init() and _fini() entry points.
  1836. */
  1837. /*ARGSUSED*/
  1838. static int
  1839. zfs_vfsinit(int fstype, char *name)
  1840. {
  1841. int error;
  1842. zfsfstype = fstype;
  1843. /*
  1844. * Setup vfsops and vnodeops tables.
  1845. */
  1846. error = vfs_setfsops(fstype, zfs_vfsops_template, &zfs_vfsops);
  1847. if (error != 0) {
  1848. cmn_err(CE_WARN, "zfs: bad vfs ops template");
  1849. }
  1850. error = zfs_create_op_tables();
  1851. if (error) {
  1852. zfs_remove_op_tables();
  1853. cmn_err(CE_WARN, "zfs: bad vnode ops template");
  1854. (void) vfs_freevfsops_by_type(zfsfstype);
  1855. return (error);
  1856. }
  1857. mutex_init(&zfs_dev_mtx, NULL, MUTEX_DEFAULT, NULL);
  1858. /*
  1859. * Unique major number for all zfs mounts.
  1860. * If we run out of 32-bit minors, we'll getudev() another major.
  1861. */
  1862. zfs_major = ddi_name_to_major(ZFS_DRIVER);
  1863. zfs_minor = ZFS_MIN_MINOR;
  1864. return (0);
  1865. }
  1866. void
  1867. zfs_init(void)
  1868. {
  1869. /*
  1870. * Initialize .zfs directory structures
  1871. */
  1872. zfsctl_init();
  1873. /*
  1874. * Initialize znode cache, vnode ops, etc...
  1875. */
  1876. zfs_znode_init();
  1877. dmu_objset_register_type(DMU_OST_ZFS, zfs_space_delta_cb);
  1878. }
  1879. void
  1880. zfs_fini(void)
  1881. {
  1882. zfsctl_fini();
  1883. zfs_znode_fini();
  1884. }
  1885. int
  1886. zfs_busy(void)
  1887. {
  1888. return (zfs_active_fs_count != 0);
  1889. }
  1890. int
  1891. zfs_set_version(zfsvfs_t *zfsvfs, uint64_t newvers)
  1892. {
  1893. int error;
  1894. objset_t *os = zfsvfs->z_os;
  1895. dmu_tx_t *tx;
  1896. if (newvers < ZPL_VERSION_INITIAL || newvers > ZPL_VERSION)
  1897. return (EINVAL);
  1898. if (newvers < zfsvfs->z_version)
  1899. return (EINVAL);
  1900. if (zfs_spa_version_map(newvers) >
  1901. spa_version(dmu_objset_spa(zfsvfs->z_os)))
  1902. return (ENOTSUP);
  1903. tx = dmu_tx_create(os);
  1904. dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_FALSE, ZPL_VERSION_STR);
  1905. if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
  1906. dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, B_TRUE,
  1907. ZFS_SA_ATTRS);
  1908. dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
  1909. }
  1910. error = dmu_tx_assign(tx, TXG_WAIT);
  1911. if (error) {
  1912. dmu_tx_abort(tx);
  1913. return (error);
  1914. }
  1915. error = zap_update(os, MASTER_NODE_OBJ, ZPL_VERSION_STR,
  1916. 8, 1, &newvers, tx);
  1917. if (error) {
  1918. dmu_tx_commit(tx);
  1919. return (error);
  1920. }
  1921. if (newvers >= ZPL_VERSION_SA && !zfsvfs->z_use_sa) {
  1922. uint64_t sa_obj;
  1923. ASSERT3U(spa_version(dmu_objset_spa(zfsvfs->z_os)), >=,
  1924. SPA_VERSION_SA);
  1925. sa_obj = zap_create(os, DMU_OT_SA_MASTER_NODE,
  1926. DMU_OT_NONE, 0, tx);
  1927. error = zap_add(os, MASTER_NODE_OBJ,
  1928. ZFS_SA_ATTRS, 8, 1, &sa_obj, tx);
  1929. ASSERT3U(error, ==, 0);
  1930. VERIFY(0 == sa_set_sa_object(os, sa_obj));
  1931. sa_register_update_callback(os, zfs_sa_upgrade);
  1932. }
  1933. spa_history_log_internal_ds(dmu_objset_ds(os), "upgrade", tx,
  1934. "from %llu to %llu", zfsvfs->z_version, newvers);
  1935. dmu_tx_commit(tx);
  1936. zfsvfs->z_version = newvers;
  1937. zfs_set_fuid_feature(zfsvfs);
  1938. return (0);
  1939. }
  1940. /*
  1941. * Read a property stored within the master node.
  1942. */
  1943. int
  1944. zfs_get_zplprop(objset_t *os, zfs_prop_t prop, uint64_t *value)
  1945. {
  1946. const char *pname;
  1947. int error = ENOENT;
  1948. /*
  1949. * Look up the file system's value for the property. For the
  1950. * version property, we look up a slightly different string.
  1951. */
  1952. if (prop == ZFS_PROP_VERSION)
  1953. pname = ZPL_VERSION_STR;
  1954. else
  1955. pname = zfs_prop_to_name(prop);
  1956. if (os != NULL)
  1957. error = zap_lookup(os, MASTER_NODE_OBJ, pname, 8, 1, value);
  1958. if (error == ENOENT) {
  1959. /* No value set, use the default value */
  1960. switch (prop) {
  1961. case ZFS_PROP_VERSION:
  1962. *value = ZPL_VERSION;
  1963. break;
  1964. case ZFS_PROP_NORMALIZE:
  1965. case ZFS_PROP_UTF8ONLY:
  1966. *value = 0;
  1967. break;
  1968. case ZFS_PROP_CASE:
  1969. *value = ZFS_CASE_SENSITIVE;
  1970. break;
  1971. default:
  1972. return (error);
  1973. }
  1974. error = 0;
  1975. }
  1976. return (error);
  1977. }
  1978. static vfsdef_t vfw = {
  1979. VFSDEF_VERSION,
  1980. MNTTYPE_ZFS,
  1981. zfs_vfsinit,
  1982. VSW_HASPROTO|VSW_CANRWRO|VSW_CANREMOUNT|VSW_VOLATILEDEV|VSW_STATS|
  1983. VSW_XID|VSW_ZMOUNT,
  1984. &zfs_mntopts
  1985. };
  1986. struct modlfs zfs_modlfs = {
  1987. &mod_fsops, "ZFS filesystem version " SPA_VERSION_STRING, &vfw
  1988. };