ZIL: Call brt_pending_add() replaying TX_CLONE_RANGE
[zfs.git] / module / os / linux / zfs / zpl_ctldir.c
blob8ee7fcecc7b7a939c86d96d2b9eb3627ea3dbafa
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
22 * Copyright (C) 2011 Lawrence Livermore National Security, LLC.
23 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
24 * LLNL-CODE-403049.
25 * Rewritten for Linux by:
26 * Rohan Puri <rohan.puri15@gmail.com>
27 * Brian Behlendorf <behlendorf1@llnl.gov>
30 #include <sys/zfs_znode.h>
31 #include <sys/zfs_vfsops.h>
32 #include <sys/zfs_vnops.h>
33 #include <sys/zfs_ctldir.h>
34 #include <sys/zpl.h>
35 #include <sys/dmu.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/zap.h>
40 * Common open routine. Disallow any write access.
42 static int
43 zpl_common_open(struct inode *ip, struct file *filp)
45 if (blk_mode_is_open_write(filp->f_mode))
46 return (-EACCES);
48 return (generic_file_open(ip, filp));
52 * Get root directory contents.
54 static int
55 zpl_root_iterate(struct file *filp, zpl_dir_context_t *ctx)
57 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
58 int error = 0;
60 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
61 return (error);
63 if (!zpl_dir_emit_dots(filp, ctx))
64 goto out;
66 if (ctx->pos == 2) {
67 if (!zpl_dir_emit(ctx, ZFS_SNAPDIR_NAME,
68 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))
69 goto out;
71 ctx->pos++;
74 if (ctx->pos == 3) {
75 if (!zpl_dir_emit(ctx, ZFS_SHAREDIR_NAME,
76 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))
77 goto out;
79 ctx->pos++;
81 out:
82 zpl_exit(zfsvfs, FTAG);
84 return (error);
87 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
88 static int
89 zpl_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
91 zpl_dir_context_t ctx =
92 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
93 int error;
95 error = zpl_root_iterate(filp, &ctx);
96 filp->f_pos = ctx.pos;
98 return (error);
100 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
103 * Get root directory attributes.
105 static int
106 #ifdef HAVE_IDMAP_IOPS_GETATTR
107 zpl_root_getattr_impl(struct mnt_idmap *user_ns,
108 const struct path *path, struct kstat *stat, u32 request_mask,
109 unsigned int query_flags)
110 #elif defined(HAVE_USERNS_IOPS_GETATTR)
111 zpl_root_getattr_impl(struct user_namespace *user_ns,
112 const struct path *path, struct kstat *stat, u32 request_mask,
113 unsigned int query_flags)
114 #else
115 zpl_root_getattr_impl(const struct path *path, struct kstat *stat,
116 u32 request_mask, unsigned int query_flags)
117 #endif
119 (void) request_mask, (void) query_flags;
120 struct inode *ip = path->dentry->d_inode;
122 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
123 #ifdef HAVE_GENERIC_FILLATTR_USERNS
124 generic_fillattr(user_ns, ip, stat);
125 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
126 generic_fillattr(user_ns, ip, stat);
127 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
128 generic_fillattr(user_ns, request_mask, ip, stat);
129 #else
130 (void) user_ns;
131 #endif
132 #else
133 generic_fillattr(ip, stat);
134 #endif
135 stat->atime = current_time(ip);
137 return (0);
139 ZPL_GETATTR_WRAPPER(zpl_root_getattr);
141 static struct dentry *
142 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
144 cred_t *cr = CRED();
145 struct inode *ip;
146 int error;
148 crhold(cr);
149 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
150 ASSERT3S(error, <=, 0);
151 crfree(cr);
153 if (error) {
154 if (error == -ENOENT)
155 return (d_splice_alias(NULL, dentry));
156 else
157 return (ERR_PTR(error));
160 return (d_splice_alias(ip, dentry));
164 * The '.zfs' control directory file and inode operations.
166 const struct file_operations zpl_fops_root = {
167 .open = zpl_common_open,
168 .llseek = generic_file_llseek,
169 .read = generic_read_dir,
170 #ifdef HAVE_VFS_ITERATE_SHARED
171 .iterate_shared = zpl_root_iterate,
172 #elif defined(HAVE_VFS_ITERATE)
173 .iterate = zpl_root_iterate,
174 #else
175 .readdir = zpl_root_readdir,
176 #endif
179 const struct inode_operations zpl_ops_root = {
180 .lookup = zpl_root_lookup,
181 .getattr = zpl_root_getattr,
184 static struct vfsmount *
185 zpl_snapdir_automount(struct path *path)
187 int error;
189 error = -zfsctl_snapshot_mount(path, 0);
190 if (error)
191 return (ERR_PTR(error));
194 * Rather than returning the new vfsmount for the snapshot we must
195 * return NULL to indicate a mount collision. This is done because
196 * the user space mount calls do_add_mount() which adds the vfsmount
197 * to the name space. If we returned the new mount here it would be
198 * added again to the vfsmount list resulting in list corruption.
200 return (NULL);
204 * Negative dentries must always be revalidated so newly created snapshots
205 * can be detected and automounted. Normal dentries should be kept because
206 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
207 * the snapshot being immediately unmounted.
209 static int
210 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
211 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i)
212 #else
213 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
214 #endif
216 return (!!dentry->d_inode);
219 static dentry_operations_t zpl_dops_snapdirs = {
221 * Auto mounting of snapshots is only supported for 2.6.37 and
222 * newer kernels. Prior to this kernel the ops->follow_link()
223 * callback was used as a hack to trigger the mount. The
224 * resulting vfsmount was then explicitly grafted in to the
225 * name space. While it might be possible to add compatibility
226 * code to accomplish this it would require considerable care.
228 .d_automount = zpl_snapdir_automount,
229 .d_revalidate = zpl_snapdir_revalidate,
232 static struct dentry *
233 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
234 unsigned int flags)
236 fstrans_cookie_t cookie;
237 cred_t *cr = CRED();
238 struct inode *ip = NULL;
239 int error;
241 crhold(cr);
242 cookie = spl_fstrans_mark();
243 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
244 0, cr, NULL, NULL);
245 ASSERT3S(error, <=, 0);
246 spl_fstrans_unmark(cookie);
247 crfree(cr);
249 if (error && error != -ENOENT)
250 return (ERR_PTR(error));
252 ASSERT(error == 0 || ip == NULL);
253 d_clear_d_op(dentry);
254 d_set_d_op(dentry, &zpl_dops_snapdirs);
255 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
257 return (d_splice_alias(ip, dentry));
260 static int
261 zpl_snapdir_iterate(struct file *filp, zpl_dir_context_t *ctx)
263 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
264 fstrans_cookie_t cookie;
265 char snapname[MAXNAMELEN];
266 boolean_t case_conflict;
267 uint64_t id, pos;
268 int error = 0;
270 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
271 return (error);
272 cookie = spl_fstrans_mark();
274 if (!zpl_dir_emit_dots(filp, ctx))
275 goto out;
277 /* Start the position at 0 if it already emitted . and .. */
278 pos = (ctx->pos == 2 ? 0 : ctx->pos);
279 while (error == 0) {
280 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
281 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
282 snapname, &id, &pos, &case_conflict);
283 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
284 if (error)
285 goto out;
287 if (!zpl_dir_emit(ctx, snapname, strlen(snapname),
288 ZFSCTL_INO_SHARES - id, DT_DIR))
289 goto out;
291 ctx->pos = pos;
293 out:
294 spl_fstrans_unmark(cookie);
295 zpl_exit(zfsvfs, FTAG);
297 if (error == -ENOENT)
298 return (0);
300 return (error);
303 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
304 static int
305 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
307 zpl_dir_context_t ctx =
308 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
309 int error;
311 error = zpl_snapdir_iterate(filp, &ctx);
312 filp->f_pos = ctx.pos;
314 return (error);
316 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
318 static int
319 #ifdef HAVE_IOPS_RENAME_USERNS
320 zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip,
321 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
322 unsigned int flags)
323 #elif defined(HAVE_IOPS_RENAME_IDMAP)
324 zpl_snapdir_rename2(struct mnt_idmap *user_ns, struct inode *sdip,
325 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
326 unsigned int flags)
327 #else
328 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,
329 struct inode *tdip, struct dentry *tdentry, unsigned int flags)
330 #endif
332 cred_t *cr = CRED();
333 int error;
335 /* We probably don't want to support renameat2(2) in ctldir */
336 if (flags)
337 return (-EINVAL);
339 crhold(cr);
340 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
341 tdip, dname(tdentry), cr, 0);
342 ASSERT3S(error, <=, 0);
343 crfree(cr);
345 return (error);
348 #if (!defined(HAVE_RENAME_WANTS_FLAGS) && \
349 !defined(HAVE_IOPS_RENAME_USERNS) && \
350 !defined(HAVE_IOPS_RENAME_IDMAP))
351 static int
352 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
353 struct inode *tdip, struct dentry *tdentry)
355 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));
357 #endif
359 static int
360 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
362 cred_t *cr = CRED();
363 int error;
365 crhold(cr);
366 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
367 ASSERT3S(error, <=, 0);
368 crfree(cr);
370 return (error);
373 static int
374 #ifdef HAVE_IOPS_MKDIR_USERNS
375 zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip,
376 struct dentry *dentry, umode_t mode)
377 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
378 zpl_snapdir_mkdir(struct mnt_idmap *user_ns, struct inode *dip,
379 struct dentry *dentry, umode_t mode)
380 #else
381 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
382 #endif
384 cred_t *cr = CRED();
385 vattr_t *vap;
386 struct inode *ip;
387 int error;
389 crhold(cr);
390 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
391 #if (defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
392 zpl_vap_init(vap, dip, mode | S_IFDIR, cr, user_ns);
393 #else
394 zpl_vap_init(vap, dip, mode | S_IFDIR, cr, zfs_init_idmap);
395 #endif
397 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
398 if (error == 0) {
399 d_clear_d_op(dentry);
400 d_set_d_op(dentry, &zpl_dops_snapdirs);
401 d_instantiate(dentry, ip);
404 kmem_free(vap, sizeof (vattr_t));
405 ASSERT3S(error, <=, 0);
406 crfree(cr);
408 return (error);
412 * Get snapshot directory attributes.
414 static int
415 #ifdef HAVE_IDMAP_IOPS_GETATTR
416 zpl_snapdir_getattr_impl(struct mnt_idmap *user_ns,
417 const struct path *path, struct kstat *stat, u32 request_mask,
418 unsigned int query_flags)
419 #elif defined(HAVE_USERNS_IOPS_GETATTR)
420 zpl_snapdir_getattr_impl(struct user_namespace *user_ns,
421 const struct path *path, struct kstat *stat, u32 request_mask,
422 unsigned int query_flags)
423 #else
424 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
425 u32 request_mask, unsigned int query_flags)
426 #endif
428 (void) request_mask, (void) query_flags;
429 struct inode *ip = path->dentry->d_inode;
430 zfsvfs_t *zfsvfs = ITOZSB(ip);
431 int error;
433 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
434 return (error);
435 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
436 #ifdef HAVE_GENERIC_FILLATTR_USERNS
437 generic_fillattr(user_ns, ip, stat);
438 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
439 generic_fillattr(user_ns, ip, stat);
440 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
441 generic_fillattr(user_ns, request_mask, ip, stat);
442 #else
443 (void) user_ns;
444 #endif
445 #else
446 generic_fillattr(ip, stat);
447 #endif
449 stat->nlink = stat->size = 2;
451 dsl_dataset_t *ds = dmu_objset_ds(zfsvfs->z_os);
452 if (dsl_dataset_phys(ds)->ds_snapnames_zapobj != 0) {
453 uint64_t snap_count;
454 int err = zap_count(
455 dmu_objset_pool(ds->ds_objset)->dp_meta_objset,
456 dsl_dataset_phys(ds)->ds_snapnames_zapobj, &snap_count);
457 if (err != 0) {
458 zpl_exit(zfsvfs, FTAG);
459 return (-err);
461 stat->nlink += snap_count;
464 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
465 stat->atime = current_time(ip);
466 zpl_exit(zfsvfs, FTAG);
468 return (0);
470 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);
473 * The '.zfs/snapshot' directory file operations. These mainly control
474 * generating the list of available snapshots when doing an 'ls' in the
475 * directory. See zpl_snapdir_readdir().
477 const struct file_operations zpl_fops_snapdir = {
478 .open = zpl_common_open,
479 .llseek = generic_file_llseek,
480 .read = generic_read_dir,
481 #ifdef HAVE_VFS_ITERATE_SHARED
482 .iterate_shared = zpl_snapdir_iterate,
483 #elif defined(HAVE_VFS_ITERATE)
484 .iterate = zpl_snapdir_iterate,
485 #else
486 .readdir = zpl_snapdir_readdir,
487 #endif
492 * The '.zfs/snapshot' directory inode operations. These mainly control
493 * creating an inode for a snapshot directory and initializing the needed
494 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
496 const struct inode_operations zpl_ops_snapdir = {
497 .lookup = zpl_snapdir_lookup,
498 .getattr = zpl_snapdir_getattr,
499 #if (defined(HAVE_RENAME_WANTS_FLAGS) || \
500 defined(HAVE_IOPS_RENAME_USERNS) || \
501 defined(HAVE_IOPS_RENAME_IDMAP))
502 .rename = zpl_snapdir_rename2,
503 #else
504 .rename = zpl_snapdir_rename,
505 #endif
506 .rmdir = zpl_snapdir_rmdir,
507 .mkdir = zpl_snapdir_mkdir,
510 static struct dentry *
511 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
512 unsigned int flags)
514 fstrans_cookie_t cookie;
515 cred_t *cr = CRED();
516 struct inode *ip = NULL;
517 int error;
519 crhold(cr);
520 cookie = spl_fstrans_mark();
521 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
522 0, cr, NULL, NULL);
523 ASSERT3S(error, <=, 0);
524 spl_fstrans_unmark(cookie);
525 crfree(cr);
527 if (error) {
528 if (error == -ENOENT)
529 return (d_splice_alias(NULL, dentry));
530 else
531 return (ERR_PTR(error));
534 return (d_splice_alias(ip, dentry));
537 static int
538 zpl_shares_iterate(struct file *filp, zpl_dir_context_t *ctx)
540 fstrans_cookie_t cookie;
541 cred_t *cr = CRED();
542 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
543 znode_t *dzp;
544 int error = 0;
546 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
547 return (error);
548 cookie = spl_fstrans_mark();
550 if (zfsvfs->z_shares_dir == 0) {
551 zpl_dir_emit_dots(filp, ctx);
552 goto out;
555 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
556 if (error)
557 goto out;
559 crhold(cr);
560 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
561 crfree(cr);
563 iput(ZTOI(dzp));
564 out:
565 spl_fstrans_unmark(cookie);
566 zpl_exit(zfsvfs, FTAG);
567 ASSERT3S(error, <=, 0);
569 return (error);
572 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
573 static int
574 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
576 zpl_dir_context_t ctx =
577 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
578 int error;
580 error = zpl_shares_iterate(filp, &ctx);
581 filp->f_pos = ctx.pos;
583 return (error);
585 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
587 static int
588 #ifdef HAVE_USERNS_IOPS_GETATTR
589 zpl_shares_getattr_impl(struct user_namespace *user_ns,
590 const struct path *path, struct kstat *stat, u32 request_mask,
591 unsigned int query_flags)
592 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
593 zpl_shares_getattr_impl(struct mnt_idmap *user_ns,
594 const struct path *path, struct kstat *stat, u32 request_mask,
595 unsigned int query_flags)
596 #else
597 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,
598 u32 request_mask, unsigned int query_flags)
599 #endif
601 (void) request_mask, (void) query_flags;
602 struct inode *ip = path->dentry->d_inode;
603 zfsvfs_t *zfsvfs = ITOZSB(ip);
604 znode_t *dzp;
605 int error;
607 if ((error = zpl_enter(zfsvfs, FTAG)) != 0)
608 return (error);
610 if (zfsvfs->z_shares_dir == 0) {
611 #if (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
612 #ifdef HAVE_GENERIC_FILLATTR_USERNS
613 generic_fillattr(user_ns, path->dentry->d_inode, stat);
614 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP)
615 generic_fillattr(user_ns, path->dentry->d_inode, stat);
616 #elif defined(HAVE_GENERIC_FILLATTR_IDMAP_REQMASK)
617 generic_fillattr(user_ns, request_mask, ip, stat);
618 #else
619 (void) user_ns;
620 #endif
621 #else
622 generic_fillattr(path->dentry->d_inode, stat);
623 #endif
624 stat->nlink = stat->size = 2;
625 stat->atime = current_time(ip);
626 zpl_exit(zfsvfs, FTAG);
627 return (0);
630 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
631 if (error == 0) {
632 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
633 error = -zfs_getattr_fast(user_ns, request_mask, ZTOI(dzp),
634 stat);
635 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
636 error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat);
637 #else
638 error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat);
639 #endif
640 iput(ZTOI(dzp));
643 zpl_exit(zfsvfs, FTAG);
644 ASSERT3S(error, <=, 0);
646 return (error);
648 ZPL_GETATTR_WRAPPER(zpl_shares_getattr);
651 * The '.zfs/shares' directory file operations.
653 const struct file_operations zpl_fops_shares = {
654 .open = zpl_common_open,
655 .llseek = generic_file_llseek,
656 .read = generic_read_dir,
657 #ifdef HAVE_VFS_ITERATE_SHARED
658 .iterate_shared = zpl_shares_iterate,
659 #elif defined(HAVE_VFS_ITERATE)
660 .iterate = zpl_shares_iterate,
661 #else
662 .readdir = zpl_shares_readdir,
663 #endif
668 * The '.zfs/shares' directory inode operations.
670 const struct inode_operations zpl_ops_shares = {
671 .lookup = zpl_shares_lookup,
672 .getattr = zpl_shares_getattr,