Print this page
6093 zfsctl_shares_lookup should only VN_RELE() on zfs_zget() success
Reviewed by: Gordon Ross <gwr@nexenta.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/fs/zfs/zfs_ctldir.c
+++ new/usr/src/uts/common/fs/zfs/zfs_ctldir.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
|
↓ open down ↓ |
13 lines elided |
↑ open up ↑ |
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2012, 2014 by Delphix. All rights reserved.
24 + * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
24 25 */
25 26
26 27 /*
27 28 * ZFS control directory (a.k.a. ".zfs")
28 29 *
29 30 * This directory provides a common location for all ZFS meta-objects.
30 31 * Currently, this is only the 'snapshot' directory, but this may expand in the
31 32 * future. The elements are built using the GFS primitives, as the hierarchy
32 33 * does not actually exist on disk.
33 34 *
34 35 * For 'snapshot', we don't want to have all snapshots always mounted, because
35 36 * this would take up a huge amount of space in /etc/mnttab. We have three
36 37 * types of objects:
37 38 *
38 39 * ctldir ------> snapshotdir -------> snapshot
39 40 * |
40 41 * |
41 42 * V
42 43 * mounted fs
43 44 *
44 45 * The 'snapshot' node contains just enough information to lookup '..' and act
45 46 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
46 47 * perform an automount of the underlying filesystem and return the
47 48 * corresponding vnode.
48 49 *
49 50 * All mounts are handled automatically by the kernel, but unmounts are
50 51 * (currently) handled from user land. The main reason is that there is no
51 52 * reliable way to auto-unmount the filesystem when it's "no longer in use".
52 53 * When the user unmounts a filesystem, we call zfsctl_unmount(), which
53 54 * unmounts any snapshots within the snapshot directory.
54 55 *
55 56 * The '.zfs', '.zfs/snapshot', and all directories created under
56 57 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') are all GFS nodes and
57 58 * share the same vfs_t as the head filesystem (what '.zfs' lives under).
58 59 *
59 60 * File systems mounted ontop of the GFS nodes '.zfs/snapshot/<snapname>'
60 61 * (ie: snapshots) are ZFS nodes and have their own unique vfs_t.
61 62 * However, vnodes within these mounted on file systems have their v_vfsp
62 63 * fields set to the head filesystem to make NFS happy (see
63 64 * zfsctl_snapdir_lookup()). We VFS_HOLD the head filesystem's vfs_t
64 65 * so that it cannot be freed until all snapshots have been unmounted.
65 66 */
66 67
67 68 #include <fs/fs_subr.h>
68 69 #include <sys/zfs_ctldir.h>
69 70 #include <sys/zfs_ioctl.h>
70 71 #include <sys/zfs_vfsops.h>
71 72 #include <sys/vfs_opreg.h>
72 73 #include <sys/gfs.h>
73 74 #include <sys/stat.h>
74 75 #include <sys/dmu.h>
75 76 #include <sys/dsl_destroy.h>
76 77 #include <sys/dsl_deleg.h>
77 78 #include <sys/mount.h>
78 79 #include <sys/sunddi.h>
79 80
80 81 #include "zfs_namecheck.h"
81 82
82 83 typedef struct zfsctl_node {
83 84 gfs_dir_t zc_gfs_private;
84 85 uint64_t zc_id;
85 86 timestruc_t zc_cmtime; /* ctime and mtime, always the same */
86 87 } zfsctl_node_t;
87 88
88 89 typedef struct zfsctl_snapdir {
89 90 zfsctl_node_t sd_node;
90 91 kmutex_t sd_lock;
91 92 avl_tree_t sd_snaps;
92 93 } zfsctl_snapdir_t;
93 94
94 95 typedef struct {
95 96 char *se_name;
96 97 vnode_t *se_root;
97 98 avl_node_t se_node;
98 99 } zfs_snapentry_t;
99 100
100 101 static int
101 102 snapentry_compare(const void *a, const void *b)
102 103 {
103 104 const zfs_snapentry_t *sa = a;
104 105 const zfs_snapentry_t *sb = b;
105 106 int ret = strcmp(sa->se_name, sb->se_name);
106 107
107 108 if (ret < 0)
108 109 return (-1);
109 110 else if (ret > 0)
110 111 return (1);
111 112 else
112 113 return (0);
113 114 }
114 115
115 116 vnodeops_t *zfsctl_ops_root;
116 117 vnodeops_t *zfsctl_ops_snapdir;
117 118 vnodeops_t *zfsctl_ops_snapshot;
118 119 vnodeops_t *zfsctl_ops_shares;
119 120 vnodeops_t *zfsctl_ops_shares_dir;
120 121
121 122 static const fs_operation_def_t zfsctl_tops_root[];
122 123 static const fs_operation_def_t zfsctl_tops_snapdir[];
123 124 static const fs_operation_def_t zfsctl_tops_snapshot[];
124 125 static const fs_operation_def_t zfsctl_tops_shares[];
125 126
126 127 static vnode_t *zfsctl_mknode_snapdir(vnode_t *);
127 128 static vnode_t *zfsctl_mknode_shares(vnode_t *);
128 129 static vnode_t *zfsctl_snapshot_mknode(vnode_t *, uint64_t objset);
129 130 static int zfsctl_unmount_snap(zfs_snapentry_t *, int, cred_t *);
130 131
131 132 static gfs_opsvec_t zfsctl_opsvec[] = {
132 133 { ".zfs", zfsctl_tops_root, &zfsctl_ops_root },
133 134 { ".zfs/snapshot", zfsctl_tops_snapdir, &zfsctl_ops_snapdir },
134 135 { ".zfs/snapshot/vnode", zfsctl_tops_snapshot, &zfsctl_ops_snapshot },
135 136 { ".zfs/shares", zfsctl_tops_shares, &zfsctl_ops_shares_dir },
136 137 { ".zfs/shares/vnode", zfsctl_tops_shares, &zfsctl_ops_shares },
137 138 { NULL }
138 139 };
139 140
140 141 /*
141 142 * Root directory elements. We only have two entries
142 143 * snapshot and shares.
143 144 */
144 145 static gfs_dirent_t zfsctl_root_entries[] = {
145 146 { "snapshot", zfsctl_mknode_snapdir, GFS_CACHE_VNODE },
146 147 { "shares", zfsctl_mknode_shares, GFS_CACHE_VNODE },
147 148 { NULL }
148 149 };
149 150
150 151 /* include . and .. in the calculation */
151 152 #define NROOT_ENTRIES ((sizeof (zfsctl_root_entries) / \
152 153 sizeof (gfs_dirent_t)) + 1)
153 154
154 155
155 156 /*
156 157 * Initialize the various GFS pieces we'll need to create and manipulate .zfs
157 158 * directories. This is called from the ZFS init routine, and initializes the
158 159 * vnode ops vectors that we'll be using.
159 160 */
160 161 void
161 162 zfsctl_init(void)
162 163 {
163 164 VERIFY(gfs_make_opsvec(zfsctl_opsvec) == 0);
164 165 }
165 166
166 167 void
167 168 zfsctl_fini(void)
168 169 {
169 170 /*
170 171 * Remove vfsctl vnode ops
171 172 */
172 173 if (zfsctl_ops_root)
173 174 vn_freevnodeops(zfsctl_ops_root);
174 175 if (zfsctl_ops_snapdir)
175 176 vn_freevnodeops(zfsctl_ops_snapdir);
176 177 if (zfsctl_ops_snapshot)
177 178 vn_freevnodeops(zfsctl_ops_snapshot);
178 179 if (zfsctl_ops_shares)
179 180 vn_freevnodeops(zfsctl_ops_shares);
180 181 if (zfsctl_ops_shares_dir)
181 182 vn_freevnodeops(zfsctl_ops_shares_dir);
182 183
183 184 zfsctl_ops_root = NULL;
184 185 zfsctl_ops_snapdir = NULL;
185 186 zfsctl_ops_snapshot = NULL;
186 187 zfsctl_ops_shares = NULL;
187 188 zfsctl_ops_shares_dir = NULL;
188 189 }
189 190
190 191 boolean_t
191 192 zfsctl_is_node(vnode_t *vp)
192 193 {
193 194 return (vn_matchops(vp, zfsctl_ops_root) ||
194 195 vn_matchops(vp, zfsctl_ops_snapdir) ||
195 196 vn_matchops(vp, zfsctl_ops_snapshot) ||
196 197 vn_matchops(vp, zfsctl_ops_shares) ||
197 198 vn_matchops(vp, zfsctl_ops_shares_dir));
198 199
199 200 }
200 201
201 202 /*
202 203 * Return the inode number associated with the 'snapshot' or
203 204 * 'shares' directory.
204 205 */
205 206 /* ARGSUSED */
206 207 static ino64_t
207 208 zfsctl_root_inode_cb(vnode_t *vp, int index)
208 209 {
209 210 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
210 211
211 212 ASSERT(index <= 2);
212 213
213 214 if (index == 0)
214 215 return (ZFSCTL_INO_SNAPDIR);
215 216
216 217 return (zfsvfs->z_shares_dir);
217 218 }
218 219
219 220 /*
220 221 * Create the '.zfs' directory. This directory is cached as part of the VFS
221 222 * structure. This results in a hold on the vfs_t. The code in zfs_umount()
222 223 * therefore checks against a vfs_count of 2 instead of 1. This reference
223 224 * is removed when the ctldir is destroyed in the unmount.
224 225 */
225 226 void
226 227 zfsctl_create(zfsvfs_t *zfsvfs)
227 228 {
228 229 vnode_t *vp, *rvp;
229 230 zfsctl_node_t *zcp;
230 231 uint64_t crtime[2];
231 232
232 233 ASSERT(zfsvfs->z_ctldir == NULL);
233 234
234 235 vp = gfs_root_create(sizeof (zfsctl_node_t), zfsvfs->z_vfs,
235 236 zfsctl_ops_root, ZFSCTL_INO_ROOT, zfsctl_root_entries,
236 237 zfsctl_root_inode_cb, MAXNAMELEN, NULL, NULL);
237 238 zcp = vp->v_data;
238 239 zcp->zc_id = ZFSCTL_INO_ROOT;
239 240
240 241 VERIFY(VFS_ROOT(zfsvfs->z_vfs, &rvp) == 0);
241 242 VERIFY(0 == sa_lookup(VTOZ(rvp)->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
242 243 &crtime, sizeof (crtime)));
243 244 ZFS_TIME_DECODE(&zcp->zc_cmtime, crtime);
244 245 VN_RELE(rvp);
245 246
246 247 /*
247 248 * We're only faking the fact that we have a root of a filesystem for
248 249 * the sake of the GFS interfaces. Undo the flag manipulation it did
249 250 * for us.
250 251 */
251 252 vp->v_flag &= ~(VROOT | VNOCACHE | VNOMAP | VNOSWAP | VNOMOUNT);
252 253
253 254 zfsvfs->z_ctldir = vp;
254 255 }
255 256
256 257 /*
257 258 * Destroy the '.zfs' directory. Only called when the filesystem is unmounted.
258 259 * There might still be more references if we were force unmounted, but only
259 260 * new zfs_inactive() calls can occur and they don't reference .zfs
260 261 */
261 262 void
262 263 zfsctl_destroy(zfsvfs_t *zfsvfs)
263 264 {
264 265 VN_RELE(zfsvfs->z_ctldir);
265 266 zfsvfs->z_ctldir = NULL;
266 267 }
267 268
268 269 /*
269 270 * Given a root znode, retrieve the associated .zfs directory.
270 271 * Add a hold to the vnode and return it.
271 272 */
272 273 vnode_t *
273 274 zfsctl_root(znode_t *zp)
274 275 {
275 276 ASSERT(zfs_has_ctldir(zp));
276 277 VN_HOLD(zp->z_zfsvfs->z_ctldir);
277 278 return (zp->z_zfsvfs->z_ctldir);
278 279 }
279 280
280 281 /*
281 282 * Common open routine. Disallow any write access.
282 283 */
283 284 /* ARGSUSED */
284 285 static int
285 286 zfsctl_common_open(vnode_t **vpp, int flags, cred_t *cr, caller_context_t *ct)
286 287 {
287 288 if (flags & FWRITE)
288 289 return (SET_ERROR(EACCES));
289 290
290 291 return (0);
291 292 }
292 293
293 294 /*
294 295 * Common close routine. Nothing to do here.
295 296 */
296 297 /* ARGSUSED */
297 298 static int
298 299 zfsctl_common_close(vnode_t *vpp, int flags, int count, offset_t off,
299 300 cred_t *cr, caller_context_t *ct)
300 301 {
301 302 return (0);
302 303 }
303 304
304 305 /*
305 306 * Common access routine. Disallow writes.
306 307 */
307 308 /* ARGSUSED */
308 309 static int
309 310 zfsctl_common_access(vnode_t *vp, int mode, int flags, cred_t *cr,
310 311 caller_context_t *ct)
311 312 {
312 313 if (flags & V_ACE_MASK) {
313 314 if (mode & ACE_ALL_WRITE_PERMS)
314 315 return (SET_ERROR(EACCES));
315 316 } else {
316 317 if (mode & VWRITE)
317 318 return (SET_ERROR(EACCES));
318 319 }
319 320
320 321 return (0);
321 322 }
322 323
323 324 /*
324 325 * Common getattr function. Fill in basic information.
325 326 */
326 327 static void
327 328 zfsctl_common_getattr(vnode_t *vp, vattr_t *vap)
328 329 {
329 330 timestruc_t now;
330 331
331 332 vap->va_uid = 0;
332 333 vap->va_gid = 0;
333 334 vap->va_rdev = 0;
334 335 /*
335 336 * We are a purely virtual object, so we have no
336 337 * blocksize or allocated blocks.
337 338 */
338 339 vap->va_blksize = 0;
339 340 vap->va_nblocks = 0;
340 341 vap->va_seq = 0;
341 342 vap->va_fsid = vp->v_vfsp->vfs_dev;
342 343 vap->va_mode = S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP |
343 344 S_IROTH | S_IXOTH;
344 345 vap->va_type = VDIR;
345 346 /*
346 347 * We live in the now (for atime).
347 348 */
348 349 gethrestime(&now);
349 350 vap->va_atime = now;
350 351 }
351 352
352 353 /*ARGSUSED*/
353 354 static int
354 355 zfsctl_common_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
355 356 {
356 357 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
357 358 zfsctl_node_t *zcp = vp->v_data;
358 359 uint64_t object = zcp->zc_id;
359 360 zfid_short_t *zfid;
360 361 int i;
361 362
362 363 ZFS_ENTER(zfsvfs);
363 364
364 365 if (fidp->fid_len < SHORT_FID_LEN) {
365 366 fidp->fid_len = SHORT_FID_LEN;
366 367 ZFS_EXIT(zfsvfs);
367 368 return (SET_ERROR(ENOSPC));
368 369 }
369 370
370 371 zfid = (zfid_short_t *)fidp;
371 372
372 373 zfid->zf_len = SHORT_FID_LEN;
373 374
374 375 for (i = 0; i < sizeof (zfid->zf_object); i++)
375 376 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
376 377
377 378 /* .zfs znodes always have a generation number of 0 */
378 379 for (i = 0; i < sizeof (zfid->zf_gen); i++)
379 380 zfid->zf_gen[i] = 0;
380 381
381 382 ZFS_EXIT(zfsvfs);
382 383 return (0);
383 384 }
384 385
385 386
386 387 /*ARGSUSED*/
387 388 static int
388 389 zfsctl_shares_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
389 390 {
390 391 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
391 392 znode_t *dzp;
392 393 int error;
393 394
394 395 ZFS_ENTER(zfsvfs);
395 396
396 397 if (zfsvfs->z_shares_dir == 0) {
397 398 ZFS_EXIT(zfsvfs);
398 399 return (SET_ERROR(ENOTSUP));
399 400 }
400 401
401 402 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
402 403 error = VOP_FID(ZTOV(dzp), fidp, ct);
403 404 VN_RELE(ZTOV(dzp));
404 405 }
405 406
406 407 ZFS_EXIT(zfsvfs);
407 408 return (error);
408 409 }
409 410 /*
410 411 * .zfs inode namespace
411 412 *
412 413 * We need to generate unique inode numbers for all files and directories
413 414 * within the .zfs pseudo-filesystem. We use the following scheme:
414 415 *
415 416 * ENTRY ZFSCTL_INODE
416 417 * .zfs 1
417 418 * .zfs/snapshot 2
418 419 * .zfs/snapshot/<snap> objectid(snap)
419 420 */
420 421
421 422 #define ZFSCTL_INO_SNAP(id) (id)
422 423
423 424 /*
424 425 * Get root directory attributes.
425 426 */
426 427 /* ARGSUSED */
427 428 static int
428 429 zfsctl_root_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
429 430 caller_context_t *ct)
430 431 {
431 432 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
432 433 zfsctl_node_t *zcp = vp->v_data;
433 434
434 435 ZFS_ENTER(zfsvfs);
435 436 vap->va_nodeid = ZFSCTL_INO_ROOT;
436 437 vap->va_nlink = vap->va_size = NROOT_ENTRIES;
437 438 vap->va_mtime = vap->va_ctime = zcp->zc_cmtime;
438 439
439 440 zfsctl_common_getattr(vp, vap);
440 441 ZFS_EXIT(zfsvfs);
441 442
442 443 return (0);
443 444 }
444 445
445 446 /*
446 447 * Special case the handling of "..".
447 448 */
448 449 /* ARGSUSED */
449 450 int
450 451 zfsctl_root_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
451 452 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
452 453 int *direntflags, pathname_t *realpnp)
453 454 {
454 455 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
455 456 int err;
456 457
457 458 /*
458 459 * No extended attributes allowed under .zfs
459 460 */
460 461 if (flags & LOOKUP_XATTR)
461 462 return (SET_ERROR(EINVAL));
462 463
463 464 ZFS_ENTER(zfsvfs);
464 465
465 466 if (strcmp(nm, "..") == 0) {
466 467 err = VFS_ROOT(dvp->v_vfsp, vpp);
467 468 } else {
468 469 err = gfs_vop_lookup(dvp, nm, vpp, pnp, flags, rdir,
469 470 cr, ct, direntflags, realpnp);
470 471 }
471 472
472 473 ZFS_EXIT(zfsvfs);
473 474
474 475 return (err);
475 476 }
476 477
477 478 static int
478 479 zfsctl_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
479 480 caller_context_t *ct)
480 481 {
481 482 /*
482 483 * We only care about ACL_ENABLED so that libsec can
483 484 * display ACL correctly and not default to POSIX draft.
484 485 */
485 486 if (cmd == _PC_ACL_ENABLED) {
486 487 *valp = _ACL_ACE_ENABLED;
487 488 return (0);
488 489 }
489 490
490 491 return (fs_pathconf(vp, cmd, valp, cr, ct));
491 492 }
492 493
493 494 static const fs_operation_def_t zfsctl_tops_root[] = {
494 495 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
495 496 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
496 497 { VOPNAME_IOCTL, { .error = fs_inval } },
497 498 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_root_getattr } },
498 499 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
499 500 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
500 501 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_root_lookup } },
501 502 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
502 503 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
503 504 { VOPNAME_PATHCONF, { .vop_pathconf = zfsctl_pathconf } },
504 505 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
505 506 { NULL }
506 507 };
507 508
508 509 /*
509 510 * Gets the full dataset name that corresponds to the given snapshot name
510 511 * Example:
511 512 * zfsctl_snapshot_zname("snap1") -> "mypool/myfs@snap1"
512 513 */
513 514 static int
514 515 zfsctl_snapshot_zname(vnode_t *vp, const char *name, int len, char *zname)
515 516 {
516 517 objset_t *os = ((zfsvfs_t *)((vp)->v_vfsp->vfs_data))->z_os;
517 518
518 519 if (zfs_component_namecheck(name, NULL, NULL) != 0)
519 520 return (SET_ERROR(EILSEQ));
520 521 dmu_objset_name(os, zname);
521 522 if (strlen(zname) + 1 + strlen(name) >= len)
522 523 return (SET_ERROR(ENAMETOOLONG));
523 524 (void) strcat(zname, "@");
524 525 (void) strcat(zname, name);
525 526 return (0);
526 527 }
527 528
528 529 static int
529 530 zfsctl_unmount_snap(zfs_snapentry_t *sep, int fflags, cred_t *cr)
530 531 {
531 532 vnode_t *svp = sep->se_root;
532 533 int error;
533 534
534 535 ASSERT(vn_ismntpt(svp));
535 536
536 537 /* this will be dropped by dounmount() */
537 538 if ((error = vn_vfswlock(svp)) != 0)
538 539 return (error);
539 540
540 541 VN_HOLD(svp);
541 542 error = dounmount(vn_mountedvfs(svp), fflags, cr);
542 543 if (error) {
543 544 VN_RELE(svp);
544 545 return (error);
545 546 }
546 547
547 548 /*
548 549 * We can't use VN_RELE(), as that will try to invoke
549 550 * zfsctl_snapdir_inactive(), which would cause us to destroy
550 551 * the sd_lock mutex held by our caller.
551 552 */
552 553 ASSERT(svp->v_count == 1);
553 554 gfs_vop_inactive(svp, cr, NULL);
554 555
555 556 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
556 557 kmem_free(sep, sizeof (zfs_snapentry_t));
557 558
558 559 return (0);
559 560 }
560 561
561 562 static void
562 563 zfsctl_rename_snap(zfsctl_snapdir_t *sdp, zfs_snapentry_t *sep, const char *nm)
563 564 {
564 565 avl_index_t where;
565 566 vfs_t *vfsp;
566 567 refstr_t *pathref;
567 568 char newpath[MAXNAMELEN];
568 569 char *tail;
569 570
570 571 ASSERT(MUTEX_HELD(&sdp->sd_lock));
571 572 ASSERT(sep != NULL);
572 573
573 574 vfsp = vn_mountedvfs(sep->se_root);
574 575 ASSERT(vfsp != NULL);
575 576
576 577 vfs_lock_wait(vfsp);
577 578
578 579 /*
579 580 * Change the name in the AVL tree.
580 581 */
581 582 avl_remove(&sdp->sd_snaps, sep);
582 583 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
583 584 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
584 585 (void) strcpy(sep->se_name, nm);
585 586 VERIFY(avl_find(&sdp->sd_snaps, sep, &where) == NULL);
586 587 avl_insert(&sdp->sd_snaps, sep, where);
587 588
588 589 /*
589 590 * Change the current mountpoint info:
590 591 * - update the tail of the mntpoint path
591 592 * - update the tail of the resource path
592 593 */
593 594 pathref = vfs_getmntpoint(vfsp);
594 595 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
595 596 VERIFY((tail = strrchr(newpath, '/')) != NULL);
596 597 *(tail+1) = '\0';
597 598 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
598 599 (void) strcat(newpath, nm);
599 600 refstr_rele(pathref);
600 601 vfs_setmntpoint(vfsp, newpath, 0);
601 602
602 603 pathref = vfs_getresource(vfsp);
603 604 (void) strncpy(newpath, refstr_value(pathref), sizeof (newpath));
604 605 VERIFY((tail = strrchr(newpath, '@')) != NULL);
605 606 *(tail+1) = '\0';
606 607 ASSERT3U(strlen(newpath) + strlen(nm), <, sizeof (newpath));
607 608 (void) strcat(newpath, nm);
608 609 refstr_rele(pathref);
609 610 vfs_setresource(vfsp, newpath, 0);
610 611
611 612 vfs_unlock(vfsp);
612 613 }
613 614
614 615 /*ARGSUSED*/
615 616 static int
616 617 zfsctl_snapdir_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
617 618 cred_t *cr, caller_context_t *ct, int flags)
618 619 {
619 620 zfsctl_snapdir_t *sdp = sdvp->v_data;
620 621 zfs_snapentry_t search, *sep;
621 622 zfsvfs_t *zfsvfs;
622 623 avl_index_t where;
623 624 char from[MAXNAMELEN], to[MAXNAMELEN];
624 625 char real[MAXNAMELEN], fsname[MAXNAMELEN];
625 626 int err;
626 627
627 628 zfsvfs = sdvp->v_vfsp->vfs_data;
628 629 ZFS_ENTER(zfsvfs);
629 630
630 631 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
631 632 err = dmu_snapshot_realname(zfsvfs->z_os, snm, real,
632 633 MAXNAMELEN, NULL);
633 634 if (err == 0) {
634 635 snm = real;
635 636 } else if (err != ENOTSUP) {
636 637 ZFS_EXIT(zfsvfs);
637 638 return (err);
638 639 }
639 640 }
640 641
641 642 ZFS_EXIT(zfsvfs);
642 643
643 644 dmu_objset_name(zfsvfs->z_os, fsname);
644 645
645 646 err = zfsctl_snapshot_zname(sdvp, snm, MAXNAMELEN, from);
646 647 if (err == 0)
647 648 err = zfsctl_snapshot_zname(tdvp, tnm, MAXNAMELEN, to);
648 649 if (err == 0)
649 650 err = zfs_secpolicy_rename_perms(from, to, cr);
650 651 if (err != 0)
651 652 return (err);
652 653
653 654 /*
654 655 * Cannot move snapshots out of the snapdir.
655 656 */
656 657 if (sdvp != tdvp)
657 658 return (SET_ERROR(EINVAL));
658 659
659 660 if (strcmp(snm, tnm) == 0)
660 661 return (0);
661 662
662 663 mutex_enter(&sdp->sd_lock);
663 664
664 665 search.se_name = (char *)snm;
665 666 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) == NULL) {
666 667 mutex_exit(&sdp->sd_lock);
667 668 return (SET_ERROR(ENOENT));
668 669 }
669 670
670 671 err = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
671 672 if (err == 0)
672 673 zfsctl_rename_snap(sdp, sep, tnm);
673 674
674 675 mutex_exit(&sdp->sd_lock);
675 676
676 677 return (err);
677 678 }
678 679
679 680 /* ARGSUSED */
680 681 static int
681 682 zfsctl_snapdir_remove(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
682 683 caller_context_t *ct, int flags)
683 684 {
684 685 zfsctl_snapdir_t *sdp = dvp->v_data;
685 686 zfs_snapentry_t *sep;
686 687 zfs_snapentry_t search;
687 688 zfsvfs_t *zfsvfs;
688 689 char snapname[MAXNAMELEN];
689 690 char real[MAXNAMELEN];
690 691 int err;
691 692
692 693 zfsvfs = dvp->v_vfsp->vfs_data;
693 694 ZFS_ENTER(zfsvfs);
694 695
695 696 if ((flags & FIGNORECASE) || zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
696 697
697 698 err = dmu_snapshot_realname(zfsvfs->z_os, name, real,
698 699 MAXNAMELEN, NULL);
699 700 if (err == 0) {
700 701 name = real;
701 702 } else if (err != ENOTSUP) {
702 703 ZFS_EXIT(zfsvfs);
703 704 return (err);
704 705 }
705 706 }
706 707
707 708 ZFS_EXIT(zfsvfs);
708 709
709 710 err = zfsctl_snapshot_zname(dvp, name, MAXNAMELEN, snapname);
710 711 if (err == 0)
711 712 err = zfs_secpolicy_destroy_perms(snapname, cr);
712 713 if (err != 0)
713 714 return (err);
714 715
715 716 mutex_enter(&sdp->sd_lock);
716 717
717 718 search.se_name = name;
718 719 sep = avl_find(&sdp->sd_snaps, &search, NULL);
719 720 if (sep) {
720 721 avl_remove(&sdp->sd_snaps, sep);
721 722 err = zfsctl_unmount_snap(sep, MS_FORCE, cr);
722 723 if (err != 0)
723 724 avl_add(&sdp->sd_snaps, sep);
724 725 else
725 726 err = dsl_destroy_snapshot(snapname, B_FALSE);
726 727 } else {
727 728 err = SET_ERROR(ENOENT);
728 729 }
729 730
730 731 mutex_exit(&sdp->sd_lock);
731 732
732 733 return (err);
733 734 }
734 735
735 736 /*
736 737 * This creates a snapshot under '.zfs/snapshot'.
737 738 */
738 739 /* ARGSUSED */
739 740 static int
740 741 zfsctl_snapdir_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
741 742 cred_t *cr, caller_context_t *cc, int flags, vsecattr_t *vsecp)
742 743 {
743 744 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
744 745 char name[MAXNAMELEN];
745 746 int err;
746 747 static enum symfollow follow = NO_FOLLOW;
747 748 static enum uio_seg seg = UIO_SYSSPACE;
748 749
749 750 if (zfs_component_namecheck(dirname, NULL, NULL) != 0)
750 751 return (SET_ERROR(EILSEQ));
751 752
752 753 dmu_objset_name(zfsvfs->z_os, name);
753 754
754 755 *vpp = NULL;
755 756
756 757 err = zfs_secpolicy_snapshot_perms(name, cr);
757 758 if (err != 0)
758 759 return (err);
759 760
760 761 if (err == 0) {
761 762 err = dmu_objset_snapshot_one(name, dirname);
762 763 if (err != 0)
763 764 return (err);
764 765 err = lookupnameat(dirname, seg, follow, NULL, vpp, dvp);
765 766 }
766 767
767 768 return (err);
768 769 }
769 770
770 771 /*
771 772 * Lookup entry point for the 'snapshot' directory. Try to open the
772 773 * snapshot if it exist, creating the pseudo filesystem vnode as necessary.
773 774 * Perform a mount of the associated dataset on top of the vnode.
774 775 */
775 776 /* ARGSUSED */
776 777 static int
777 778 zfsctl_snapdir_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
778 779 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
779 780 int *direntflags, pathname_t *realpnp)
780 781 {
781 782 zfsctl_snapdir_t *sdp = dvp->v_data;
782 783 objset_t *snap;
783 784 char snapname[MAXNAMELEN];
784 785 char real[MAXNAMELEN];
785 786 char *mountpoint;
786 787 zfs_snapentry_t *sep, search;
787 788 struct mounta margs;
788 789 vfs_t *vfsp;
789 790 size_t mountpoint_len;
790 791 avl_index_t where;
791 792 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
792 793 int err;
793 794
794 795 /*
795 796 * No extended attributes allowed under .zfs
796 797 */
797 798 if (flags & LOOKUP_XATTR)
798 799 return (SET_ERROR(EINVAL));
799 800
800 801 ASSERT(dvp->v_type == VDIR);
801 802
802 803 /*
803 804 * If we get a recursive call, that means we got called
804 805 * from the domount() code while it was trying to look up the
805 806 * spec (which looks like a local path for zfs). We need to
806 807 * add some flag to domount() to tell it not to do this lookup.
807 808 */
808 809 if (MUTEX_HELD(&sdp->sd_lock))
809 810 return (SET_ERROR(ENOENT));
810 811
811 812 ZFS_ENTER(zfsvfs);
812 813
813 814 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
814 815 ZFS_EXIT(zfsvfs);
815 816 return (0);
816 817 }
817 818
818 819 if (flags & FIGNORECASE) {
819 820 boolean_t conflict = B_FALSE;
820 821
821 822 err = dmu_snapshot_realname(zfsvfs->z_os, nm, real,
822 823 MAXNAMELEN, &conflict);
823 824 if (err == 0) {
824 825 nm = real;
825 826 } else if (err != ENOTSUP) {
826 827 ZFS_EXIT(zfsvfs);
827 828 return (err);
828 829 }
829 830 if (realpnp)
830 831 (void) strlcpy(realpnp->pn_buf, nm,
831 832 realpnp->pn_bufsize);
832 833 if (conflict && direntflags)
833 834 *direntflags = ED_CASE_CONFLICT;
834 835 }
835 836
836 837 mutex_enter(&sdp->sd_lock);
837 838 search.se_name = (char *)nm;
838 839 if ((sep = avl_find(&sdp->sd_snaps, &search, &where)) != NULL) {
839 840 *vpp = sep->se_root;
840 841 VN_HOLD(*vpp);
841 842 err = traverse(vpp);
842 843 if (err != 0) {
843 844 VN_RELE(*vpp);
844 845 *vpp = NULL;
845 846 } else if (*vpp == sep->se_root) {
846 847 /*
847 848 * The snapshot was unmounted behind our backs,
848 849 * try to remount it.
849 850 */
850 851 goto domount;
851 852 } else {
852 853 /*
853 854 * VROOT was set during the traverse call. We need
854 855 * to clear it since we're pretending to be part
855 856 * of our parent's vfs.
856 857 */
857 858 (*vpp)->v_flag &= ~VROOT;
858 859 }
859 860 mutex_exit(&sdp->sd_lock);
860 861 ZFS_EXIT(zfsvfs);
861 862 return (err);
862 863 }
863 864
864 865 /*
865 866 * The requested snapshot is not currently mounted, look it up.
866 867 */
867 868 err = zfsctl_snapshot_zname(dvp, nm, MAXNAMELEN, snapname);
868 869 if (err != 0) {
869 870 mutex_exit(&sdp->sd_lock);
870 871 ZFS_EXIT(zfsvfs);
871 872 /*
872 873 * handle "ls *" or "?" in a graceful manner,
873 874 * forcing EILSEQ to ENOENT.
874 875 * Since shell ultimately passes "*" or "?" as name to lookup
875 876 */
876 877 return (err == EILSEQ ? ENOENT : err);
877 878 }
878 879 if (dmu_objset_hold(snapname, FTAG, &snap) != 0) {
879 880 mutex_exit(&sdp->sd_lock);
880 881 ZFS_EXIT(zfsvfs);
881 882 return (SET_ERROR(ENOENT));
882 883 }
883 884
884 885 sep = kmem_alloc(sizeof (zfs_snapentry_t), KM_SLEEP);
885 886 sep->se_name = kmem_alloc(strlen(nm) + 1, KM_SLEEP);
886 887 (void) strcpy(sep->se_name, nm);
887 888 *vpp = sep->se_root = zfsctl_snapshot_mknode(dvp, dmu_objset_id(snap));
888 889 avl_insert(&sdp->sd_snaps, sep, where);
889 890
890 891 dmu_objset_rele(snap, FTAG);
891 892 domount:
892 893 mountpoint_len = strlen(refstr_value(dvp->v_vfsp->vfs_mntpt)) +
893 894 strlen("/.zfs/snapshot/") + strlen(nm) + 1;
894 895 mountpoint = kmem_alloc(mountpoint_len, KM_SLEEP);
895 896 (void) snprintf(mountpoint, mountpoint_len, "%s/.zfs/snapshot/%s",
896 897 refstr_value(dvp->v_vfsp->vfs_mntpt), nm);
897 898
898 899 margs.spec = snapname;
899 900 margs.dir = mountpoint;
900 901 margs.flags = MS_SYSSPACE | MS_NOMNTTAB;
901 902 margs.fstype = "zfs";
902 903 margs.dataptr = NULL;
903 904 margs.datalen = 0;
904 905 margs.optptr = NULL;
905 906 margs.optlen = 0;
906 907
907 908 err = domount("zfs", &margs, *vpp, kcred, &vfsp);
908 909 kmem_free(mountpoint, mountpoint_len);
909 910
910 911 if (err == 0) {
911 912 /*
912 913 * Return the mounted root rather than the covered mount point.
913 914 * Takes the GFS vnode at .zfs/snapshot/<snapname> and returns
914 915 * the ZFS vnode mounted on top of the GFS node. This ZFS
915 916 * vnode is the root of the newly created vfsp.
916 917 */
917 918 VFS_RELE(vfsp);
918 919 err = traverse(vpp);
919 920 }
920 921
921 922 if (err == 0) {
922 923 /*
923 924 * Fix up the root vnode mounted on .zfs/snapshot/<snapname>.
924 925 *
925 926 * This is where we lie about our v_vfsp in order to
926 927 * make .zfs/snapshot/<snapname> accessible over NFS
927 928 * without requiring manual mounts of <snapname>.
928 929 */
929 930 ASSERT(VTOZ(*vpp)->z_zfsvfs != zfsvfs);
930 931 VTOZ(*vpp)->z_zfsvfs->z_parent = zfsvfs;
931 932 (*vpp)->v_vfsp = zfsvfs->z_vfs;
932 933 (*vpp)->v_flag &= ~VROOT;
933 934 }
934 935 mutex_exit(&sdp->sd_lock);
935 936 ZFS_EXIT(zfsvfs);
936 937
937 938 /*
938 939 * If we had an error, drop our hold on the vnode and
939 940 * zfsctl_snapshot_inactive() will clean up.
940 941 */
941 942 if (err != 0) {
942 943 VN_RELE(*vpp);
943 944 *vpp = NULL;
944 945 }
945 946 return (err);
946 947 }
947 948
948 949 /* ARGSUSED */
949 950 static int
950 951 zfsctl_shares_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, pathname_t *pnp,
951 952 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
952 953 int *direntflags, pathname_t *realpnp)
953 954 {
954 955 zfsvfs_t *zfsvfs = dvp->v_vfsp->vfs_data;
955 956 znode_t *dzp;
956 957 int error;
957 958
958 959 ZFS_ENTER(zfsvfs);
|
↓ open down ↓ |
925 lines elided |
↑ open up ↑ |
959 960
960 961 if (gfs_lookup_dot(vpp, dvp, zfsvfs->z_ctldir, nm) == 0) {
961 962 ZFS_EXIT(zfsvfs);
962 963 return (0);
963 964 }
964 965
965 966 if (zfsvfs->z_shares_dir == 0) {
966 967 ZFS_EXIT(zfsvfs);
967 968 return (SET_ERROR(ENOTSUP));
968 969 }
969 - if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0)
970 + if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
970 971 error = VOP_LOOKUP(ZTOV(dzp), nm, vpp, pnp,
971 972 flags, rdir, cr, ct, direntflags, realpnp);
973 + VN_RELE(ZTOV(dzp));
974 + }
972 975
973 - VN_RELE(ZTOV(dzp));
974 976 ZFS_EXIT(zfsvfs);
975 977
976 978 return (error);
977 979 }
978 980
979 981 /* ARGSUSED */
980 982 static int
981 983 zfsctl_snapdir_readdir_cb(vnode_t *vp, void *dp, int *eofp,
982 984 offset_t *offp, offset_t *nextp, void *data, int flags)
983 985 {
984 986 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
985 987 char snapname[MAXNAMELEN];
986 988 uint64_t id, cookie;
987 989 boolean_t case_conflict;
988 990 int error;
989 991
990 992 ZFS_ENTER(zfsvfs);
991 993
992 994 cookie = *offp;
993 995 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
994 996 error = dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN, snapname, &id,
995 997 &cookie, &case_conflict);
996 998 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
997 999 if (error) {
998 1000 ZFS_EXIT(zfsvfs);
999 1001 if (error == ENOENT) {
1000 1002 *eofp = 1;
1001 1003 return (0);
1002 1004 }
1003 1005 return (error);
1004 1006 }
1005 1007
1006 1008 if (flags & V_RDDIR_ENTFLAGS) {
1007 1009 edirent_t *eodp = dp;
1008 1010
1009 1011 (void) strcpy(eodp->ed_name, snapname);
1010 1012 eodp->ed_ino = ZFSCTL_INO_SNAP(id);
1011 1013 eodp->ed_eflags = case_conflict ? ED_CASE_CONFLICT : 0;
1012 1014 } else {
1013 1015 struct dirent64 *odp = dp;
1014 1016
1015 1017 (void) strcpy(odp->d_name, snapname);
1016 1018 odp->d_ino = ZFSCTL_INO_SNAP(id);
1017 1019 }
1018 1020 *nextp = cookie;
1019 1021
1020 1022 ZFS_EXIT(zfsvfs);
1021 1023
1022 1024 return (0);
1023 1025 }
1024 1026
1025 1027 /* ARGSUSED */
1026 1028 static int
1027 1029 zfsctl_shares_readdir(vnode_t *vp, uio_t *uiop, cred_t *cr, int *eofp,
1028 1030 caller_context_t *ct, int flags)
1029 1031 {
1030 1032 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1031 1033 znode_t *dzp;
1032 1034 int error;
1033 1035
1034 1036 ZFS_ENTER(zfsvfs);
1035 1037
1036 1038 if (zfsvfs->z_shares_dir == 0) {
1037 1039 ZFS_EXIT(zfsvfs);
1038 1040 return (SET_ERROR(ENOTSUP));
1039 1041 }
1040 1042 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1041 1043 error = VOP_READDIR(ZTOV(dzp), uiop, cr, eofp, ct, flags);
1042 1044 VN_RELE(ZTOV(dzp));
1043 1045 } else {
1044 1046 *eofp = 1;
1045 1047 error = SET_ERROR(ENOENT);
1046 1048 }
1047 1049
1048 1050 ZFS_EXIT(zfsvfs);
1049 1051 return (error);
1050 1052 }
1051 1053
1052 1054 /*
1053 1055 * pvp is the '.zfs' directory (zfsctl_node_t).
1054 1056 *
1055 1057 * Creates vp, which is '.zfs/snapshot' (zfsctl_snapdir_t).
1056 1058 *
1057 1059 * This function is the callback to create a GFS vnode for '.zfs/snapshot'
1058 1060 * when a lookup is performed on .zfs for "snapshot".
1059 1061 */
1060 1062 vnode_t *
1061 1063 zfsctl_mknode_snapdir(vnode_t *pvp)
1062 1064 {
1063 1065 vnode_t *vp;
1064 1066 zfsctl_snapdir_t *sdp;
1065 1067
1066 1068 vp = gfs_dir_create(sizeof (zfsctl_snapdir_t), pvp,
1067 1069 zfsctl_ops_snapdir, NULL, NULL, MAXNAMELEN,
1068 1070 zfsctl_snapdir_readdir_cb, NULL);
1069 1071 sdp = vp->v_data;
1070 1072 sdp->sd_node.zc_id = ZFSCTL_INO_SNAPDIR;
1071 1073 sdp->sd_node.zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1072 1074 mutex_init(&sdp->sd_lock, NULL, MUTEX_DEFAULT, NULL);
1073 1075 avl_create(&sdp->sd_snaps, snapentry_compare,
1074 1076 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, se_node));
1075 1077 return (vp);
1076 1078 }
1077 1079
1078 1080 vnode_t *
1079 1081 zfsctl_mknode_shares(vnode_t *pvp)
1080 1082 {
1081 1083 vnode_t *vp;
1082 1084 zfsctl_node_t *sdp;
1083 1085
1084 1086 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1085 1087 zfsctl_ops_shares, NULL, NULL, MAXNAMELEN,
1086 1088 NULL, NULL);
1087 1089 sdp = vp->v_data;
1088 1090 sdp->zc_cmtime = ((zfsctl_node_t *)pvp->v_data)->zc_cmtime;
1089 1091 return (vp);
1090 1092
1091 1093 }
1092 1094
1093 1095 /* ARGSUSED */
1094 1096 static int
1095 1097 zfsctl_shares_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1096 1098 caller_context_t *ct)
1097 1099 {
1098 1100 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1099 1101 znode_t *dzp;
1100 1102 int error;
1101 1103
1102 1104 ZFS_ENTER(zfsvfs);
1103 1105 if (zfsvfs->z_shares_dir == 0) {
1104 1106 ZFS_EXIT(zfsvfs);
1105 1107 return (SET_ERROR(ENOTSUP));
1106 1108 }
1107 1109 if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp)) == 0) {
1108 1110 error = VOP_GETATTR(ZTOV(dzp), vap, flags, cr, ct);
1109 1111 VN_RELE(ZTOV(dzp));
1110 1112 }
1111 1113 ZFS_EXIT(zfsvfs);
1112 1114 return (error);
1113 1115
1114 1116
1115 1117 }
1116 1118
1117 1119 /* ARGSUSED */
1118 1120 static int
1119 1121 zfsctl_snapdir_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
1120 1122 caller_context_t *ct)
1121 1123 {
1122 1124 zfsvfs_t *zfsvfs = vp->v_vfsp->vfs_data;
1123 1125 zfsctl_snapdir_t *sdp = vp->v_data;
1124 1126
1125 1127 ZFS_ENTER(zfsvfs);
1126 1128 zfsctl_common_getattr(vp, vap);
1127 1129 vap->va_nodeid = gfs_file_inode(vp);
1128 1130 vap->va_nlink = vap->va_size = avl_numnodes(&sdp->sd_snaps) + 2;
1129 1131 vap->va_ctime = vap->va_mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
1130 1132 ZFS_EXIT(zfsvfs);
1131 1133
1132 1134 return (0);
1133 1135 }
1134 1136
1135 1137 /* ARGSUSED */
1136 1138 static void
1137 1139 zfsctl_snapdir_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1138 1140 {
1139 1141 zfsctl_snapdir_t *sdp = vp->v_data;
1140 1142 void *private;
1141 1143
1142 1144 private = gfs_dir_inactive(vp);
1143 1145 if (private != NULL) {
1144 1146 ASSERT(avl_numnodes(&sdp->sd_snaps) == 0);
1145 1147 mutex_destroy(&sdp->sd_lock);
1146 1148 avl_destroy(&sdp->sd_snaps);
1147 1149 kmem_free(private, sizeof (zfsctl_snapdir_t));
1148 1150 }
1149 1151 }
1150 1152
1151 1153 static const fs_operation_def_t zfsctl_tops_snapdir[] = {
1152 1154 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1153 1155 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1154 1156 { VOPNAME_IOCTL, { .error = fs_inval } },
1155 1157 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_snapdir_getattr } },
1156 1158 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1157 1159 { VOPNAME_RENAME, { .vop_rename = zfsctl_snapdir_rename } },
1158 1160 { VOPNAME_RMDIR, { .vop_rmdir = zfsctl_snapdir_remove } },
1159 1161 { VOPNAME_MKDIR, { .vop_mkdir = zfsctl_snapdir_mkdir } },
1160 1162 { VOPNAME_READDIR, { .vop_readdir = gfs_vop_readdir } },
1161 1163 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_snapdir_lookup } },
1162 1164 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1163 1165 { VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapdir_inactive } },
1164 1166 { VOPNAME_FID, { .vop_fid = zfsctl_common_fid } },
1165 1167 { NULL }
1166 1168 };
1167 1169
1168 1170 static const fs_operation_def_t zfsctl_tops_shares[] = {
1169 1171 { VOPNAME_OPEN, { .vop_open = zfsctl_common_open } },
1170 1172 { VOPNAME_CLOSE, { .vop_close = zfsctl_common_close } },
1171 1173 { VOPNAME_IOCTL, { .error = fs_inval } },
1172 1174 { VOPNAME_GETATTR, { .vop_getattr = zfsctl_shares_getattr } },
1173 1175 { VOPNAME_ACCESS, { .vop_access = zfsctl_common_access } },
1174 1176 { VOPNAME_READDIR, { .vop_readdir = zfsctl_shares_readdir } },
1175 1177 { VOPNAME_LOOKUP, { .vop_lookup = zfsctl_shares_lookup } },
1176 1178 { VOPNAME_SEEK, { .vop_seek = fs_seek } },
1177 1179 { VOPNAME_INACTIVE, { .vop_inactive = gfs_vop_inactive } },
1178 1180 { VOPNAME_FID, { .vop_fid = zfsctl_shares_fid } },
1179 1181 { NULL }
1180 1182 };
1181 1183
1182 1184 /*
1183 1185 * pvp is the GFS vnode '.zfs/snapshot'.
1184 1186 *
1185 1187 * This creates a GFS node under '.zfs/snapshot' representing each
1186 1188 * snapshot. This newly created GFS node is what we mount snapshot
1187 1189 * vfs_t's ontop of.
1188 1190 */
1189 1191 static vnode_t *
1190 1192 zfsctl_snapshot_mknode(vnode_t *pvp, uint64_t objset)
1191 1193 {
1192 1194 vnode_t *vp;
1193 1195 zfsctl_node_t *zcp;
1194 1196
1195 1197 vp = gfs_dir_create(sizeof (zfsctl_node_t), pvp,
1196 1198 zfsctl_ops_snapshot, NULL, NULL, MAXNAMELEN, NULL, NULL);
1197 1199 zcp = vp->v_data;
1198 1200 zcp->zc_id = objset;
1199 1201
1200 1202 return (vp);
1201 1203 }
1202 1204
1203 1205 static void
1204 1206 zfsctl_snapshot_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
1205 1207 {
1206 1208 zfsctl_snapdir_t *sdp;
1207 1209 zfs_snapentry_t *sep, *next;
1208 1210 vnode_t *dvp;
1209 1211
1210 1212 VERIFY(gfs_dir_lookup(vp, "..", &dvp, cr, 0, NULL, NULL) == 0);
1211 1213 sdp = dvp->v_data;
1212 1214
1213 1215 mutex_enter(&sdp->sd_lock);
1214 1216
1215 1217 mutex_enter(&vp->v_lock);
1216 1218 if (vp->v_count > 1) {
1217 1219 vp->v_count--;
1218 1220 mutex_exit(&vp->v_lock);
1219 1221 mutex_exit(&sdp->sd_lock);
1220 1222 VN_RELE(dvp);
1221 1223 return;
1222 1224 }
1223 1225 mutex_exit(&vp->v_lock);
1224 1226 ASSERT(!vn_ismntpt(vp));
1225 1227
1226 1228 sep = avl_first(&sdp->sd_snaps);
1227 1229 while (sep != NULL) {
1228 1230 next = AVL_NEXT(&sdp->sd_snaps, sep);
1229 1231
1230 1232 if (sep->se_root == vp) {
1231 1233 avl_remove(&sdp->sd_snaps, sep);
1232 1234 kmem_free(sep->se_name, strlen(sep->se_name) + 1);
1233 1235 kmem_free(sep, sizeof (zfs_snapentry_t));
1234 1236 break;
1235 1237 }
1236 1238 sep = next;
1237 1239 }
1238 1240 ASSERT(sep != NULL);
1239 1241
1240 1242 mutex_exit(&sdp->sd_lock);
1241 1243 VN_RELE(dvp);
1242 1244
1243 1245 /*
1244 1246 * Dispose of the vnode for the snapshot mount point.
1245 1247 * This is safe to do because once this entry has been removed
1246 1248 * from the AVL tree, it can't be found again, so cannot become
1247 1249 * "active". If we lookup the same name again we will end up
1248 1250 * creating a new vnode.
1249 1251 */
1250 1252 gfs_vop_inactive(vp, cr, ct);
1251 1253 }
1252 1254
1253 1255
1254 1256 /*
1255 1257 * These VP's should never see the light of day. They should always
1256 1258 * be covered.
1257 1259 */
1258 1260 static const fs_operation_def_t zfsctl_tops_snapshot[] = {
1259 1261 VOPNAME_INACTIVE, { .vop_inactive = zfsctl_snapshot_inactive },
1260 1262 NULL, NULL
1261 1263 };
1262 1264
1263 1265 int
1264 1266 zfsctl_lookup_objset(vfs_t *vfsp, uint64_t objsetid, zfsvfs_t **zfsvfsp)
1265 1267 {
1266 1268 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1267 1269 vnode_t *dvp, *vp;
1268 1270 zfsctl_snapdir_t *sdp;
1269 1271 zfsctl_node_t *zcp;
1270 1272 zfs_snapentry_t *sep;
1271 1273 int error;
1272 1274
1273 1275 ASSERT(zfsvfs->z_ctldir != NULL);
1274 1276 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1275 1277 NULL, 0, NULL, kcred, NULL, NULL, NULL);
1276 1278 if (error != 0)
1277 1279 return (error);
1278 1280 sdp = dvp->v_data;
1279 1281
1280 1282 mutex_enter(&sdp->sd_lock);
1281 1283 sep = avl_first(&sdp->sd_snaps);
1282 1284 while (sep != NULL) {
1283 1285 vp = sep->se_root;
1284 1286 zcp = vp->v_data;
1285 1287 if (zcp->zc_id == objsetid)
1286 1288 break;
1287 1289
1288 1290 sep = AVL_NEXT(&sdp->sd_snaps, sep);
1289 1291 }
1290 1292
1291 1293 if (sep != NULL) {
1292 1294 VN_HOLD(vp);
1293 1295 /*
1294 1296 * Return the mounted root rather than the covered mount point.
1295 1297 * Takes the GFS vnode at .zfs/snapshot/<snapshot objsetid>
1296 1298 * and returns the ZFS vnode mounted on top of the GFS node.
1297 1299 * This ZFS vnode is the root of the vfs for objset 'objsetid'.
1298 1300 */
1299 1301 error = traverse(&vp);
1300 1302 if (error == 0) {
1301 1303 if (vp == sep->se_root)
1302 1304 error = SET_ERROR(EINVAL);
1303 1305 else
1304 1306 *zfsvfsp = VTOZ(vp)->z_zfsvfs;
1305 1307 }
1306 1308 mutex_exit(&sdp->sd_lock);
1307 1309 VN_RELE(vp);
1308 1310 } else {
1309 1311 error = SET_ERROR(EINVAL);
1310 1312 mutex_exit(&sdp->sd_lock);
1311 1313 }
1312 1314
1313 1315 VN_RELE(dvp);
1314 1316
1315 1317 return (error);
1316 1318 }
1317 1319
1318 1320 /*
1319 1321 * Unmount any snapshots for the given filesystem. This is called from
1320 1322 * zfs_umount() - if we have a ctldir, then go through and unmount all the
1321 1323 * snapshots.
1322 1324 */
1323 1325 int
1324 1326 zfsctl_umount_snapshots(vfs_t *vfsp, int fflags, cred_t *cr)
1325 1327 {
1326 1328 zfsvfs_t *zfsvfs = vfsp->vfs_data;
1327 1329 vnode_t *dvp;
1328 1330 zfsctl_snapdir_t *sdp;
1329 1331 zfs_snapentry_t *sep, *next;
1330 1332 int error;
1331 1333
1332 1334 ASSERT(zfsvfs->z_ctldir != NULL);
1333 1335 error = zfsctl_root_lookup(zfsvfs->z_ctldir, "snapshot", &dvp,
1334 1336 NULL, 0, NULL, cr, NULL, NULL, NULL);
1335 1337 if (error != 0)
1336 1338 return (error);
1337 1339 sdp = dvp->v_data;
1338 1340
1339 1341 mutex_enter(&sdp->sd_lock);
1340 1342
1341 1343 sep = avl_first(&sdp->sd_snaps);
1342 1344 while (sep != NULL) {
1343 1345 next = AVL_NEXT(&sdp->sd_snaps, sep);
1344 1346
1345 1347 /*
1346 1348 * If this snapshot is not mounted, then it must
1347 1349 * have just been unmounted by somebody else, and
1348 1350 * will be cleaned up by zfsctl_snapdir_inactive().
1349 1351 */
1350 1352 if (vn_ismntpt(sep->se_root)) {
1351 1353 avl_remove(&sdp->sd_snaps, sep);
1352 1354 error = zfsctl_unmount_snap(sep, fflags, cr);
1353 1355 if (error) {
1354 1356 avl_add(&sdp->sd_snaps, sep);
1355 1357 break;
1356 1358 }
1357 1359 }
1358 1360 sep = next;
1359 1361 }
1360 1362
1361 1363 mutex_exit(&sdp->sd_lock);
1362 1364 VN_RELE(dvp);
1363 1365
1364 1366 return (error);
1365 1367 }
|
↓ open down ↓ |
382 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX