PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

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