]>
Commit | Line | Data |
---|---|---|
ebe7e575 BB |
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> | |
2e528b49 | 30 | * Copyright (c) 2013 by Delphix. All rights reserved. |
8adb798a | 31 | * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. |
ebe7e575 BB |
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> | |
24ef51f6 | 85 | #include <sys/dmu_objset.h> |
13fe0198 | 86 | #include <sys/dsl_destroy.h> |
ebe7e575 BB |
87 | #include <sys/dsl_deleg.h> |
88 | #include <sys/mount.h> | |
89 | #include <sys/zpl.h> | |
90 | #include "zfs_namecheck.h" | |
91 | ||
278bee93 BB |
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; | |
5ed27c57 | 106 | static krwlock_t zfs_snapshot_lock; |
278bee93 | 107 | |
ebe7e575 BB |
108 | /* |
109 | * Control Directory Tunables (.zfs) | |
110 | */ | |
111 | int zfs_expire_snapshot = ZFSCTL_EXPIRE_SNAPSHOT; | |
f74b821a | 112 | int zfs_admin_snapshot = 1; |
ebe7e575 | 113 | |
2ae10319 BB |
114 | /* |
115 | * Dedicated task queue for unmounting snapshots. | |
116 | */ | |
117 | static taskq_t *zfs_expire_taskq; | |
118 | ||
278bee93 BB |
119 | typedef struct { |
120 | char *se_name; /* full snapshot name */ | |
121 | char *se_path; /* full mount path */ | |
24ef51f6 | 122 | spa_t *se_spa; /* pool spa */ |
278bee93 BB |
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 | */ | |
ebe7e575 | 137 | static zfs_snapentry_t * |
24ef51f6 CC |
138 | zfsctl_snapshot_alloc(char *full_name, char *full_path, spa_t *spa, |
139 | uint64_t objsetid, struct dentry *root_dentry) | |
ebe7e575 | 140 | { |
278bee93 BB |
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); | |
24ef51f6 | 147 | se->se_spa = spa; |
278bee93 BB |
148 | se->se_objsetid = objsetid; |
149 | se->se_root_dentry = root_dentry; | |
48d3eb40 | 150 | se->se_taskqid = TASKQID_INVALID; |
278bee93 BB |
151 | |
152 | refcount_create(&se->se_refcount); | |
153 | ||
154 | return (se); | |
ebe7e575 BB |
155 | } |
156 | ||
278bee93 BB |
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) | |
ebe7e575 | 163 | { |
278bee93 BB |
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)); | |
ebe7e575 BB |
169 | } |
170 | ||
171 | /* | |
278bee93 | 172 | * Hold a reference on the zfs_snapentry_t. |
ebe7e575 BB |
173 | */ |
174 | static void | |
278bee93 | 175 | zfsctl_snapshot_hold(zfs_snapentry_t *se) |
ebe7e575 | 176 | { |
278bee93 BB |
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 | } | |
ebe7e575 | 190 | |
278bee93 BB |
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 | { | |
5ed27c57 | 199 | ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); |
278bee93 BB |
200 | refcount_add(&se->se_refcount, NULL); |
201 | avl_add(&zfs_snapshots_by_name, se); | |
202 | avl_add(&zfs_snapshots_by_objsetid, se); | |
ebe7e575 BB |
203 | } |
204 | ||
278bee93 BB |
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 | { | |
5ed27c57 | 214 | ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); |
278bee93 BB |
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) | |
ebe7e575 | 225 | { |
278bee93 BB |
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); | |
ebe7e575 BB |
231 | |
232 | if (ret < 0) | |
233 | return (-1); | |
234 | else if (ret > 0) | |
235 | return (1); | |
236 | else | |
237 | return (0); | |
238 | } | |
239 | ||
278bee93 BB |
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 | ||
24ef51f6 CC |
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 | ||
278bee93 BB |
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 | ||
5ed27c57 | 272 | ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock)); |
278bee93 BB |
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 * | |
24ef51f6 | 288 | zfsctl_snapshot_find_by_objsetid(spa_t *spa, uint64_t objsetid) |
278bee93 BB |
289 | { |
290 | zfs_snapentry_t *se, search; | |
291 | ||
5ed27c57 | 292 | ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock)); |
278bee93 | 293 | |
24ef51f6 | 294 | search.se_spa = spa; |
278bee93 BB |
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 | ||
5ed27c57 | 312 | ASSERT(RW_WRITE_HELD(&zfs_snapshot_lock)); |
278bee93 BB |
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; | |
24ef51f6 | 334 | spa_t *spa = se->se_spa; |
278bee93 BB |
335 | uint64_t objsetid = se->se_objsetid; |
336 | ||
5e94284f BB |
337 | if (zfs_expire_snapshot <= 0) { |
338 | zfsctl_snapshot_rele(se); | |
339 | return; | |
340 | } | |
341 | ||
48d3eb40 | 342 | se->se_taskqid = TASKQID_INVALID; |
278bee93 BB |
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 | */ | |
5ed27c57 | 350 | rw_enter(&zfs_snapshot_lock, RW_READER); |
24ef51f6 | 351 | if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) { |
278bee93 BB |
352 | zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot); |
353 | zfsctl_snapshot_rele(se); | |
354 | } | |
5ed27c57 | 355 | rw_exit(&zfs_snapshot_lock); |
278bee93 BB |
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 | { | |
5ed27c57 | 366 | ASSERT(RW_LOCK_HELD(&zfs_snapshot_lock)); |
278bee93 BB |
367 | |
368 | if (taskq_cancel_id(zfs_expire_taskq, se->se_taskqid) == 0) { | |
48d3eb40 | 369 | se->se_taskqid = TASKQID_INVALID; |
278bee93 BB |
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 | { | |
48d3eb40 | 380 | ASSERT3S(se->se_taskqid, ==, TASKQID_INVALID); |
278bee93 | 381 | |
5e94284f BB |
382 | if (delay <= 0) |
383 | return; | |
384 | ||
245b7ab3 | 385 | zfsctl_snapshot_hold(se); |
278bee93 BB |
386 | se->se_taskqid = taskq_dispatch_delay(zfs_expire_taskq, |
387 | snapentry_expire, se, TQ_SLEEP, ddi_get_lbolt() + delay * HZ); | |
278bee93 BB |
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 | |
24ef51f6 | 397 | zfsctl_snapshot_unmount_delay(spa_t *spa, uint64_t objsetid, int delay) |
278bee93 BB |
398 | { |
399 | zfs_snapentry_t *se; | |
400 | int error = ENOENT; | |
401 | ||
5ed27c57 | 402 | rw_enter(&zfs_snapshot_lock, RW_READER); |
24ef51f6 | 403 | if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) { |
278bee93 BB |
404 | zfsctl_snapshot_unmount_cancel(se); |
405 | zfsctl_snapshot_unmount_delay_impl(se, delay); | |
406 | zfsctl_snapshot_rele(se); | |
407 | error = 0; | |
408 | } | |
5ed27c57 | 409 | rw_exit(&zfs_snapshot_lock); |
278bee93 BB |
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 | ||
5ed27c57 | 424 | rw_enter(&zfs_snapshot_lock, RW_READER); |
278bee93 BB |
425 | if ((se = zfsctl_snapshot_find_by_name(snapname)) != NULL) { |
426 | zfsctl_snapshot_rele(se); | |
427 | ismounted = B_TRUE; | |
428 | } | |
5ed27c57 | 429 | rw_exit(&zfs_snapshot_lock); |
278bee93 BB |
430 | |
431 | return (ismounted); | |
432 | } | |
433 | ||
434 | /* | |
435 | * Check if the given inode is a part of the virtual .zfs directory. | |
436 | */ | |
ebe7e575 BB |
437 | boolean_t |
438 | zfsctl_is_node(struct inode *ip) | |
439 | { | |
440 | return (ITOZ(ip)->z_is_ctldir); | |
441 | } | |
442 | ||
278bee93 BB |
443 | /* |
444 | * Check if the given inode is a .zfs/snapshots/snapname directory. | |
445 | */ | |
ebe7e575 BB |
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; | |
ebe7e575 | 480 | zp->z_size = 0; |
ebe7e575 | 481 | zp->z_pflags = 0; |
ebe7e575 BB |
482 | zp->z_mode = 0; |
483 | zp->z_sync_cnt = 0; | |
ebe7e575 BB |
484 | zp->z_is_mapped = B_FALSE; |
485 | zp->z_is_ctldir = B_TRUE; | |
486 | zp->z_is_sa = B_FALSE; | |
7b3e34ba | 487 | zp->z_is_stale = B_FALSE; |
278f2236 | 488 | ip->i_generation = 0; |
ebe7e575 | 489 | ip->i_ino = id; |
f74b821a | 490 | ip->i_mode = (S_IFDIR | S_IRWXUGO); |
570d6edf RY |
491 | ip->i_uid = SUID_TO_KUID(0); |
492 | ip->i_gid = SGID_TO_KGID(0); | |
ebe7e575 BB |
493 | ip->i_blkbits = SPA_MINBLOCKSHIFT; |
494 | ip->i_atime = now; | |
495 | ip->i_mtime = now; | |
496 | ip->i_ctime = now; | |
497 | ip->i_fop = fops; | |
498 | ip->i_op = ops; | |
499 | ||
500 | if (insert_inode_locked(ip)) { | |
501 | unlock_new_inode(ip); | |
502 | iput(ip); | |
503 | return (NULL); | |
504 | } | |
505 | ||
506 | mutex_enter(&zsb->z_znodes_lock); | |
507 | list_insert_tail(&zsb->z_all_znodes, zp); | |
9ed86e7c | 508 | zsb->z_nr_znodes++; |
ebe7e575 BB |
509 | membar_producer(); |
510 | mutex_exit(&zsb->z_znodes_lock); | |
511 | ||
512 | unlock_new_inode(ip); | |
513 | ||
514 | return (ip); | |
515 | } | |
516 | ||
517 | /* | |
518 | * Lookup the inode with given id, it will be allocated if needed. | |
519 | */ | |
520 | static struct inode * | |
fc173c85 | 521 | zfsctl_inode_lookup(zfs_sb_t *zsb, uint64_t id, |
ebe7e575 BB |
522 | const struct file_operations *fops, const struct inode_operations *ops) |
523 | { | |
524 | struct inode *ip = NULL; | |
525 | ||
526 | while (ip == NULL) { | |
fc173c85 | 527 | ip = ilookup(zsb->z_sb, (unsigned long)id); |
ebe7e575 BB |
528 | if (ip) |
529 | break; | |
530 | ||
531 | /* May fail due to concurrent zfsctl_inode_alloc() */ | |
532 | ip = zfsctl_inode_alloc(zsb, id, fops, ops); | |
533 | } | |
534 | ||
535 | return (ip); | |
536 | } | |
537 | ||
ebe7e575 BB |
538 | /* |
539 | * Create the '.zfs' directory. This directory is cached as part of the VFS | |
540 | * structure. This results in a hold on the zfs_sb_t. The code in zfs_umount() | |
541 | * therefore checks against a vfs_count of 2 instead of 1. This reference | |
542 | * is removed when the ctldir is destroyed in the unmount. All other entities | |
543 | * under the '.zfs' directory are created dynamically as needed. | |
fc173c85 BB |
544 | * |
545 | * Because the dynamically created '.zfs' directory entries assume the use | |
546 | * of 64-bit inode numbers this support must be disabled on 32-bit systems. | |
ebe7e575 BB |
547 | */ |
548 | int | |
549 | zfsctl_create(zfs_sb_t *zsb) | |
550 | { | |
551 | ASSERT(zsb->z_ctldir == NULL); | |
552 | ||
553 | zsb->z_ctldir = zfsctl_inode_alloc(zsb, ZFSCTL_INO_ROOT, | |
554 | &zpl_fops_root, &zpl_ops_root); | |
555 | if (zsb->z_ctldir == NULL) | |
2e528b49 | 556 | return (SET_ERROR(ENOENT)); |
ebe7e575 BB |
557 | |
558 | return (0); | |
559 | } | |
560 | ||
561 | /* | |
278bee93 BB |
562 | * Destroy the '.zfs' directory or remove a snapshot from zfs_snapshots_by_name. |
563 | * Only called when the filesystem is unmounted. | |
ebe7e575 BB |
564 | */ |
565 | void | |
566 | zfsctl_destroy(zfs_sb_t *zsb) | |
567 | { | |
278bee93 BB |
568 | if (zsb->z_issnap) { |
569 | zfs_snapentry_t *se; | |
24ef51f6 | 570 | spa_t *spa = zsb->z_os->os_spa; |
278bee93 BB |
571 | uint64_t objsetid = dmu_objset_id(zsb->z_os); |
572 | ||
5ed27c57 | 573 | rw_enter(&zfs_snapshot_lock, RW_WRITER); |
24ef51f6 CC |
574 | if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) |
575 | != NULL) { | |
278bee93 BB |
576 | zfsctl_snapshot_unmount_cancel(se); |
577 | zfsctl_snapshot_remove(se); | |
578 | zfsctl_snapshot_rele(se); | |
579 | } | |
5ed27c57 | 580 | rw_exit(&zfs_snapshot_lock); |
278bee93 BB |
581 | } else if (zsb->z_ctldir) { |
582 | iput(zsb->z_ctldir); | |
583 | zsb->z_ctldir = NULL; | |
584 | } | |
ebe7e575 BB |
585 | } |
586 | ||
587 | /* | |
588 | * Given a root znode, retrieve the associated .zfs directory. | |
589 | * Add a hold to the vnode and return it. | |
590 | */ | |
591 | struct inode * | |
592 | zfsctl_root(znode_t *zp) | |
593 | { | |
594 | ASSERT(zfs_has_ctldir(zp)); | |
595 | igrab(ZTOZSB(zp)->z_ctldir); | |
596 | return (ZTOZSB(zp)->z_ctldir); | |
597 | } | |
0500e835 BB |
598 | /* |
599 | * Generate a long fid which includes the root object and objset of a | |
600 | * snapshot but not the generation number. For the root object the | |
601 | * generation number is ignored when zero to avoid needing to open | |
602 | * the dataset when generating fids for the snapshot names. | |
603 | */ | |
604 | static int | |
605 | zfsctl_snapdir_fid(struct inode *ip, fid_t *fidp) | |
606 | { | |
607 | zfs_sb_t *zsb = ITOZSB(ip); | |
608 | zfid_short_t *zfid = (zfid_short_t *)fidp; | |
609 | zfid_long_t *zlfid = (zfid_long_t *)fidp; | |
610 | uint32_t gen = 0; | |
611 | uint64_t object; | |
612 | uint64_t objsetid; | |
613 | int i; | |
614 | ||
615 | object = zsb->z_root; | |
616 | objsetid = ZFSCTL_INO_SNAPDIRS - ip->i_ino; | |
617 | zfid->zf_len = LONG_FID_LEN; | |
618 | ||
619 | for (i = 0; i < sizeof (zfid->zf_object); i++) | |
620 | zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); | |
621 | ||
622 | for (i = 0; i < sizeof (zfid->zf_gen); i++) | |
623 | zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); | |
624 | ||
625 | for (i = 0; i < sizeof (zlfid->zf_setid); i++) | |
626 | zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); | |
627 | ||
628 | for (i = 0; i < sizeof (zlfid->zf_setgen); i++) | |
629 | zlfid->zf_setgen[i] = 0; | |
630 | ||
631 | return (0); | |
632 | } | |
ebe7e575 | 633 | |
0500e835 BB |
634 | /* |
635 | * Generate an appropriate fid for an entry in the .zfs directory. | |
636 | */ | |
ebe7e575 BB |
637 | int |
638 | zfsctl_fid(struct inode *ip, fid_t *fidp) | |
639 | { | |
640 | znode_t *zp = ITOZ(ip); | |
641 | zfs_sb_t *zsb = ITOZSB(ip); | |
642 | uint64_t object = zp->z_id; | |
643 | zfid_short_t *zfid; | |
644 | int i; | |
645 | ||
646 | ZFS_ENTER(zsb); | |
647 | ||
648 | if (fidp->fid_len < SHORT_FID_LEN) { | |
649 | fidp->fid_len = SHORT_FID_LEN; | |
650 | ZFS_EXIT(zsb); | |
2e528b49 | 651 | return (SET_ERROR(ENOSPC)); |
ebe7e575 BB |
652 | } |
653 | ||
0500e835 BB |
654 | if (zfsctl_is_snapdir(ip)) { |
655 | ZFS_EXIT(zsb); | |
656 | return (zfsctl_snapdir_fid(ip, fidp)); | |
657 | } | |
658 | ||
ebe7e575 BB |
659 | zfid = (zfid_short_t *)fidp; |
660 | ||
661 | zfid->zf_len = SHORT_FID_LEN; | |
662 | ||
663 | for (i = 0; i < sizeof (zfid->zf_object); i++) | |
664 | zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); | |
665 | ||
666 | /* .zfs znodes always have a generation number of 0 */ | |
667 | for (i = 0; i < sizeof (zfid->zf_gen); i++) | |
668 | zfid->zf_gen[i] = 0; | |
669 | ||
670 | ZFS_EXIT(zsb); | |
671 | return (0); | |
672 | } | |
673 | ||
278bee93 BB |
674 | /* |
675 | * Construct a full dataset name in full_name: "pool/dataset@snap_name" | |
676 | */ | |
ebe7e575 | 677 | static int |
278bee93 BB |
678 | zfsctl_snapshot_name(zfs_sb_t *zsb, const char *snap_name, int len, |
679 | char *full_name) | |
ebe7e575 | 680 | { |
278bee93 | 681 | objset_t *os = zsb->z_os; |
ebe7e575 | 682 | |
278bee93 | 683 | if (zfs_component_namecheck(snap_name, NULL, NULL) != 0) |
2e528b49 | 684 | return (SET_ERROR(EILSEQ)); |
ebe7e575 | 685 | |
278bee93 BB |
686 | dmu_objset_name(os, full_name); |
687 | if ((strlen(full_name) + 1 + strlen(snap_name)) >= len) | |
2e528b49 | 688 | return (SET_ERROR(ENAMETOOLONG)); |
ebe7e575 | 689 | |
278bee93 BB |
690 | (void) strcat(full_name, "@"); |
691 | (void) strcat(full_name, snap_name); | |
ebe7e575 BB |
692 | |
693 | return (0); | |
694 | } | |
695 | ||
e49f1e20 | 696 | /* |
278bee93 | 697 | * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/" |
e49f1e20 | 698 | */ |
ebe7e575 | 699 | static int |
278bee93 | 700 | zfsctl_snapshot_path(struct path *path, int len, char *full_path) |
ebe7e575 BB |
701 | { |
702 | char *path_buffer, *path_ptr; | |
703 | int path_len, error = 0; | |
704 | ||
705 | path_buffer = kmem_alloc(len, KM_SLEEP); | |
706 | ||
707 | path_ptr = d_path(path, path_buffer, len); | |
708 | if (IS_ERR(path_ptr)) { | |
709 | error = -PTR_ERR(path_ptr); | |
710 | goto out; | |
711 | } | |
712 | ||
713 | path_len = path_buffer + len - 1 - path_ptr; | |
714 | if (path_len > len) { | |
2e528b49 | 715 | error = SET_ERROR(EFAULT); |
ebe7e575 BB |
716 | goto out; |
717 | } | |
718 | ||
278bee93 BB |
719 | memcpy(full_path, path_ptr, path_len); |
720 | full_path[path_len] = '\0'; | |
ebe7e575 BB |
721 | out: |
722 | kmem_free(path_buffer, len); | |
723 | ||
724 | return (error); | |
725 | } | |
726 | ||
0500e835 BB |
727 | /* |
728 | * Returns full path in full_path: "/pool/dataset/.zfs/snapshot/snap_name/" | |
729 | */ | |
730 | static int | |
731 | zfsctl_snapshot_path_objset(zfs_sb_t *zsb, uint64_t objsetid, | |
732 | int path_len, char *full_path) | |
733 | { | |
734 | objset_t *os = zsb->z_os; | |
735 | fstrans_cookie_t cookie; | |
736 | char *snapname; | |
737 | boolean_t case_conflict; | |
738 | uint64_t id, pos = 0; | |
739 | int error = 0; | |
740 | ||
741 | if (zsb->z_mntopts->z_mntpoint == NULL) | |
742 | return (ENOENT); | |
743 | ||
744 | cookie = spl_fstrans_mark(); | |
eca7b760 | 745 | snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
0500e835 BB |
746 | |
747 | while (error == 0) { | |
748 | dsl_pool_config_enter(dmu_objset_pool(os), FTAG); | |
eca7b760 IK |
749 | error = dmu_snapshot_list_next(zsb->z_os, |
750 | ZFS_MAX_DATASET_NAME_LEN, snapname, &id, &pos, | |
751 | &case_conflict); | |
0500e835 BB |
752 | dsl_pool_config_exit(dmu_objset_pool(os), FTAG); |
753 | if (error) | |
754 | goto out; | |
755 | ||
756 | if (id == objsetid) | |
757 | break; | |
758 | } | |
759 | ||
760 | memset(full_path, 0, path_len); | |
761 | snprintf(full_path, path_len - 1, "%s/.zfs/snapshot/%s", | |
762 | zsb->z_mntopts->z_mntpoint, snapname); | |
763 | out: | |
eca7b760 | 764 | kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN); |
0500e835 BB |
765 | spl_fstrans_unmark(cookie); |
766 | ||
767 | return (error); | |
768 | } | |
769 | ||
ebe7e575 BB |
770 | /* |
771 | * Special case the handling of "..". | |
772 | */ | |
ebe7e575 BB |
773 | int |
774 | zfsctl_root_lookup(struct inode *dip, char *name, struct inode **ipp, | |
775 | int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) | |
776 | { | |
777 | zfs_sb_t *zsb = ITOZSB(dip); | |
778 | int error = 0; | |
779 | ||
780 | ZFS_ENTER(zsb); | |
781 | ||
782 | if (strcmp(name, "..") == 0) { | |
783 | *ipp = dip->i_sb->s_root->d_inode; | |
784 | } else if (strcmp(name, ZFS_SNAPDIR_NAME) == 0) { | |
785 | *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIR, | |
786 | &zpl_fops_snapdir, &zpl_ops_snapdir); | |
787 | } else if (strcmp(name, ZFS_SHAREDIR_NAME) == 0) { | |
788 | *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SHARES, | |
789 | &zpl_fops_shares, &zpl_ops_shares); | |
790 | } else { | |
791 | *ipp = NULL; | |
792 | } | |
793 | ||
794 | if (*ipp == NULL) | |
2e528b49 | 795 | error = SET_ERROR(ENOENT); |
ebe7e575 BB |
796 | |
797 | ZFS_EXIT(zsb); | |
798 | ||
799 | return (error); | |
800 | } | |
801 | ||
802 | /* | |
803 | * Lookup entry point for the 'snapshot' directory. Try to open the | |
804 | * snapshot if it exist, creating the pseudo filesystem inode as necessary. | |
805 | * Perform a mount of the associated dataset on top of the inode. | |
806 | */ | |
ebe7e575 BB |
807 | int |
808 | zfsctl_snapdir_lookup(struct inode *dip, char *name, struct inode **ipp, | |
809 | int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) | |
810 | { | |
811 | zfs_sb_t *zsb = ITOZSB(dip); | |
812 | uint64_t id; | |
813 | int error; | |
814 | ||
815 | ZFS_ENTER(zsb); | |
816 | ||
6772fb67 | 817 | error = dmu_snapshot_lookup(zsb->z_os, name, &id); |
ebe7e575 BB |
818 | if (error) { |
819 | ZFS_EXIT(zsb); | |
820 | return (error); | |
821 | } | |
822 | ||
823 | *ipp = zfsctl_inode_lookup(zsb, ZFSCTL_INO_SNAPDIRS - id, | |
824 | &simple_dir_operations, &simple_dir_inode_operations); | |
278bee93 | 825 | if (*ipp == NULL) |
2e528b49 | 826 | error = SET_ERROR(ENOENT); |
ebe7e575 BB |
827 | |
828 | ZFS_EXIT(zsb); | |
829 | ||
830 | return (error); | |
831 | } | |
832 | ||
ebe7e575 BB |
833 | /* |
834 | * Renaming a directory under '.zfs/snapshot' will automatically trigger | |
835 | * a rename of the snapshot to the new given name. The rename is confined | |
836 | * to the '.zfs/snapshot' directory snapshots cannot be moved elsewhere. | |
837 | */ | |
ebe7e575 | 838 | int |
13fe0198 MA |
839 | zfsctl_snapdir_rename(struct inode *sdip, char *snm, |
840 | struct inode *tdip, char *tnm, cred_t *cr, int flags) | |
ebe7e575 BB |
841 | { |
842 | zfs_sb_t *zsb = ITOZSB(sdip); | |
13fe0198 | 843 | char *to, *from, *real, *fsname; |
ebe7e575 BB |
844 | int error; |
845 | ||
0500e835 BB |
846 | if (!zfs_admin_snapshot) |
847 | return (EACCES); | |
848 | ||
ebe7e575 BB |
849 | ZFS_ENTER(zsb); |
850 | ||
eca7b760 IK |
851 | to = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
852 | from = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); | |
853 | real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); | |
854 | fsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); | |
ebe7e575 BB |
855 | |
856 | if (zsb->z_case == ZFS_CASE_INSENSITIVE) { | |
13fe0198 | 857 | error = dmu_snapshot_realname(zsb->z_os, snm, real, |
eca7b760 | 858 | ZFS_MAX_DATASET_NAME_LEN, NULL); |
ebe7e575 | 859 | if (error == 0) { |
13fe0198 | 860 | snm = real; |
ebe7e575 BB |
861 | } else if (error != ENOTSUP) { |
862 | goto out; | |
863 | } | |
864 | } | |
865 | ||
13fe0198 MA |
866 | dmu_objset_name(zsb->z_os, fsname); |
867 | ||
eca7b760 IK |
868 | error = zfsctl_snapshot_name(ITOZSB(sdip), snm, |
869 | ZFS_MAX_DATASET_NAME_LEN, from); | |
13fe0198 | 870 | if (error == 0) |
eca7b760 IK |
871 | error = zfsctl_snapshot_name(ITOZSB(tdip), tnm, |
872 | ZFS_MAX_DATASET_NAME_LEN, to); | |
13fe0198 | 873 | if (error == 0) |
ebe7e575 | 874 | error = zfs_secpolicy_rename_perms(from, to, cr); |
13fe0198 | 875 | if (error != 0) |
ebe7e575 BB |
876 | goto out; |
877 | ||
878 | /* | |
879 | * Cannot move snapshots out of the snapdir. | |
880 | */ | |
881 | if (sdip != tdip) { | |
2e528b49 | 882 | error = SET_ERROR(EINVAL); |
ebe7e575 BB |
883 | goto out; |
884 | } | |
885 | ||
886 | /* | |
887 | * No-op when names are identical. | |
888 | */ | |
13fe0198 | 889 | if (strcmp(snm, tnm) == 0) { |
ebe7e575 BB |
890 | error = 0; |
891 | goto out; | |
892 | } | |
893 | ||
5ed27c57 | 894 | rw_enter(&zfs_snapshot_lock, RW_WRITER); |
ebe7e575 | 895 | |
13fe0198 | 896 | error = dsl_dataset_rename_snapshot(fsname, snm, tnm, B_FALSE); |
278bee93 BB |
897 | if (error == 0) |
898 | (void) zfsctl_snapshot_rename(snm, tnm); | |
ebe7e575 | 899 | |
5ed27c57 | 900 | rw_exit(&zfs_snapshot_lock); |
ebe7e575 | 901 | out: |
eca7b760 IK |
902 | kmem_free(from, ZFS_MAX_DATASET_NAME_LEN); |
903 | kmem_free(to, ZFS_MAX_DATASET_NAME_LEN); | |
904 | kmem_free(real, ZFS_MAX_DATASET_NAME_LEN); | |
905 | kmem_free(fsname, ZFS_MAX_DATASET_NAME_LEN); | |
ebe7e575 BB |
906 | |
907 | ZFS_EXIT(zsb); | |
908 | ||
909 | return (error); | |
910 | } | |
911 | ||
912 | /* | |
913 | * Removing a directory under '.zfs/snapshot' will automatically trigger | |
914 | * the removal of the snapshot with the given name. | |
915 | */ | |
ebe7e575 BB |
916 | int |
917 | zfsctl_snapdir_remove(struct inode *dip, char *name, cred_t *cr, int flags) | |
918 | { | |
919 | zfs_sb_t *zsb = ITOZSB(dip); | |
920 | char *snapname, *real; | |
921 | int error; | |
922 | ||
0500e835 BB |
923 | if (!zfs_admin_snapshot) |
924 | return (EACCES); | |
925 | ||
ebe7e575 BB |
926 | ZFS_ENTER(zsb); |
927 | ||
eca7b760 IK |
928 | snapname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
929 | real = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); | |
ebe7e575 BB |
930 | |
931 | if (zsb->z_case == ZFS_CASE_INSENSITIVE) { | |
932 | error = dmu_snapshot_realname(zsb->z_os, name, real, | |
eca7b760 | 933 | ZFS_MAX_DATASET_NAME_LEN, NULL); |
ebe7e575 BB |
934 | if (error == 0) { |
935 | name = real; | |
936 | } else if (error != ENOTSUP) { | |
937 | goto out; | |
938 | } | |
939 | } | |
940 | ||
eca7b760 IK |
941 | error = zfsctl_snapshot_name(ITOZSB(dip), name, |
942 | ZFS_MAX_DATASET_NAME_LEN, snapname); | |
13fe0198 | 943 | if (error == 0) |
ebe7e575 | 944 | error = zfs_secpolicy_destroy_perms(snapname, cr); |
13fe0198 | 945 | if (error != 0) |
ebe7e575 BB |
946 | goto out; |
947 | ||
278bee93 | 948 | error = zfsctl_snapshot_unmount(snapname, MNT_FORCE); |
ebe7e575 | 949 | if ((error == 0) || (error == ENOENT)) |
13fe0198 | 950 | error = dsl_destroy_snapshot(snapname, B_FALSE); |
ebe7e575 | 951 | out: |
eca7b760 IK |
952 | kmem_free(snapname, ZFS_MAX_DATASET_NAME_LEN); |
953 | kmem_free(real, ZFS_MAX_DATASET_NAME_LEN); | |
ebe7e575 BB |
954 | |
955 | ZFS_EXIT(zsb); | |
956 | ||
957 | return (error); | |
958 | } | |
959 | ||
960 | /* | |
961 | * Creating a directory under '.zfs/snapshot' will automatically trigger | |
962 | * the creation of a new snapshot with the given name. | |
963 | */ | |
ebe7e575 BB |
964 | int |
965 | zfsctl_snapdir_mkdir(struct inode *dip, char *dirname, vattr_t *vap, | |
966 | struct inode **ipp, cred_t *cr, int flags) | |
967 | { | |
968 | zfs_sb_t *zsb = ITOZSB(dip); | |
969 | char *dsname; | |
970 | int error; | |
971 | ||
0500e835 BB |
972 | if (!zfs_admin_snapshot) |
973 | return (EACCES); | |
974 | ||
eca7b760 | 975 | dsname = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
ebe7e575 | 976 | |
da536844 | 977 | if (zfs_component_namecheck(dirname, NULL, NULL) != 0) { |
2e528b49 | 978 | error = SET_ERROR(EILSEQ); |
ebe7e575 BB |
979 | goto out; |
980 | } | |
981 | ||
982 | dmu_objset_name(zsb->z_os, dsname); | |
983 | ||
984 | error = zfs_secpolicy_snapshot_perms(dsname, cr); | |
13fe0198 | 985 | if (error != 0) |
ebe7e575 BB |
986 | goto out; |
987 | ||
988 | if (error == 0) { | |
6f1ffb06 | 989 | error = dmu_objset_snapshot_one(dsname, dirname); |
13fe0198 | 990 | if (error != 0) |
ebe7e575 BB |
991 | goto out; |
992 | ||
993 | error = zfsctl_snapdir_lookup(dip, dirname, ipp, | |
994 | 0, cr, NULL, NULL); | |
995 | } | |
996 | out: | |
eca7b760 | 997 | kmem_free(dsname, ZFS_MAX_DATASET_NAME_LEN); |
ebe7e575 BB |
998 | |
999 | return (error); | |
1000 | } | |
1001 | ||
ebe7e575 BB |
1002 | /* |
1003 | * Attempt to unmount a snapshot by making a call to user space. | |
1004 | * There is no assurance that this can or will succeed, is just a | |
1005 | * best effort. In the case where it does fail, perhaps because | |
1006 | * it's in use, the unmount will fail harmlessly. | |
1007 | */ | |
278bee93 BB |
1008 | int |
1009 | zfsctl_snapshot_unmount(char *snapname, int flags) | |
ebe7e575 | 1010 | { |
5dc1ff29 SE |
1011 | char *argv[] = { "/usr/bin/env", "umount", "-t", "zfs", "-n", NULL, |
1012 | NULL }; | |
ebe7e575 | 1013 | char *envp[] = { NULL }; |
278bee93 | 1014 | zfs_snapentry_t *se; |
ebe7e575 BB |
1015 | int error; |
1016 | ||
5ed27c57 | 1017 | rw_enter(&zfs_snapshot_lock, RW_READER); |
278bee93 | 1018 | if ((se = zfsctl_snapshot_find_by_name(snapname)) == NULL) { |
5ed27c57 | 1019 | rw_exit(&zfs_snapshot_lock); |
278bee93 BB |
1020 | return (ENOENT); |
1021 | } | |
5ed27c57 | 1022 | rw_exit(&zfs_snapshot_lock); |
278bee93 | 1023 | |
5dc1ff29 SE |
1024 | if (flags & MNT_FORCE) |
1025 | argv[4] = "-fn"; | |
1026 | argv[5] = se->se_path; | |
278bee93 | 1027 | dprintf("unmount; path=%s\n", se->se_path); |
761394b3 | 1028 | error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); |
00b65db7 | 1029 | zfsctl_snapshot_rele(se); |
ebe7e575 | 1030 | |
278bee93 | 1031 | |
ebe7e575 BB |
1032 | /* |
1033 | * The umount system utility will return 256 on error. We must | |
1034 | * assume this error is because the file system is busy so it is | |
1035 | * converted to the more sensible EBUSY. | |
1036 | */ | |
1037 | if (error) | |
2e528b49 | 1038 | error = SET_ERROR(EBUSY); |
ebe7e575 | 1039 | |
ebe7e575 BB |
1040 | return (error); |
1041 | } | |
1042 | ||
fd4f7616 | 1043 | #define MOUNT_BUSY 0x80 /* Mount failed due to EBUSY (from mntent.h) */ |
ebe7e575 BB |
1044 | |
1045 | int | |
278bee93 | 1046 | zfsctl_snapshot_mount(struct path *path, int flags) |
ebe7e575 BB |
1047 | { |
1048 | struct dentry *dentry = path->dentry; | |
1049 | struct inode *ip = dentry->d_inode; | |
278bee93 BB |
1050 | zfs_sb_t *zsb; |
1051 | zfs_sb_t *snap_zsb; | |
1052 | zfs_snapentry_t *se; | |
ebe7e575 | 1053 | char *full_name, *full_path; |
5dc1ff29 SE |
1054 | char *argv[] = { "/usr/bin/env", "mount", "-t", "zfs", "-n", NULL, NULL, |
1055 | NULL }; | |
ebe7e575 BB |
1056 | char *envp[] = { NULL }; |
1057 | int error; | |
d287880a | 1058 | struct path spath; |
ebe7e575 | 1059 | |
278bee93 BB |
1060 | if (ip == NULL) |
1061 | return (EISDIR); | |
1062 | ||
1063 | zsb = ITOZSB(ip); | |
ebe7e575 BB |
1064 | ZFS_ENTER(zsb); |
1065 | ||
eca7b760 | 1066 | full_name = kmem_zalloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
278bee93 | 1067 | full_path = kmem_zalloc(MAXPATHLEN, KM_SLEEP); |
ebe7e575 | 1068 | |
278bee93 | 1069 | error = zfsctl_snapshot_name(zsb, dname(dentry), |
eca7b760 | 1070 | ZFS_MAX_DATASET_NAME_LEN, full_name); |
ebe7e575 BB |
1071 | if (error) |
1072 | goto error; | |
1073 | ||
278bee93 | 1074 | error = zfsctl_snapshot_path(path, MAXPATHLEN, full_path); |
ebe7e575 BB |
1075 | if (error) |
1076 | goto error; | |
1077 | ||
278bee93 BB |
1078 | /* |
1079 | * Multiple concurrent automounts of a snapshot are never allowed. | |
1080 | * The snapshot may be manually mounted as many times as desired. | |
1081 | */ | |
1082 | if (zfsctl_snapshot_ismounted(full_name)) { | |
19976601 | 1083 | error = 0; |
278bee93 BB |
1084 | goto error; |
1085 | } | |
1086 | ||
ebe7e575 BB |
1087 | /* |
1088 | * Attempt to mount the snapshot from user space. Normally this | |
1089 | * would be done using the vfs_kern_mount() function, however that | |
1090 | * function is marked GPL-only and cannot be used. On error we | |
1091 | * careful to log the real error to the console and return EISDIR | |
1092 | * to safely abort the automount. This should be very rare. | |
fd4f7616 TC |
1093 | * |
1094 | * If the user mode helper happens to return EBUSY, a concurrent | |
1095 | * mount is already in progress in which case the error is ignored. | |
1096 | * Take note that if the program was executed successfully the return | |
1097 | * value from call_usermodehelper() will be (exitcode << 8 + signal). | |
ebe7e575 | 1098 | */ |
278bee93 | 1099 | dprintf("mount; name=%s path=%s\n", full_name, full_path); |
5dc1ff29 SE |
1100 | argv[5] = full_name; |
1101 | argv[6] = full_path; | |
761394b3 | 1102 | error = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); |
d287880a CC |
1103 | if (error) { |
1104 | if (!(error & MOUNT_BUSY << 8)) { | |
1105 | cmn_err(CE_WARN, "Unable to automount %s/%s: %d", | |
1106 | full_path, full_name, error); | |
1107 | error = SET_ERROR(EISDIR); | |
1108 | } else { | |
1109 | /* | |
1110 | * EBUSY, this could mean a concurrent mount, or the | |
1111 | * snapshot has already been mounted at completely | |
1112 | * different place. We return 0 so VFS will retry. For | |
1113 | * the latter case the VFS will retry several times | |
1114 | * and return ELOOP, which is probably not a very good | |
1115 | * behavior. | |
1116 | */ | |
1117 | error = 0; | |
1118 | } | |
ebe7e575 BB |
1119 | goto error; |
1120 | } | |
1121 | ||
ebe7e575 | 1122 | /* |
278bee93 BB |
1123 | * Follow down in to the mounted snapshot and set MNT_SHRINKABLE |
1124 | * to identify this as an automounted filesystem. | |
ebe7e575 | 1125 | */ |
d287880a CC |
1126 | spath = *path; |
1127 | path_get(&spath); | |
1128 | if (zpl_follow_down_one(&spath)) { | |
1129 | snap_zsb = ITOZSB(spath.dentry->d_inode); | |
1130 | snap_zsb->z_parent = zsb; | |
1131 | dentry = spath.dentry; | |
1132 | spath.mnt->mnt_flags |= MNT_SHRINKABLE; | |
ebe7e575 | 1133 | |
5ed27c57 | 1134 | rw_enter(&zfs_snapshot_lock, RW_WRITER); |
d287880a | 1135 | se = zfsctl_snapshot_alloc(full_name, full_path, |
24ef51f6 CC |
1136 | snap_zsb->z_os->os_spa, dmu_objset_id(snap_zsb->z_os), |
1137 | dentry); | |
d287880a CC |
1138 | zfsctl_snapshot_add(se); |
1139 | zfsctl_snapshot_unmount_delay_impl(se, zfs_expire_snapshot); | |
5ed27c57 | 1140 | rw_exit(&zfs_snapshot_lock); |
d287880a CC |
1141 | } |
1142 | path_put(&spath); | |
ebe7e575 | 1143 | error: |
eca7b760 | 1144 | kmem_free(full_name, ZFS_MAX_DATASET_NAME_LEN); |
278bee93 | 1145 | kmem_free(full_path, MAXPATHLEN); |
ebe7e575 BB |
1146 | |
1147 | ZFS_EXIT(zsb); | |
1148 | ||
1149 | return (error); | |
1150 | } | |
1151 | ||
1152 | /* | |
278bee93 | 1153 | * Given the objset id of the snapshot return its zfs_sb_t as zsbp. |
ebe7e575 | 1154 | */ |
ebe7e575 BB |
1155 | int |
1156 | zfsctl_lookup_objset(struct super_block *sb, uint64_t objsetid, zfs_sb_t **zsbp) | |
1157 | { | |
278bee93 | 1158 | zfs_snapentry_t *se; |
ebe7e575 | 1159 | int error; |
24ef51f6 | 1160 | spa_t *spa = ((zfs_sb_t *)(sb->s_fs_info))->z_os->os_spa; |
ebe7e575 | 1161 | |
ebe7e575 | 1162 | /* |
278bee93 BB |
1163 | * Verify that the snapshot is mounted then lookup the mounted root |
1164 | * rather than the covered mount point. This may fail if the | |
1165 | * snapshot has just been unmounted by an unrelated user space | |
1166 | * process. This race cannot occur to an expired mount point | |
1167 | * because we hold the zfs_snapshot_lock to prevent the race. | |
ebe7e575 | 1168 | */ |
5ed27c57 | 1169 | rw_enter(&zfs_snapshot_lock, RW_READER); |
24ef51f6 | 1170 | if ((se = zfsctl_snapshot_find_by_objsetid(spa, objsetid)) != NULL) { |
d4787d55 BB |
1171 | zfs_sb_t *zsb; |
1172 | ||
1173 | zsb = ITOZSB(se->se_root_dentry->d_inode); | |
1174 | ASSERT3U(dmu_objset_id(zsb->z_os), ==, objsetid); | |
1175 | ||
1176 | if (time_after(jiffies, zsb->z_snap_defer_time + | |
1177 | MAX(zfs_expire_snapshot * HZ / 2, HZ))) { | |
1178 | zsb->z_snap_defer_time = jiffies; | |
f22f900f BB |
1179 | zfsctl_snapshot_unmount_cancel(se); |
1180 | zfsctl_snapshot_unmount_delay_impl(se, | |
d4787d55 BB |
1181 | zfs_expire_snapshot); |
1182 | } | |
1183 | ||
1184 | *zsbp = zsb; | |
278bee93 BB |
1185 | zfsctl_snapshot_rele(se); |
1186 | error = SET_ERROR(0); | |
ebe7e575 | 1187 | } else { |
278bee93 | 1188 | error = SET_ERROR(ENOENT); |
ebe7e575 | 1189 | } |
5ed27c57 | 1190 | rw_exit(&zfs_snapshot_lock); |
ebe7e575 | 1191 | |
0500e835 BB |
1192 | /* |
1193 | * Automount the snapshot given the objset id by constructing the | |
1194 | * full mount point and performing a traversal. | |
1195 | */ | |
1196 | if (error == ENOENT) { | |
1197 | struct path path; | |
1198 | char *mnt; | |
1199 | ||
1200 | mnt = kmem_alloc(MAXPATHLEN, KM_SLEEP); | |
1201 | error = zfsctl_snapshot_path_objset(sb->s_fs_info, objsetid, | |
1202 | MAXPATHLEN, mnt); | |
1203 | if (error) { | |
1204 | kmem_free(mnt, MAXPATHLEN); | |
1205 | return (SET_ERROR(error)); | |
1206 | } | |
1207 | ||
1208 | error = kern_path(mnt, LOOKUP_FOLLOW|LOOKUP_DIRECTORY, &path); | |
1209 | if (error == 0) { | |
1210 | *zsbp = ITOZSB(path.dentry->d_inode); | |
1211 | path_put(&path); | |
1212 | } | |
1213 | ||
1214 | kmem_free(mnt, MAXPATHLEN); | |
1215 | } | |
1216 | ||
ebe7e575 BB |
1217 | return (error); |
1218 | } | |
1219 | ||
ebe7e575 BB |
1220 | int |
1221 | zfsctl_shares_lookup(struct inode *dip, char *name, struct inode **ipp, | |
1222 | int flags, cred_t *cr, int *direntflags, pathname_t *realpnp) | |
1223 | { | |
1224 | zfs_sb_t *zsb = ITOZSB(dip); | |
1225 | struct inode *ip; | |
1226 | znode_t *dzp; | |
1227 | int error; | |
1228 | ||
1229 | ZFS_ENTER(zsb); | |
1230 | ||
1231 | if (zsb->z_shares_dir == 0) { | |
1232 | ZFS_EXIT(zsb); | |
2e528b49 | 1233 | return (SET_ERROR(ENOTSUP)); |
ebe7e575 BB |
1234 | } |
1235 | ||
8adb798a DM |
1236 | if ((error = zfs_zget(zsb, zsb->z_shares_dir, &dzp)) == 0) { |
1237 | error = zfs_lookup(ZTOI(dzp), name, &ip, 0, cr, NULL, NULL); | |
1238 | iput(ZTOI(dzp)); | |
ebe7e575 BB |
1239 | } |
1240 | ||
ebe7e575 BB |
1241 | ZFS_EXIT(zsb); |
1242 | ||
1243 | return (error); | |
1244 | } | |
1245 | ||
ebe7e575 BB |
1246 | /* |
1247 | * Initialize the various pieces we'll need to create and manipulate .zfs | |
1248 | * directories. Currently this is unused but available. | |
1249 | */ | |
1250 | void | |
1251 | zfsctl_init(void) | |
1252 | { | |
278bee93 BB |
1253 | avl_create(&zfs_snapshots_by_name, snapentry_compare_by_name, |
1254 | sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, | |
1255 | se_node_name)); | |
1256 | avl_create(&zfs_snapshots_by_objsetid, snapentry_compare_by_objsetid, | |
1257 | sizeof (zfs_snapentry_t), offsetof(zfs_snapentry_t, | |
1258 | se_node_objsetid)); | |
5ed27c57 | 1259 | rw_init(&zfs_snapshot_lock, NULL, RW_DEFAULT, NULL); |
278bee93 | 1260 | |
1229323d | 1261 | zfs_expire_taskq = taskq_create("z_unmount", 1, defclsyspri, |
2ae10319 | 1262 | 1, 8, TASKQ_PREPOPULATE); |
ebe7e575 BB |
1263 | } |
1264 | ||
1265 | /* | |
1266 | * Cleanup the various pieces we needed for .zfs directories. In particular | |
1267 | * ensure the expiry timer is canceled safely. | |
1268 | */ | |
1269 | void | |
1270 | zfsctl_fini(void) | |
1271 | { | |
2ae10319 | 1272 | taskq_destroy(zfs_expire_taskq); |
278bee93 BB |
1273 | |
1274 | avl_destroy(&zfs_snapshots_by_name); | |
1275 | avl_destroy(&zfs_snapshots_by_objsetid); | |
5ed27c57 | 1276 | rw_destroy(&zfs_snapshot_lock); |
ebe7e575 BB |
1277 | } |
1278 | ||
0500e835 BB |
1279 | module_param(zfs_admin_snapshot, int, 0644); |
1280 | MODULE_PARM_DESC(zfs_admin_snapshot, "Enable mkdir/rmdir/mv in .zfs/snapshot"); | |
1281 | ||
ebe7e575 BB |
1282 | module_param(zfs_expire_snapshot, int, 0644); |
1283 | MODULE_PARM_DESC(zfs_expire_snapshot, "Seconds to expire .zfs/snapshot"); |