]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_ctldir.c
Remove dummy znode from zvol_state
[mirror_zfs.git] / module / zfs / zfs_ctldir.c
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 *
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
25 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
26 * LLNL-CODE-403049.
27 * Rewritten for Linux by:
28 * Rohan Puri <rohan.puri15@gmail.com>
29 * Brian Behlendorf <behlendorf1@llnl.gov>
30 * Copyright (c) 2013 by Delphix. All rights reserved.
31 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
32 */
33
34 /*
35 * ZFS control directory (a.k.a. ".zfs")
36 *
37 * This directory provides a common location for all ZFS meta-objects.
38 * Currently, this is only the 'snapshot' and 'shares' directory, but this may
39 * expand in the future. The elements are built dynamically, as the hierarchy
40 * does not actually exist on disk.
41 *
42 * For 'snapshot', we don't want to have all snapshots always mounted, because
43 * this would take up a huge amount of space in /etc/mnttab. We have three
44 * types of objects:
45 *
46 * ctldir ------> snapshotdir -------> snapshot
47 * |
48 * |
49 * V
50 * mounted fs
51 *
52 * The 'snapshot' node contains just enough information to lookup '..' and act
53 * as a mountpoint for the snapshot. Whenever we lookup a specific snapshot, we
54 * perform an automount of the underlying filesystem and return the
55 * corresponding inode.
56 *
57 * All mounts are handled automatically by an user mode helper which invokes
58 * the mount mount procedure. Unmounts are handled by allowing the mount
59 * point to expire so the kernel may automatically unmount it.
60 *
61 * The '.zfs', '.zfs/snapshot', and all directories created under
62 * '.zfs/snapshot' (ie: '.zfs/snapshot/<snapname>') all share the same
63 * share the same zfs_sb_t as the head filesystem (what '.zfs' lives under).
64 *
65 * File systems mounted on top of the '.zfs/snapshot/<snapname>' paths
66 * (ie: snapshots) are complete ZFS filesystems and have their own unique
67 * zfs_sb_t. However, the fsid reported by these mounts will be the same
68 * as that used by the parent zfs_sb_t to make NFS happy.
69 */
70
71 #include <sys/types.h>
72 #include <sys/param.h>
73 #include <sys/time.h>
74 #include <sys/systm.h>
75 #include <sys/sysmacros.h>
76 #include <sys/pathname.h>
77 #include <sys/vfs.h>
78 #include <sys/vfs_opreg.h>
79 #include <sys/zfs_ctldir.h>
80 #include <sys/zfs_ioctl.h>
81 #include <sys/zfs_vfsops.h>
82 #include <sys/zfs_vnops.h>
83 #include <sys/stat.h>
84 #include <sys/dmu.h>
85 #include <sys/dmu_objset.h>
86 #include <sys/dsl_destroy.h>
87 #include <sys/dsl_deleg.h>
88 #include <sys/mount.h>
89 #include <sys/zpl.h>
90 #include "zfs_namecheck.h"
91
92 /*
93 * Two AVL trees are maintained which contain all currently automounted
94 * snapshots. Every automounted snapshots maps to a single zfs_snapentry_t
95 * entry which MUST:
96 *
97 * - be attached to both trees, and
98 * - be unique, no duplicate entries are allowed.
99 *
100 * The zfs_snapshots_by_name tree is indexed by the full dataset name
101 * while the zfs_snapshots_by_objsetid tree is indexed by the unique
102 * objsetid. This allows for fast lookups either by name or objsetid.
103 */
104 static avl_tree_t zfs_snapshots_by_name;
105 static avl_tree_t zfs_snapshots_by_objsetid;
106 static krwlock_t zfs_snapshot_lock;
107
108 /*
109 * Control Directory Tunables (.zfs)
110 */
111 int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT;
112 int zfs_admin_snapshot = 0;
113
114 /*
115 * Dedicated task queue for unmounting snapshots.
116 */
117 static taskq_t *zfs_expire_taskq;
118
119 typedef struct {
120 char *se_name; /* full snapshot name */
121 char *se_path; /* full mount path */
122 spa_t *se_spa; /* pool spa */
123 uint64_t se_objsetid; /* snapshot objset id */
124 struct dentry *se_root_dentry; /* snapshot root dentry */
125 taskqid_t se_taskqid; /* scheduled unmount taskqid */
126 avl_node_t se_node_name; /* zfs_snapshots_by_name link */
127 avl_node_t se_node_objsetid; /* zfs_snapshots_by_objsetid link */
128 refcount_t se_refcount; /* reference count */
129 } zfs_snapentry_t;
130
131 static void zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay);
132
133 /*
134 * Allocate a new zfs_snapentry_t being careful to make a copy of the
135 * the snapshot name and provided mount point. No reference is taken.
136 */
137 static zfs_snapentry_t *
138 zfsctl_snapshot_alloc(char *full_name, char *full_path, spa_t *spa,
139 uint64_t objsetid, struct dentry *root_dentry)
140 {
141 zfs_snapentry_t *se;
142
143 se = kmem_zalloc(sizeof (zfs_snapentry_t), KM_SLEEP);
144
145 se->se_name = strdup(full_name);
146 se->se_path = strdup(full_path);
147 se->se_spa = spa;
148 se->se_objsetid = objsetid;
149 se->se_root_dentry = root_dentry;
150 se->se_taskqid = -1;
151
152 refcount_create(&se->se_refcount);
153
154 return (se);
155 }
156
157 /*
158 * Free a zfs_snapentry_t the called must ensure there are no active
159 * references.
160 */
161 static void
162 zfsctl_snapshot_free(zfs_snapentry_t *se)
163 {
164 refcount_destroy(&se->se_refcount);
165 strfree(se->se_name);
166 strfree(se->se_path);
167
168 kmem_free(se, sizeof (zfs_snapentry_t));
169 }
170
171 /*
172 * Hold a reference on the zfs_snapentry_t.
173 */
174 static void
175 zfsctl_snapshot_hold(zfs_snapentry_t *se)
176 {
177 refcount_add(&se->se_refcount, NULL);
178 }
179
180 /*
181 * Release a reference on the zfs_snapentry_t. When the number of
182 * references drops to zero the structure will be freed.
183 */
184 static void
185 zfsctl_snapshot_rele(zfs_snapentry_t *se)
186 {
187 if (refcount_remove(&se->se_refcount, NULL) == 0)
188 zfsctl_snapshot_free(se);
189 }
190
191 /*
192 * Add a zfs_snapentry_t to both the zfs_snapshots_by_name and
193 * zfs_snapshots_by_objsetid trees. While the zfs_snapentry_t is part
194 * of the trees a reference is held.
195 */
196 static void
197 zfsctl_snapshot_add(zfs_snapentry_t *se)
198 {
199 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
200 refcount_add(&se->se_refcount, NULL);
201 avl_add(&zfs_snapshots_by_name, se);
202 avl_add(&zfs_snapshots_by_objsetid, se);
203 }
204
205 /*
206 * Remove a zfs_snapentry_t from both the zfs_snapshots_by_name and
207 * zfs_snapshots_by_objsetid trees. Upon removal a reference is dropped,
208 * this can result in the structure being freed if that was the last
209 * remaining reference.
210 */
211 static void
212 zfsctl_snapshot_remove(zfs_snapentry_t *se)
213 {
214 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
215 avl_remove(&zfs_snapshots_by_name, se);
216 avl_remove(&zfs_snapshots_by_objsetid, se);
217 zfsctl_snapshot_rele(se);
218 }
219
220 /*
221 * Snapshot name comparison function for the zfs_snapshots_by_name.
222 */
223 static int
224 snapentry_compare_by_name(const void *a, const void *b)
225 {
226 const zfs_snapentry_t *se_a = a;
227 const zfs_snapentry_t *se_b = b;
228 int ret;
229
230 ret = strcmp(se_a->se_name, se_b->se_name);
231
232 if (ret < 0)
233 return (-1);
234 else if (ret > 0)
235 return (1);
236 else
237 return (0);
238 }
239
240 /*
241 * Snapshot name comparison function for the zfs_snapshots_by_objsetid.
242 */
243 static int
244 snapentry_compare_by_objsetid(const void *a, const void *b)
245 {
246 const zfs_snapentry_t *se_a = a;
247 const zfs_snapentry_t *se_b = b;
248
249 if (se_a->se_spa != se_b->se_spa)
250 return ((ulong_t)se_a->se_spa < (ulong_t)se_b->se_spa ? -1 : 1);
251
252 if (se_a->se_objsetid < se_b->se_objsetid)
253 return (-1);
254 else if (se_a->se_objsetid > se_b->se_objsetid)
255 return (1);
256 else
257 return (0);
258 }
259
260 /*
261 * Find a zfs_snapentry_t in zfs_snapshots_by_name. If the snapname
262 * is found a pointer to the zfs_snapentry_t is returned and a reference
263 * taken on the structure. The caller is responsible for dropping the
264 * reference with zfsctl_snapshot_rele(). If the snapname is not found
265 * NULL will be returned.
266 */
267 static zfs_snapentry_t *
268 zfsctl_snapshot_find_by_name(char *snapname)
269 {
270 zfs_snapentry_t *se, search;
271
272 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
273
274 search.se_name = snapname;
275 se = avl_find(&zfs_snapshots_by_name, &search, NULL);
276 if (se)
277 refcount_add(&se->se_refcount, NULL);
278
279 return (se);
280 }
281
282 /*
283 * Find a zfs_snapentry_t in zfs_snapshots_by_objsetid given the objset id
284 * rather than the snapname. In all other respects it behaves the same
285 * as zfsctl_snapshot_find_by_name().
286 */
287 static zfs_snapentry_t *
288 zfsctl_snapshot_find_by_objsetid(spa_t *spa, uint64_t objsetid)
289 {
290 zfs_snapentry_t *se, search;
291
292 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
293
294 search.se_spa = spa;
295 search.se_objsetid = objsetid;
296 se = avl_find(&zfs_snapshots_by_objsetid, &search, NULL);
297 if (se)
298 refcount_add(&se->se_refcount, NULL);
299
300 return (se);
301 }
302
303 /*
304 * Rename a zfs_snapentry_t in the zfs_snapshots_by_name. The structure is
305 * removed, renamed, and added back to the new correct location in the tree.
306 */
307 static int
308 zfsctl_snapshot_rename(char *old_snapname, char *new_snapname)
309 {
310 zfs_snapentry_t *se;
311
312 ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock));
313
314 se = zfsctl_snapshot_find_by_name(old_snapname);
315 if (se == NULL)
316 return (ENOENT);
317
318 zfsctl_snapshot_remove(se);
319 strfree(se->se_name);
320 se->se_name = strdup(new_snapname);
321 zfsctl_snapshot_add(se);
322 zfsctl_snapshot_rele(se);
323
324 return (0);
325 }
326
327 /*
328 * Delayed task responsible for unmounting an expired automounted snapshot.
329 */
330 static void
331 snapentry_expire(void *data)
332 {
333 zfs_snapentry_t *se = (zfs_snapentry_t *)data;
334 spa_t *spa = se->se_spa;
335 uint64_t objsetid = se->se_objsetid;
336
337 if (zfs_expire_snapshot <= 0) {
338 zfsctl_snapshot_rele(se);
339 return;
340 }
341
342 se->se_taskqid = -1;
343 (void) zfsctl_snapshot_unmount(se->se_name, MNT_EXPIRE);
344 zfsctl_snapshot_rele(se);
345
346 /*
347 * Reschedule the unmount if the zfs_snapentry_t wasn't removed.
348 * This can occur when the snapshot is busy.
349 */
350 rw_enter(&zfs_snapshot_lock, RW_READER);
351 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
352 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
353 zfsctl_snapshot_rele(se);
354 }
355 rw_exit(&zfs_snapshot_lock);
356 }
357
358 /*
359 * Cancel an automatic unmount of a snapname. This callback is responsible
360 * for dropping the reference on the zfs_snapentry_t which was taken when
361 * during dispatch.
362 */
363 static void
364 zfsctl_snapshot_unmount_cancel(zfs_snapentry_t *se)
365 {
366 ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock));
367
368 if (taskq_cancel_id(zfs_expire_taskq, se->se_taskqid) == 0) {
369 se->se_taskqid = -1;
370 zfsctl_snapshot_rele(se);
371 }
372 }
373
374 /*
375 * Dispatch the unmount task for delayed handling with a hold protecting it.
376 */
377 static void
378 zfsctl_snapshot_unmount_delay_impl(zfs_snapentry_t *se, int delay)
379 {
380 ASSERT3S(se->se_taskqid, ==, -1);
381
382 if (delay <= 0)
383 return;
384
385 zfsctl_snapshot_hold(se);
386 se->se_taskqid = taskq_dispatch_delay(zfs_expire_taskq,
387 snapentry_expire, se, TQ_SLEEP, ddi_get_lbolt() + delay * HZ);
388 }
389
390 /*
391 * Schedule an automatic unmount of objset id to occur in delay seconds from
392 * now. Any previous delayed unmount will be cancelled in favor of the
393 * updated deadline. A reference is taken by zfsctl_snapshot_find_by_name()
394 * and held until the outstanding task is handled or cancelled.
395 */
396 int
397 zfsctl_snapshot_unmount_delay(spa_t *spa, uint64_t objsetid, int delay)
398 {
399 zfs_snapentry_t *se;
400 int error = ENOENT;
401
402 rw_enter(&zfs_snapshot_lock, RW_READER);
403 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
404 zfsctl_snapshot_unmount_cancel(se);
405 zfsctl_snapshot_unmount_delay_impl(se, delay);
406 zfsctl_snapshot_rele(se);
407 error = 0;
408 }
409 rw_exit(&zfs_snapshot_lock);
410
411 return (error);
412 }
413
414 /*
415 * Check if snapname is currently mounted. Returned non-zero when mounted
416 * and zero when unmounted.
417 */
418 static boolean_t
419 zfsctl_snapshot_ismounted(char *snapname)
420 {
421 zfs_snapentry_t *se;
422 boolean_t ismounted = B_FALSE;
423
424 rw_enter(&zfs_snapshot_lock, RW_READER);
425 if ((se = zfsctl_snapshot_find_by_name(snapname)) != NULL) {
426 zfsctl_snapshot_rele(se);
427 ismounted = B_TRUE;
428 }
429 rw_exit(&zfs_snapshot_lock);
430
431 return (ismounted);
432 }
433
434 /*
435 * Check if the given inode is a part of the virtual .zfs directory.
436 */
437 boolean_t
438 zfsctl_is_node(struct inode *ip)
439 {
440 return (ITOZ(ip)->z_is_ctldir);
441 }
442
443 /*
444 * Check if the given inode is a .zfs/snapshots/snapname directory.
445 */
446 boolean_t
447 zfsctl_is_snapdir(struct inode *ip)
448 {
449 return (zfsctl_is_node(ip) && (ip->i_ino <= ZFSCTL_INO_SNAPDIRS));
450 }
451
452 /*
453 * Allocate a new inode with the passed id and ops.
454 */
455 static struct inode *
456 zfsctl_inode_alloc(zfs_sb_t *zsb, uint64_t id,
457 const struct file_operations *fops, const struct inode_operations *ops)
458 {
459 struct timespec now = current_fs_time(zsb->z_sb);
460 struct inode *ip;
461 znode_t *zp;
462
463 ip = new_inode(zsb->z_sb);
464 if (ip == NULL)
465 return (NULL);
466
467 zp = ITOZ(ip);
468 ASSERT3P(zp->z_dirlocks, ==, NULL);
469 ASSERT3P(zp->z_acl_cached, ==, NULL);
470 ASSERT3P(zp->z_xattr_cached, ==, NULL);
471 zp->z_id = id;
472 zp->z_unlinked = 0;
473 zp->z_atime_dirty = 0;
474 zp->z_zn_prefetch = 0;
475 zp->z_moved = 0;
476 zp->z_sa_hdl = NULL;
477 zp->z_blksz = 0;
478 zp->z_seq = 0;
479 zp->z_mapcnt = 0;
480 zp->z_gen = 0;
481 zp->z_size = 0;
482 zp->z_links = 0;
483 zp->z_pflags = 0;
484 zp->z_uid = 0;
485 zp->z_gid = 0;
486 zp->z_mode = 0;
487 zp->z_sync_cnt = 0;
488 zp->z_is_mapped = B_FALSE;
489 zp->z_is_ctldir = B_TRUE;
490 zp->z_is_sa = B_FALSE;
491 zp->z_is_stale = B_FALSE;
492 ip->i_ino = id;
493 ip->i_mode = (S_IFDIR | S_IRUGO | S_IXUGO);
494 ip->i_uid = SUID_TO_KUID(0);
495 ip->i_gid = SGID_TO_KGID(0);
496 ip->i_blkbits = SPA_MINBLOCKSHIFT;
497 ip->i_atime = now;
498 ip->i_mtime = now;
499 ip->i_ctime = now;
500 ip->i_fop = fops;
501 ip->i_op = ops;
502
503 if (insert_inode_locked(ip)) {
504 unlock_new_inode(ip);
505 iput(ip);
506 return (NULL);
507 }
508
509 mutex_enter(&zsb->z_znodes_lock);
510 list_insert_tail(&zsb->z_all_znodes, zp);
511 zsb->z_nr_znodes++;
512 membar_producer();
513 mutex_exit(&zsb->z_znodes_lock);
514
515 unlock_new_inode(ip);
516
517 return (ip);
518 }
519
520 /*
521 * Lookup the inode with given id, it will be allocated if needed.
522 */
523 static struct inode *
524 zfsctl_inode_lookup(zfs_sb_t *zsb, uint64_t id,
525 const struct file_operations *fops, const struct inode_operations *ops)
526 {
527 struct inode *ip = NULL;
528
529 while (ip == NULL) {
530 ip = ilookup(zsb->z_sb, (unsigned long)id);
531 if (ip)
532 break;
533
534 /* May fail due to concurrent zfsctl_inode_alloc() */
535 ip = zfsctl_inode_alloc(zsb, id, fops, ops);
536 }
537
538 return (ip);
539 }
540
541 /*
542 * Create the '.zfs' directory. This directory is cached as part of the VFS
543 * structure. This results in a hold on the zfs_sb_t. The code in zfs_umount()
544 * therefore checks against a vfs_count of 2 instead of 1. This reference
545 * is removed when the ctldir is destroyed in the unmount. All other entities
546 * under the '.zfs' directory are created dynamically as needed.
547 *
548 * Because the dynamically created '.zfs' directory entries assume the use
549 * of 64-bit inode numbers this support must be disabled on 32-bit systems.
550 */
551 int
552 zfsctl_create(zfs_sb_t *zsb)
553 {
554 #if defined(CONFIG_64BIT)
555 ASSERT(zsb->z_ctldir == NULL);
556
557 zsb->z_ctldir = zfsctl_inode_alloc(zsb, ZFSCTL_INO_ROOT,
558 &zpl_fops_root, &zpl_ops_root);
559 if (zsb->z_ctldir == NULL)
560 return (SET_ERROR(ENOENT));
561
562 return (0);
563 #else
564 return (SET_ERROR(EOPNOTSUPP));
565 #endif /* CONFIG_64BIT */
566 }
567
568 /*
569 * Destroy the '.zfs' directory or remove a snapshot from zfs_snapshots_by_name.
570 * Only called when the filesystem is unmounted.
571 */
572 void
573 zfsctl_destroy(zfs_sb_t *zsb)
574 {
575 if (zsb->z_issnap) {
576 zfs_snapentry_t *se;
577 spa_t *spa = zsb->z_os->os_spa;
578 uint64_t objsetid = dmu_objset_id(zsb->z_os);
579
580 rw_enter(&zfs_snapshot_lock, RW_WRITER);
581 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid))
582 != NULL) {
583 zfsctl_snapshot_unmount_cancel(se);
584 zfsctl_snapshot_remove(se);
585 zfsctl_snapshot_rele(se);
586 }
587 rw_exit(&zfs_snapshot_lock);
588 } else if (zsb->z_ctldir) {
589 iput(zsb->z_ctldir);
590 zsb->z_ctldir = NULL;
591 }
592 }
593
594 /*
595 * Given a root znode, retrieve the associated .zfs directory.
596 * Add a hold to the vnode and return it.
597 */
598 struct inode *
599 zfsctl_root(znode_t *zp)
600 {
601 ASSERT(zfs_has_ctldir(zp));
602 igrab(ZTOZSB(zp)->z_ctldir);
603 return (ZTOZSB(zp)->z_ctldir);
604 }
605 /*
606 * Generate a long fid which includes the root object and objset of a
607 * snapshot but not the generation number. For the root object the
608 * generation number is ignored when zero to avoid needing to open
609 * the dataset when generating fids for the snapshot names.
610 */
611 static int
612 zfsctl_snapdir_fid(struct inode *ip, fid_t *fidp)
613 {
614 zfs_sb_t *zsb = ITOZSB(ip);
615 zfid_short_t *zfid = (zfid_short_t *)fidp;
616 zfid_long_t *zlfid = (zfid_long_t *)fidp;
617 uint32_t gen = 0;
618 uint64_t object;
619 uint64_t objsetid;
620 int i;
621
622 object = zsb->z_root;
623 objsetid = ZFSCTL_INO_SNAPDIRS - ip->i_ino;
624 zfid->zf_len = LONG_FID_LEN;
625
626 for (i = 0; i < sizeof (zfid->zf_object); i++)
627 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
628
629 for (i = 0; i < sizeof (zfid->zf_gen); i++)
630 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
631
632 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
633 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
634
635 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
636 zlfid->zf_setgen[i] = 0;
637
638 return (0);
639 }
640
641 /*
642 * Generate an appropriate fid for an entry in the .zfs directory.
643 */
644 int
645 zfsctl_fid(struct inode *ip, fid_t *fidp)
646 {
647 znode_t *zp = ITOZ(ip);
648 zfs_sb_t *zsb = ITOZSB(ip);
649 uint64_t object = zp->z_id;
650 zfid_short_t *zfid;
651 int i;
652
653 ZFS_ENTER(zsb);
654
655 if (fidp->fid_len < SHORT_FID_LEN) {
656 fidp->fid_len = SHORT_FID_LEN;
657 ZFS_EXIT(zsb);
658 return (SET_ERROR(ENOSPC));
659 }
660
661 if (zfsctl_is_snapdir(ip)) {
662 ZFS_EXIT(zsb);
663 return (zfsctl_snapdir_fid(ip, fidp));
664 }
665
666 zfid = (zfid_short_t *)fidp;
667
668 zfid->zf_len = SHORT_FID_LEN;
669
670 for (i = 0; i < sizeof (zfid->zf_object); i++)
671 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
672
673 /* .zfs znodes always have a generation number of 0 */
674 for (i = 0; i < sizeof (zfid->zf_gen); i++)
675 zfid->zf_gen[i] = 0;
676
677 ZFS_EXIT(zsb);
678 return (0);
679 }
680
681 /*
682 * Construct a full dataset name in full_name: "pool/dataset@snap_name"
683 */
684 static int
685 zfsctl_snapshot_name(zfs_sb_t *zsb, const char *snap_name, int len,
686 char *full_name)
687 {
688 objset_t *os = zsb->z_os;
689
690 if (zfs_component_namecheck(snap_name, NULL, NULL) != 0)
691 return (SET_ERROR(EILSEQ));
692
693 dmu_objset_name(os, full_name);
694 if ((strlen(full_name) + 1 + strlen(snap_name)) >= len)
695 return (SET_ERROR(ENAMETOOLONG));
696
697 (void) strcat(full_name, "@");
698 (void) strcat(full_name, snap_name);
699
700 return (0);
701 }
702
703 /*
704 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/"
705 */
706 static int
707 zfsctl_snapshot_path(struct path *path, int len, char *full_path)
708 {
709 char *path_buffer, *path_ptr;
710 int path_len, error = 0;
711
712 path_buffer = kmem_alloc(len, KM_SLEEP);
713
714 path_ptr = d_path(path, path_buffer, len);
715 if (IS_ERR(path_ptr)) {
716 error = -PTR_ERR(path_ptr);
717 goto out;
718 }
719
720 path_len = path_buffer + len - 1 - path_ptr;
721 if (path_len > len) {
722 error = SET_ERROR(EFAULT);
723 goto out;
724 }
725
726 memcpy(full_path, path_ptr, path_len);
727 full_path[path_len] = '\0';
728 out:
729 kmem_free(path_buffer, len);
730
731 return (error);
732 }
733
734 /*
735 * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/"
736 */
737 static int
738 zfsctl_snapshot_path_objset(zfs_sb_t *zsb, uint64_t objsetid,
739 int path_len, char *full_path)
740 {
741 objset_t *os = zsb->z_os;
742 fstrans_cookie_t cookie;
743 char *snapname;
744 boolean_t case_conflict;
745 uint64_t id, pos = 0;
746 int error = 0;
747
748 if (zsb->z_mntopts->z_mntpoint == NULL)
749 return (ENOENT);
750
751 cookie = spl_fstrans_mark();
752 snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
753
754 while (error == 0) {
755 dsl_pool_config_enter(dmu_objset_pool(os), FTAG);
756 error = dmu_snapshot_list_next(zsb->z_os, MAXNAMELEN,
757 snapname, &id, &pos, &case_conflict);
758 dsl_pool_config_exit(dmu_objset_pool(os), FTAG);
759 if (error)
760 goto out;
761
762 if (id == objsetid)
763 break;
764 }
765
766 memset(full_path, 0, path_len);
767 snprintf(full_path, path_len - 1, "%s/.zfs/snapshot/%s",
768 zsb->z_mntopts->z_mntpoint, snapname);
769 out:
770 kmem_free(snapname, MAXNAMELEN);
771 spl_fstrans_unmark(cookie);
772
773 return (error);
774 }
775
776 /*
777 * Special case the handling of "..".
778 */
779 int
780 zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp,
781 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
782 {
783 zfs_sb_t *zsb = ITOZSB(dip);
784 int error = 0;
785
786 ZFS_ENTER(zsb);
787
788 if (strcmp(name, "..") == 0) {
789 *ipp = dip->i_sb->s_root->d_inode;
790 } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) {
791 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIR,
792 &zpl_fops_snapdir, &zpl_ops_snapdir);
793 } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) {
794 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SHARES,
795 &zpl_fops_shares, &zpl_ops_shares);
796 } else {
797 *ipp = NULL;
798 }
799
800 if (*ipp == NULL)
801 error = SET_ERROR(ENOENT);
802
803 ZFS_EXIT(zsb);
804
805 return (error);
806 }
807
808 /*
809 * Lookup entry point for the 'snapshot' directory. Try to open the
810 * snapshot if it exist, creating the pseudo filesystem inode as necessary.
811 * Perform a mount of the associated dataset on top of the inode.
812 */
813 int
814 zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp,
815 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
816 {
817 zfs_sb_t *zsb = ITOZSB(dip);
818 uint64_t id;
819 int error;
820
821 ZFS_ENTER(zsb);
822
823 error = dmu_snapshot_lookup(zsb->z_os, name, &id);
824 if (error) {
825 ZFS_EXIT(zsb);
826 return (error);
827 }
828
829 *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIRS - id,
830 &simple_dir_operations, &simple_dir_inode_operations);
831 if (*ipp == NULL)
832 error = SET_ERROR(ENOENT);
833
834 ZFS_EXIT(zsb);
835
836 return (error);
837 }
838
839 /*
840 * Renaming a directory under '.zfs/snapshot' will automatically trigger
841 * a rename of the snapshot to the new given name. The rename is confined
842 * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere.
843 */
844 int
845 zfsctl_snapdir_rename(struct inode *sdip, char *snm,
846 struct inode *tdip, char *tnm, cred_t *cr, int flags)
847 {
848 zfs_sb_t *zsb = ITOZSB(sdip);
849 char *to, *from, *real, *fsname;
850 int error;
851
852 if (!zfs_admin_snapshot)
853 return (EACCES);
854
855 ZFS_ENTER(zsb);
856
857 to = kmem_alloc(MAXNAMELEN, KM_SLEEP);
858 from = kmem_alloc(MAXNAMELEN, KM_SLEEP);
859 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
860 fsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
861
862 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
863 error = dmu_snapshot_realname(zsb->z_os, snm, real,
864 MAXNAMELEN, NULL);
865 if (error == 0) {
866 snm = real;
867 } else if (error != ENOTSUP) {
868 goto out;
869 }
870 }
871
872 dmu_objset_name(zsb->z_os, fsname);
873
874 error = zfsctl_snapshot_name(ITOZSB(sdip), snm, MAXNAMELEN, from);
875 if (error == 0)
876 error = zfsctl_snapshot_name(ITOZSB(tdip), tnm, MAXNAMELEN, to);
877 if (error == 0)
878 error = zfs_secpolicy_rename_perms(from, to, cr);
879 if (error != 0)
880 goto out;
881
882 /*
883 * Cannot move snapshots out of the snapdir.
884 */
885 if (sdip != tdip) {
886 error = SET_ERROR(EINVAL);
887 goto out;
888 }
889
890 /*
891 * No-op when names are identical.
892 */
893 if (strcmp(snm, tnm) == 0) {
894 error = 0;
895 goto out;
896 }
897
898 rw_enter(&zfs_snapshot_lock, RW_WRITER);
899
900 error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE);
901 if (error == 0)
902 (void) zfsctl_snapshot_rename(snm, tnm);
903
904 rw_exit(&zfs_snapshot_lock);
905 out:
906 kmem_free(from, MAXNAMELEN);
907 kmem_free(to, MAXNAMELEN);
908 kmem_free(real, MAXNAMELEN);
909 kmem_free(fsname, MAXNAMELEN);
910
911 ZFS_EXIT(zsb);
912
913 return (error);
914 }
915
916 /*
917 * Removing a directory under '.zfs/snapshot' will automatically trigger
918 * the removal of the snapshot with the given name.
919 */
920 int
921 zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags)
922 {
923 zfs_sb_t *zsb = ITOZSB(dip);
924 char *snapname, *real;
925 int error;
926
927 if (!zfs_admin_snapshot)
928 return (EACCES);
929
930 ZFS_ENTER(zsb);
931
932 snapname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
933 real = kmem_alloc(MAXNAMELEN, KM_SLEEP);
934
935 if (zsb->z_case == ZFS_CASE_INSENSITIVE) {
936 error = dmu_snapshot_realname(zsb->z_os, name, real,
937 MAXNAMELEN, NULL);
938 if (error == 0) {
939 name = real;
940 } else if (error != ENOTSUP) {
941 goto out;
942 }
943 }
944
945 error = zfsctl_snapshot_name(ITOZSB(dip), name, MAXNAMELEN, snapname);
946 if (error == 0)
947 error = zfs_secpolicy_destroy_perms(snapname, cr);
948 if (error != 0)
949 goto out;
950
951 error = zfsctl_snapshot_unmount(snapname, MNT_FORCE);
952 if ((error == 0) || (error == ENOENT))
953 error = dsl_destroy_snapshot(snapname, B_FALSE);
954 out:
955 kmem_free(snapname, MAXNAMELEN);
956 kmem_free(real, MAXNAMELEN);
957
958 ZFS_EXIT(zsb);
959
960 return (error);
961 }
962
963 /*
964 * Creating a directory under '.zfs/snapshot' will automatically trigger
965 * the creation of a new snapshot with the given name.
966 */
967 int
968 zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap,
969 struct inode **ipp, cred_t *cr, int flags)
970 {
971 zfs_sb_t *zsb = ITOZSB(dip);
972 char *dsname;
973 int error;
974
975 if (!zfs_admin_snapshot)
976 return (EACCES);
977
978 dsname = kmem_alloc(MAXNAMELEN, KM_SLEEP);
979
980 if (zfs_component_namecheck(dirname, NULL, NULL) != 0) {
981 error = SET_ERROR(EILSEQ);
982 goto out;
983 }
984
985 dmu_objset_name(zsb->z_os, dsname);
986
987 error = zfs_secpolicy_snapshot_perms(dsname, cr);
988 if (error != 0)
989 goto out;
990
991 if (error == 0) {
992 error = dmu_objset_snapshot_one(dsname, dirname);
993 if (error != 0)
994 goto out;
995
996 error = zfsctl_snapdir_lookup(dip, dirname, ipp,
997 0, cr, NULL, NULL);
998 }
999 out:
1000 kmem_free(dsname, MAXNAMELEN);
1001
1002 return (error);
1003 }
1004
1005 /*
1006 * Attempt to unmount a snapshot by making a call to user space.
1007 * There is no assurance that this can or will succeed, is just a
1008 * best effort. In the case where it does fail, perhaps because
1009 * it's in use, the unmount will fail harmlessly.
1010 */
1011 #define SET_UNMOUNT_CMD \
1012 "exec 0</dev/null " \
1013 " 1>/dev/null " \
1014 " 2>/dev/null; " \
1015 "umount -t zfs -n %s'%s'"
1016
1017 int
1018 zfsctl_snapshot_unmount(char *snapname, int flags)
1019 {
1020 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
1021 char *envp[] = { NULL };
1022 zfs_snapentry_t *se;
1023 int error;
1024
1025 rw_enter(&zfs_snapshot_lock, RW_READER);
1026 if ((se = zfsctl_snapshot_find_by_name(snapname)) == NULL) {
1027 rw_exit(&zfs_snapshot_lock);
1028 return (ENOENT);
1029 }
1030 rw_exit(&zfs_snapshot_lock);
1031
1032 argv[2] = kmem_asprintf(SET_UNMOUNT_CMD,
1033 flags & MNT_FORCE ? "-f " : "", se->se_path);
1034 zfsctl_snapshot_rele(se);
1035 dprintf("unmount; path=%s\n", se->se_path);
1036 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1037 strfree(argv[2]);
1038
1039
1040 /*
1041 * The umount system utility will return 256 on error. We must
1042 * assume this error is because the file system is busy so it is
1043 * converted to the more sensible EBUSY.
1044 */
1045 if (error)
1046 error = SET_ERROR(EBUSY);
1047
1048 return (error);
1049 }
1050
1051 #define MOUNT_BUSY 0x80 /* Mount failed due to EBUSY (from mntent.h) */
1052 #define SET_MOUNT_CMD \
1053 "exec 0</dev/null " \
1054 " 1>/dev/null " \
1055 " 2>/dev/null; " \
1056 "mount -t zfs -n '%s' '%s'"
1057
1058 int
1059 zfsctl_snapshot_mount(struct path *path, int flags)
1060 {
1061 struct dentry *dentry = path->dentry;
1062 struct inode *ip = dentry->d_inode;
1063 zfs_sb_t *zsb;
1064 zfs_sb_t *snap_zsb;
1065 zfs_snapentry_t *se;
1066 char *full_name, *full_path;
1067 char *argv[] = { "/bin/sh", "-c", NULL, NULL };
1068 char *envp[] = { NULL };
1069 int error;
1070 struct path spath;
1071
1072 if (ip == NULL)
1073 return (EISDIR);
1074
1075 zsb = ITOZSB(ip);
1076 ZFS_ENTER(zsb);
1077
1078 full_name = kmem_zalloc(MAXNAMELEN, KM_SLEEP);
1079 full_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
1080
1081 error = zfsctl_snapshot_name(zsb, dname(dentry),
1082 MAXNAMELEN, full_name);
1083 if (error)
1084 goto error;
1085
1086 error = zfsctl_snapshot_path(path, MAXPATHLEN, full_path);
1087 if (error)
1088 goto error;
1089
1090 /*
1091 * Multiple concurrent automounts of a snapshot are never allowed.
1092 * The snapshot may be manually mounted as many times as desired.
1093 */
1094 if (zfsctl_snapshot_ismounted(full_name)) {
1095 error = 0;
1096 goto error;
1097 }
1098
1099 /*
1100 * Attempt to mount the snapshot from user space. Normally this
1101 * would be done using the vfs_kern_mount() function, however that
1102 * function is marked GPL-only and cannot be used. On error we
1103 * careful to log the real error to the console and return EISDIR
1104 * to safely abort the automount. This should be very rare.
1105 *
1106 * If the user mode helper happens to return EBUSY, a concurrent
1107 * mount is already in progress in which case the error is ignored.
1108 * Take note that if the program was executed successfully the return
1109 * value from call_usermodehelper() will be (exitcode << 8 + signal).
1110 */
1111 dprintf("mount; name=%s path=%s\n", full_name, full_path);
1112 argv[2] = kmem_asprintf(SET_MOUNT_CMD, full_name, full_path);
1113 error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
1114 strfree(argv[2]);
1115 if (error) {
1116 if (!(error & MOUNT_BUSY << 8)) {
1117 cmn_err(CE_WARN, "Unable to automount %s/%s: %d",
1118 full_path, full_name, error);
1119 error = SET_ERROR(EISDIR);
1120 } else {
1121 /*
1122 * EBUSY, this could mean a concurrent mount, or the
1123 * snapshot has already been mounted at completely
1124 * different place. We return 0 so VFS will retry. For
1125 * the latter case the VFS will retry several times
1126 * and return ELOOP, which is probably not a very good
1127 * behavior.
1128 */
1129 error = 0;
1130 }
1131 goto error;
1132 }
1133
1134 /*
1135 * Follow down in to the mounted snapshot and set MNT_SHRINKABLE
1136 * to identify this as an automounted filesystem.
1137 */
1138 spath = *path;
1139 path_get(&spath);
1140 if (zpl_follow_down_one(&spath)) {
1141 snap_zsb = ITOZSB(spath.dentry->d_inode);
1142 snap_zsb->z_parent = zsb;
1143 dentry = spath.dentry;
1144 spath.mnt->mnt_flags |= MNT_SHRINKABLE;
1145
1146 rw_enter(&zfs_snapshot_lock, RW_WRITER);
1147 se = zfsctl_snapshot_alloc(full_name, full_path,
1148 snap_zsb->z_os->os_spa, dmu_objset_id(snap_zsb->z_os),
1149 dentry);
1150 zfsctl_snapshot_add(se);
1151 zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot);
1152 rw_exit(&zfs_snapshot_lock);
1153 }
1154 path_put(&spath);
1155 error:
1156 kmem_free(full_name, MAXNAMELEN);
1157 kmem_free(full_path, MAXPATHLEN);
1158
1159 ZFS_EXIT(zsb);
1160
1161 return (error);
1162 }
1163
1164 /*
1165 * Given the objset id of the snapshot return its zfs_sb_t as zsbp.
1166 */
1167 int
1168 zfsctl_lookup_objset(struct super_block *sb, uint64_t objsetid, zfs_sb_t **zsbp)
1169 {
1170 zfs_snapentry_t *se;
1171 int error;
1172 spa_t *spa = ((zfs_sb_t *)(sb->s_fs_info))->z_os->os_spa;
1173
1174 /*
1175 * Verify that the snapshot is mounted then lookup the mounted root
1176 * rather than the covered mount point. This may fail if the
1177 * snapshot has just been unmounted by an unrelated user space
1178 * process. This race cannot occur to an expired mount point
1179 * because we hold the zfs_snapshot_lock to prevent the race.
1180 */
1181 rw_enter(&zfs_snapshot_lock, RW_READER);
1182 if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) {
1183 zfs_sb_t *zsb;
1184
1185 zsb = ITOZSB(se->se_root_dentry->d_inode);
1186 ASSERT3U(dmu_objset_id(zsb->z_os), ==, objsetid);
1187
1188 if (time_after(jiffies, zsb->z_snap_defer_time +
1189 MAX(zfs_expire_snapshot * HZ / 2, HZ))) {
1190 zsb->z_snap_defer_time = jiffies;
1191 zfsctl_snapshot_unmount_cancel(se);
1192 zfsctl_snapshot_unmount_delay_impl(se,
1193 zfs_expire_snapshot);
1194 }
1195
1196 *zsbp = zsb;
1197 zfsctl_snapshot_rele(se);
1198 error = SET_ERROR(0);
1199 } else {
1200 error = SET_ERROR(ENOENT);
1201 }
1202 rw_exit(&zfs_snapshot_lock);
1203
1204 /*
1205 * Automount the snapshot given the objset id by constructing the
1206 * full mount point and performing a traversal.
1207 */
1208 if (error == ENOENT) {
1209 struct path path;
1210 char *mnt;
1211
1212 mnt = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1213 error = zfsctl_snapshot_path_objset(sb->s_fs_info, objsetid,
1214 MAXPATHLEN, mnt);
1215 if (error) {
1216 kmem_free(mnt, MAXPATHLEN);
1217 return (SET_ERROR(error));
1218 }
1219
1220 error = kern_path(mnt, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path);
1221 if (error == 0) {
1222 *zsbp = ITOZSB(path.dentry->d_inode);
1223 path_put(&path);
1224 }
1225
1226 kmem_free(mnt, MAXPATHLEN);
1227 }
1228
1229 return (error);
1230 }
1231
1232 int
1233 zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp,
1234 int flags, cred_t *cr, int *direntflags, pathname_t *realpnp)
1235 {
1236 zfs_sb_t *zsb = ITOZSB(dip);
1237 struct inode *ip;
1238 znode_t *dzp;
1239 int error;
1240
1241 ZFS_ENTER(zsb);
1242
1243 if (zsb->z_shares_dir == 0) {
1244 ZFS_EXIT(zsb);
1245 return (SET_ERROR(ENOTSUP));
1246 }
1247
1248 if ((error = zfs_zget(zsb, zsb->z_shares_dir, &dzp)) == 0) {
1249 error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL);
1250 iput(ZTOI(dzp));
1251 }
1252
1253 ZFS_EXIT(zsb);
1254
1255 return (error);
1256 }
1257
1258 /*
1259 * Initialize the various pieces we'll need to create and manipulate .zfs
1260 * directories. Currently this is unused but available.
1261 */
1262 void
1263 zfsctl_init(void)
1264 {
1265 avl_create(&zfs_snapshots_by_name, snapentry_compare_by_name,
1266 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1267 se_node_name));
1268 avl_create(&zfs_snapshots_by_objsetid, snapentry_compare_by_objsetid,
1269 sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t,
1270 se_node_objsetid));
1271 rw_init(&zfs_snapshot_lock, NULL, RW_DEFAULT, NULL);
1272
1273 zfs_expire_taskq = taskq_create("z_unmount", 1, defclsyspri,
1274 1, 8, TASKQ_PREPOPULATE);
1275 }
1276
1277 /*
1278 * Cleanup the various pieces we needed for .zfs directories. In particular
1279 * ensure the expiry timer is canceled safely.
1280 */
1281 void
1282 zfsctl_fini(void)
1283 {
1284 taskq_destroy(zfs_expire_taskq);
1285
1286 avl_destroy(&zfs_snapshots_by_name);
1287 avl_destroy(&zfs_snapshots_by_objsetid);
1288 rw_destroy(&zfs_snapshot_lock);
1289 }
1290
1291 module_param(zfs_admin_snapshot, int, 0644);
1292 MODULE_PARM_DESC(zfs_admin_snapshot, "Enable mkdir/rmdir/mv in .zfs/snapshot");
1293
1294 module_param(zfs_expire_snapshot, int, 0644);
1295 MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot");