1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * CDDL HEADER START
4 *
5 * The contents of this file are subject to the terms of the
6 * Common Development and Distribution License (the "License").
7 * You may not use this file except in compliance with the License.
8 *
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or https://opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22 /*
23 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
24 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
25 * LLNL-CODE-403049.
26 * Rewritten for Linux by:
27 * Rohan Puri <rohan.puri15@gmail.com>
28 * Brian Behlendorf <behlendorf1@llnl.gov>
29 */
30
31 #include <sys/zfs_znode.h>
32 #include <sys/zfs_vfsops.h>
33 #include <sys/zfs_vnops.h>
34 #include <sys/zfs_ctldir.h>
35 #include <sys/zpl.h>
36 #include <sys/dmu.h>
37 #include <sys/dsl_dataset.h>
38 #include <sys/zap.h>
39
40 /*
41 * Common open routine. Disallow any write access.
42 */
43 static int
zpl_common_open(struct inode * ip,struct file * filp)44 zpl_common_open(struct inode *ip, struct file *filp)
45 {
46 if (blk_mode_is_open_write(filp->f_mode))
47 return (-EACCES);
48
49 return (generic_file_open(ip, filp));
50 }
51
52 /*
53 * Get root directory contents.
54 */
55 static int
zpl_root_iterate(struct file * filp,struct dir_context * ctx)56 zpl_root_iterate(struct file *filp, struct dir_context *ctx)
57 {
58 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
59 int error = 0;
60
61 if (zfsvfs->z_show_ctldir == ZFS_SNAPDIR_DISABLED) {
62 return (SET_ERROR(ENOENT));
63 }
64
65 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
66 return (error);
67
68 if (!dir_emit_dots(filp, ctx))
69 goto out;
70
71 if (ctx->pos == 2) {
72 if (!dir_emit(ctx, ZFS_SNAPDIR_NAME,
73 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))
74 goto out;
75
76 ctx->pos++;
77 }
78
79 if (ctx->pos == 3) {
80 if (!dir_emit(ctx, ZFS_SHAREDIR_NAME,
81 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))
82 goto out;
83
84 ctx->pos++;
85 }
86 out:
87 zpl_exit(zfsvfs, FTAG);
88
89 return (error);
90 }
91
92 /*
93 * Get root directory attributes.
94 */
95 static int
96 #ifdef HAVE_IDMAP_IOPS_GETATTR
zpl_root_getattr_impl(struct mnt_idmap * user_ns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)97 zpl_root_getattr_impl(struct mnt_idmap *user_ns,
98 const struct path *path, struct kstat *stat, u32 request_mask,
99 unsigned int query_flags)
100 #elif defined(HAVE_USERNS_IOPS_GETATTR)
101 zpl_root_getattr_impl(struct user_namespace *user_ns,
102 const struct path *path, struct kstat *stat, u32 request_mask,
103 unsigned int query_flags)
104 #else
105 zpl_root_getattr_impl(const struct path *path, struct kstat *stat,
106 u32 request_mask, unsigned int query_flags)
107 #endif
108 {
109 (void) request_mask, (void) query_flags;
110 struct inode *ip = path->dentry->d_inode;
111
112 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
113 #ifdef HAVE_GENERIC_FILLATTR_USERNS
114 generic_fillattr(user_ns, ip, stat);
115 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
116 generic_fillattr(user_ns, ip, stat);
117 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
118 generic_fillattr(user_ns, request_mask, ip, stat);
119 #else
120 (void) user_ns;
121 #endif
122 #else
123 generic_fillattr(ip, stat);
124 #endif
125 stat->atime = current_time(ip);
126
127 return (0);
128 }
129 ZPL_GETATTR_WRAPPER(zpl_root_getattr);
130
131 static struct dentry *
zpl_root_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)132 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
133 {
134 cred_t *cr = CRED();
135 struct inode *ip;
136 int error;
137
138 crhold(cr);
139 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
140 ASSERT3S(error, <=, 0);
141 crfree(cr);
142
143 if (error) {
144 if (error == -ENOENT)
145 return (d_splice_alias(NULL, dentry));
146 else
147 return (ERR_PTR(error));
148 }
149
150 return (d_splice_alias(ip, dentry));
151 }
152
153 /*
154 * The '.zfs' control directory file and inode operations.
155 */
156 const struct file_operations zpl_fops_root = {
157 .open = zpl_common_open,
158 .llseek = generic_file_llseek,
159 .read = generic_read_dir,
160 .iterate_shared = zpl_root_iterate,
161 };
162
163 const struct inode_operations zpl_ops_root = {
164 .lookup = zpl_root_lookup,
165 .getattr = zpl_root_getattr,
166 };
167
168 static struct vfsmount *
zpl_snapdir_automount(struct path * path)169 zpl_snapdir_automount(struct path *path)
170 {
171 int error;
172
173 error = -zfsctl_snapshot_mount(path, 0);
174 if (error)
175 return (ERR_PTR(error));
176
177 /*
178 * Rather than returning the new vfsmount for the snapshot we must
179 * return NULL to indicate a mount collision. This is done because
180 * the user space mount calls do_add_mount() which adds the vfsmount
181 * to the name space. If we returned the new mount here it would be
182 * added again to the vfsmount list resulting in list corruption.
183 */
184 return (NULL);
185 }
186
187 /*
188 * Negative dentries must always be revalidated so newly created snapshots
189 * can be detected and automounted. Normal dentries should be kept because
190 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
191 * the snapshot being immediately unmounted.
192 */
193 #ifdef HAVE_D_REVALIDATE_4ARGS
194 static int
zpl_snapdir_revalidate(struct inode * dir,const struct qstr * name,struct dentry * dentry,unsigned int flags)195 zpl_snapdir_revalidate(struct inode *dir, const struct qstr *name,
196 struct dentry *dentry, unsigned int flags)
197 #else
198 static int
199 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
200 #endif
201 {
202 return (!!dentry->d_inode);
203 }
204
205 static dentry_operations_t zpl_dops_snapdirs = {
206 /*
207 * Auto mounting of snapshots is only supported for 2.6.37 and
208 * newer kernels. Prior to this kernel the ops->follow_link()
209 * callback was used as a hack to trigger the mount. The
210 * resulting vfsmount was then explicitly grafted in to the
211 * name space. While it might be possible to add compatibility
212 * code to accomplish this it would require considerable care.
213 */
214 .d_automount = zpl_snapdir_automount,
215 .d_revalidate = zpl_snapdir_revalidate,
216 };
217
218 static struct dentry *
zpl_snapdir_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)219 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
220 unsigned int flags)
221 {
222 fstrans_cookie_t cookie;
223 cred_t *cr = CRED();
224 struct inode *ip = NULL;
225 int error;
226
227 crhold(cr);
228 cookie = spl_fstrans_mark();
229 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
230 0, cr, NULL, NULL);
231 ASSERT3S(error, <=, 0);
232 spl_fstrans_unmark(cookie);
233 crfree(cr);
234
235 if (error && error != -ENOENT)
236 return (ERR_PTR(error));
237
238 ASSERT(error == 0 || ip == NULL);
239 d_clear_d_op(dentry);
240 d_set_d_op(dentry, &zpl_dops_snapdirs);
241 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
242
243 return (d_splice_alias(ip, dentry));
244 }
245
246 static int
zpl_snapdir_iterate(struct file * filp,struct dir_context * ctx)247 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
248 {
249 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
250 fstrans_cookie_t cookie;
251 char snapname[MAXNAMELEN];
252 boolean_t case_conflict;
253 uint64_t id, pos;
254 int error = 0;
255
256 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
257 return (error);
258 cookie = spl_fstrans_mark();
259
260 if (!dir_emit_dots(filp, ctx))
261 goto out;
262
263 /* Start the position at 0 if it already emitted . and .. */
264 pos = (ctx->pos == 2 ? 0 : ctx->pos);
265 while (error == 0) {
266 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
267 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
268 snapname, &id, &pos, &case_conflict);
269 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
270 if (error)
271 goto out;
272
273 if (!dir_emit(ctx, snapname, strlen(snapname),
274 ZFSCTL_INO_SHARES - id, DT_DIR))
275 goto out;
276
277 ctx->pos = pos;
278 }
279 out:
280 spl_fstrans_unmark(cookie);
281 zpl_exit(zfsvfs, FTAG);
282
283 if (error == -ENOENT)
284 return (0);
285
286 return (error);
287 }
288
289 static int
290 #ifdef HAVE_IOPS_RENAME_USERNS
zpl_snapdir_rename2(struct user_namespace * user_ns,struct inode * sdip,struct dentry * sdentry,struct inode * tdip,struct dentry * tdentry,unsigned int flags)291 zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip,
292 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
293 unsigned int flags)
294 #elif defined(HAVE_IOPS_RENAME_IDMAP)
295 zpl_snapdir_rename2(struct mnt_idmap *user_ns, struct inode *sdip,
296 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
297 unsigned int flags)
298 #else
299 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,
300 struct inode *tdip, struct dentry *tdentry, unsigned int flags)
301 #endif
302 {
303 cred_t *cr = CRED();
304 int error;
305
306 /* We probably don't want to support renameat2(2) in ctldir */
307 if (flags)
308 return (-EINVAL);
309
310 crhold(cr);
311 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
312 tdip, dname(tdentry), cr, 0);
313 ASSERT3S(error, <=, 0);
314 crfree(cr);
315
316 return (error);
317 }
318
319 #if (!defined(HAVE_RENAME_WANTS_FLAGS) && \
320 !defined(HAVE_IOPS_RENAME_USERNS) && \
321 !defined(HAVE_IOPS_RENAME_IDMAP))
322 static int
zpl_snapdir_rename(struct inode * sdip,struct dentry * sdentry,struct inode * tdip,struct dentry * tdentry)323 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
324 struct inode *tdip, struct dentry *tdentry)
325 {
326 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));
327 }
328 #endif
329
330 static int
zpl_snapdir_rmdir(struct inode * dip,struct dentry * dentry)331 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
332 {
333 cred_t *cr = CRED();
334 int error;
335
336 crhold(cr);
337 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
338 ASSERT3S(error, <=, 0);
339 crfree(cr);
340
341 return (error);
342 }
343
344 static int
345 #ifdef HAVE_IOPS_MKDIR_USERNS
zpl_snapdir_mkdir(struct user_namespace * user_ns,struct inode * dip,struct dentry * dentry,umode_t mode)346 zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip,
347 struct dentry *dentry, umode_t mode)
348 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
349 zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,
350 struct dentry *dentry, umode_t mode)
351 #else
352 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
353 #endif
354 {
355 cred_t *cr = CRED();
356 vattr_t *vap;
357 struct inode *ip;
358 int error;
359
360 crhold(cr);
361 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
362 #if (defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
363 zpl_vap_init(vap, dip, mode | S_IFDIR, cr, user_ns);
364 #else
365 zpl_vap_init(vap, dip, mode | S_IFDIR, cr, zfs_init_idmap);
366 #endif
367
368 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
369 if (error == 0) {
370 d_clear_d_op(dentry);
371 d_set_d_op(dentry, &zpl_dops_snapdirs);
372 d_instantiate(dentry, ip);
373 }
374
375 kmem_free(vap, sizeof (vattr_t));
376 ASSERT3S(error, <=, 0);
377 crfree(cr);
378
379 return (error);
380 }
381
382 /*
383 * Get snapshot directory attributes.
384 */
385 static int
386 #ifdef HAVE_IDMAP_IOPS_GETATTR
zpl_snapdir_getattr_impl(struct mnt_idmap * user_ns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)387 zpl_snapdir_getattr_impl(struct mnt_idmap *user_ns,
388 const struct path *path, struct kstat *stat, u32 request_mask,
389 unsigned int query_flags)
390 #elif defined(HAVE_USERNS_IOPS_GETATTR)
391 zpl_snapdir_getattr_impl(struct user_namespace *user_ns,
392 const struct path *path, struct kstat *stat, u32 request_mask,
393 unsigned int query_flags)
394 #else
395 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
396 u32 request_mask, unsigned int query_flags)
397 #endif
398 {
399 (void) request_mask, (void) query_flags;
400 struct inode *ip = path->dentry->d_inode;
401 zfsvfs_t *zfsvfs = ITOZSB(ip);
402 int error;
403
404 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
405 return (error);
406 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
407 #ifdef HAVE_GENERIC_FILLATTR_USERNS
408 generic_fillattr(user_ns, ip, stat);
409 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
410 generic_fillattr(user_ns, ip, stat);
411 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
412 generic_fillattr(user_ns, request_mask, ip, stat);
413 #else
414 (void) user_ns;
415 #endif
416 #else
417 generic_fillattr(ip, stat);
418 #endif
419
420 stat->nlink = stat->size = 2;
421
422 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
423 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
424 uint64_t snap_count;
425 int err = zap_count(
426 dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
427 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
428 if (err != 0) {
429 zpl_exit(zfsvfs, FTAG);
430 return (-err);
431 }
432 stat->nlink += snap_count;
433 }
434
435 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
436 stat->atime = current_time(ip);
437 zpl_exit(zfsvfs, FTAG);
438
439 return (0);
440 }
441 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);
442
443 /*
444 * The '.zfs/snapshot' directory file operations. These mainly control
445 * generating the list of available snapshots when doing an 'ls' in the
446 * directory. See zpl_snapdir_readdir().
447 */
448 const struct file_operations zpl_fops_snapdir = {
449 .open = zpl_common_open,
450 .llseek = generic_file_llseek,
451 .read = generic_read_dir,
452 .iterate_shared = zpl_snapdir_iterate,
453
454 };
455
456 /*
457 * The '.zfs/snapshot' directory inode operations. These mainly control
458 * creating an inode for a snapshot directory and initializing the needed
459 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
460 */
461 const struct inode_operations zpl_ops_snapdir = {
462 .lookup = zpl_snapdir_lookup,
463 .getattr = zpl_snapdir_getattr,
464 #if (defined(HAVE_RENAME_WANTS_FLAGS) || \
465 defined(HAVE_IOPS_RENAME_USERNS) || \
466 defined(HAVE_IOPS_RENAME_IDMAP))
467 .rename = zpl_snapdir_rename2,
468 #else
469 .rename = zpl_snapdir_rename,
470 #endif
471 .rmdir = zpl_snapdir_rmdir,
472 .mkdir = zpl_snapdir_mkdir,
473 };
474
475 static struct dentry *
zpl_shares_lookup(struct inode * dip,struct dentry * dentry,unsigned int flags)476 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
477 unsigned int flags)
478 {
479 fstrans_cookie_t cookie;
480 cred_t *cr = CRED();
481 struct inode *ip = NULL;
482 int error;
483
484 crhold(cr);
485 cookie = spl_fstrans_mark();
486 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
487 0, cr, NULL, NULL);
488 ASSERT3S(error, <=, 0);
489 spl_fstrans_unmark(cookie);
490 crfree(cr);
491
492 if (error) {
493 if (error == -ENOENT)
494 return (d_splice_alias(NULL, dentry));
495 else
496 return (ERR_PTR(error));
497 }
498
499 return (d_splice_alias(ip, dentry));
500 }
501
502 static int
zpl_shares_iterate(struct file * filp,struct dir_context * ctx)503 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
504 {
505 fstrans_cookie_t cookie;
506 cred_t *cr = CRED();
507 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
508 znode_t *dzp;
509 int error = 0;
510
511 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
512 return (error);
513 cookie = spl_fstrans_mark();
514
515 if (zfsvfs->z_shares_dir == 0) {
516 dir_emit_dots(filp, ctx);
517 goto out;
518 }
519
520 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
521 if (error)
522 goto out;
523
524 crhold(cr);
525 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
526 crfree(cr);
527
528 iput(ZTOI(dzp));
529 out:
530 spl_fstrans_unmark(cookie);
531 zpl_exit(zfsvfs, FTAG);
532 ASSERT3S(error, <=, 0);
533
534 return (error);
535 }
536
537 static int
538 #ifdef HAVE_USERNS_IOPS_GETATTR
zpl_shares_getattr_impl(struct user_namespace * user_ns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)539 zpl_shares_getattr_impl(struct user_namespace *user_ns,
540 const struct path *path, struct kstat *stat, u32 request_mask,
541 unsigned int query_flags)
542 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
543 zpl_shares_getattr_impl(struct mnt_idmap *user_ns,
544 const struct path *path, struct kstat *stat, u32 request_mask,
545 unsigned int query_flags)
546 #else
547 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,
548 u32 request_mask, unsigned int query_flags)
549 #endif
550 {
551 (void) request_mask, (void) query_flags;
552 struct inode *ip = path->dentry->d_inode;
553 zfsvfs_t *zfsvfs = ITOZSB(ip);
554 znode_t *dzp;
555 int error;
556
557 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
558 return (error);
559
560 if (zfsvfs->z_shares_dir == 0) {
561 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
562 #ifdef HAVE_GENERIC_FILLATTR_USERNS
563 generic_fillattr(user_ns, path->dentry->d_inode, stat);
564 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
565 generic_fillattr(user_ns, path->dentry->d_inode, stat);
566 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
567 generic_fillattr(user_ns, request_mask, ip, stat);
568 #else
569 (void) user_ns;
570 #endif
571 #else
572 generic_fillattr(path->dentry->d_inode, stat);
573 #endif
574 stat->nlink = stat->size = 2;
575 stat->atime = current_time(ip);
576 zpl_exit(zfsvfs, FTAG);
577 return (0);
578 }
579
580 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
581 if (error == 0) {
582 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
583 error = -zfs_getattr_fast(user_ns, request_mask, ZTOI(dzp),
584 stat);
585 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
586 error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat);
587 #else
588 error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat);
589 #endif
590 iput(ZTOI(dzp));
591 }
592
593 zpl_exit(zfsvfs, FTAG);
594 ASSERT3S(error, <=, 0);
595
596 return (error);
597 }
598 ZPL_GETATTR_WRAPPER(zpl_shares_getattr);
599
600 /*
601 * The '.zfs/shares' directory file operations.
602 */
603 const struct file_operations zpl_fops_shares = {
604 .open = zpl_common_open,
605 .llseek = generic_file_llseek,
606 .read = generic_read_dir,
607 .iterate_shared = zpl_shares_iterate,
608 };
609
610 /*
611 * The '.zfs/shares' directory inode operations.
612 */
613 const struct inode_operations zpl_ops_shares = {
614 .lookup = zpl_shares_lookup,
615 .getattr = zpl_shares_getattr,
616 };
617