]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - module/zfs/zpl_ctldir.c
Mark additional functions as PF_FSTRANS
[FreeBSD/FreeBSD.git] / module / zfs / zpl_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  * 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>
28  */
29
30 #include <sys/zfs_vfsops.h>
31 #include <sys/zfs_vnops.h>
32 #include <sys/zfs_znode.h>
33 #include <sys/zfs_ctldir.h>
34 #include <sys/zpl.h>
35
36 /*
37  * Common open routine.  Disallow any write access.
38  */
39 /* ARGSUSED */
40 static int
41 zpl_common_open(struct inode *ip, struct file *filp)
42 {
43         if (filp->f_mode & FMODE_WRITE)
44                 return (-EACCES);
45
46         return (generic_file_open(ip, filp));
47 }
48
49 /*
50  * Get root directory contents.
51  */
52 static int
53 zpl_root_iterate(struct file *filp, struct dir_context *ctx)
54 {
55         zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
56         int error = 0;
57
58         ZFS_ENTER(zsb);
59
60         if (!dir_emit_dots(filp, ctx))
61                 goto out;
62
63         if (ctx->pos == 2) {
64                 if (!dir_emit(ctx, ZFS_SNAPDIR_NAME, strlen(ZFS_SNAPDIR_NAME),
65                     ZFSCTL_INO_SNAPDIR, DT_DIR))
66                         goto out;
67
68                 ctx->pos++;
69         }
70
71         if (ctx->pos == 3) {
72                 if (!dir_emit(ctx, ZFS_SHAREDIR_NAME, strlen(ZFS_SHAREDIR_NAME),
73                     ZFSCTL_INO_SHARES, DT_DIR))
74                         goto out;
75
76                 ctx->pos++;
77         }
78 out:
79         ZFS_EXIT(zsb);
80
81         return (error);
82 }
83
84 #if !defined(HAVE_VFS_ITERATE)
85 static int
86 zpl_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
87 {
88         struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
89         int error;
90
91         error = zpl_root_iterate(filp, &ctx);
92         filp->f_pos = ctx.pos;
93
94         return (error);
95 }
96 #endif /* HAVE_VFS_ITERATE */
97
98 /*
99  * Get root directory attributes.
100  */
101 /* ARGSUSED */
102 static int
103 zpl_root_getattr(struct vfsmount *mnt, struct dentry *dentry,
104     struct kstat *stat)
105 {
106         int error;
107
108         error = simple_getattr(mnt, dentry, stat);
109         stat->atime = CURRENT_TIME;
110
111         return (error);
112 }
113
114 static struct dentry *
115 #ifdef HAVE_LOOKUP_NAMEIDATA
116 zpl_root_lookup(struct inode *dip, struct dentry *dentry, struct nameidata *nd)
117 #else
118 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
119 #endif
120 {
121         cred_t *cr = CRED();
122         struct inode *ip;
123         int error;
124
125         crhold(cr);
126         error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
127         ASSERT3S(error, <=, 0);
128         crfree(cr);
129
130         if (error) {
131                 if (error == -ENOENT)
132                         return (d_splice_alias(NULL, dentry));
133                 else
134                         return (ERR_PTR(error));
135         }
136
137         return (d_splice_alias(ip, dentry));
138 }
139
140 /*
141  * The '.zfs' control directory file and inode operations.
142  */
143 const struct file_operations zpl_fops_root = {
144         .open           = zpl_common_open,
145         .llseek         = generic_file_llseek,
146         .read           = generic_read_dir,
147 #ifdef HAVE_VFS_ITERATE
148         .iterate        = zpl_root_iterate,
149 #else
150         .readdir        = zpl_root_readdir,
151 #endif
152 };
153
154 const struct inode_operations zpl_ops_root = {
155         .lookup         = zpl_root_lookup,
156         .getattr        = zpl_root_getattr,
157 };
158
159 #ifdef HAVE_AUTOMOUNT
160 static struct vfsmount *
161 zpl_snapdir_automount(struct path *path)
162 {
163         struct dentry *dentry = path->dentry;
164         int error;
165
166         /*
167          * We must briefly disable automounts for this dentry because the
168          * user space mount utility will trigger another lookup on this
169          * directory.  That will result in zpl_snapdir_automount() being
170          * called repeatedly.  The DCACHE_NEED_AUTOMOUNT flag can be
171          * safely reset once the mount completes.
172          */
173         dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
174         error = -zfsctl_mount_snapshot(path, 0);
175         dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
176         if (error)
177                 return (ERR_PTR(error));
178
179         /*
180          * Rather than returning the new vfsmount for the snapshot we must
181          * return NULL to indicate a mount collision.  This is done because
182          * the user space mount calls do_add_mount() which adds the vfsmount
183          * to the name space.  If we returned the new mount here it would be
184          * added again to the vfsmount list resulting in list corruption.
185          */
186         return (NULL);
187 }
188 #endif /* HAVE_AUTOMOUNT */
189
190 /*
191  * Revalidate any dentry in the snapshot directory on lookup, since a snapshot
192  * having the same name have been created or destroyed since it was cached.
193  */
194 static int
195 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
196 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i)
197 #else
198 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
199 #endif
200 {
201         return (0);
202 }
203
204 dentry_operations_t zpl_dops_snapdirs = {
205 /*
206  * Auto mounting of snapshots is only supported for 2.6.37 and
207  * newer kernels.  Prior to this kernel the ops->follow_link()
208  * callback was used as a hack to trigger the mount.  The
209  * resulting vfsmount was then explicitly grafted in to the
210  * name space.  While it might be possible to add compatibility
211  * code to accomplish this it would require considerable care.
212  */
213 #ifdef HAVE_AUTOMOUNT
214         .d_automount    = zpl_snapdir_automount,
215 #endif /* HAVE_AUTOMOUNT */
216         .d_revalidate   = zpl_snapdir_revalidate,
217 };
218
219 static struct dentry *
220 #ifdef HAVE_LOOKUP_NAMEIDATA
221 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
222     struct nameidata *nd)
223 #else
224 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
225     unsigned int flags)
226 #endif
227
228 {
229         fstrans_cookie_t cookie;
230         cred_t *cr = CRED();
231         struct inode *ip = NULL;
232         int error;
233
234         crhold(cr);
235         cookie = spl_fstrans_mark();
236         error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
237             0, cr, NULL, NULL);
238         ASSERT3S(error, <=, 0);
239         spl_fstrans_unmark(cookie);
240         crfree(cr);
241
242         if (error && error != -ENOENT)
243                 return (ERR_PTR(error));
244
245         ASSERT(error == 0 || ip == NULL);
246         d_clear_d_op(dentry);
247         d_set_d_op(dentry, &zpl_dops_snapdirs);
248
249         return (d_splice_alias(ip, dentry));
250 }
251
252 static int
253 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
254 {
255         zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
256         fstrans_cookie_t cookie;
257         char snapname[MAXNAMELEN];
258         boolean_t case_conflict;
259         uint64_t id, pos;
260         int error = 0;
261
262         ZFS_ENTER(zsb);
263         cookie = spl_fstrans_mark();
264
265         if (!dir_emit_dots(filp, ctx))
266                 goto out;
267
268         pos = ctx->pos;
269         while (error == 0) {
270                 dsl_pool_config_enter(dmu_objset_pool(zsb->z_os), FTAG);
271                 error = -dmu_snapshot_list_next(zsb->z_os, MAXNAMELEN,
272                     snapname, &id, &pos, &case_conflict);
273                 dsl_pool_config_exit(dmu_objset_pool(zsb->z_os), FTAG);
274                 if (error)
275                         goto out;
276
277                 if (!dir_emit(ctx, snapname, strlen(snapname),
278                     ZFSCTL_INO_SHARES - id, DT_DIR))
279                         goto out;
280
281                 ctx->pos = pos;
282         }
283 out:
284         spl_fstrans_unmark(cookie);
285         ZFS_EXIT(zsb);
286
287         if (error == -ENOENT)
288                 return (0);
289
290         return (error);
291 }
292
293 #if !defined(HAVE_VFS_ITERATE)
294 static int
295 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
296 {
297         struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
298         int error;
299
300         error = zpl_snapdir_iterate(filp, &ctx);
301         filp->f_pos = ctx.pos;
302
303         return (error);
304 }
305 #endif /* HAVE_VFS_ITERATE */
306
307 int
308 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
309     struct inode *tdip, struct dentry *tdentry)
310 {
311         cred_t *cr = CRED();
312         int error;
313
314         crhold(cr);
315         error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
316             tdip, dname(tdentry), cr, 0);
317         ASSERT3S(error, <=, 0);
318         crfree(cr);
319
320         return (error);
321 }
322
323 static int
324 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
325 {
326         cred_t *cr = CRED();
327         int error;
328
329         crhold(cr);
330         error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
331         ASSERT3S(error, <=, 0);
332         crfree(cr);
333
334         return (error);
335 }
336
337 static int
338 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, zpl_umode_t mode)
339 {
340         cred_t *cr = CRED();
341         vattr_t *vap;
342         struct inode *ip;
343         int error;
344
345         crhold(cr);
346         vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
347         zpl_vap_init(vap, dip, mode | S_IFDIR, cr);
348
349         error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
350         if (error == 0) {
351                 d_clear_d_op(dentry);
352                 d_set_d_op(dentry, &zpl_dops_snapdirs);
353                 d_instantiate(dentry, ip);
354         }
355
356         kmem_free(vap, sizeof (vattr_t));
357         ASSERT3S(error, <=, 0);
358         crfree(cr);
359
360         return (error);
361 }
362
363 /*
364  * Get snapshot directory attributes.
365  */
366 /* ARGSUSED */
367 static int
368 zpl_snapdir_getattr(struct vfsmount *mnt, struct dentry *dentry,
369     struct kstat *stat)
370 {
371         zfs_sb_t *zsb = ITOZSB(dentry->d_inode);
372         int error;
373
374         ZFS_ENTER(zsb);
375         error = simple_getattr(mnt, dentry, stat);
376         stat->nlink = stat->size = avl_numnodes(&zsb->z_ctldir_snaps) + 2;
377         stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zsb->z_os);
378         stat->atime = CURRENT_TIME;
379         ZFS_EXIT(zsb);
380
381         return (error);
382 }
383
384 /*
385  * The '.zfs/snapshot' directory file operations.  These mainly control
386  * generating the list of available snapshots when doing an 'ls' in the
387  * directory.  See zpl_snapdir_readdir().
388  */
389 const struct file_operations zpl_fops_snapdir = {
390         .open           = zpl_common_open,
391         .llseek         = generic_file_llseek,
392         .read           = generic_read_dir,
393 #ifdef HAVE_VFS_ITERATE
394         .iterate        = zpl_snapdir_iterate,
395 #else
396         .readdir        = zpl_snapdir_readdir,
397 #endif
398
399 };
400
401 /*
402  * The '.zfs/snapshot' directory inode operations.  These mainly control
403  * creating an inode for a snapshot directory and initializing the needed
404  * infrastructure to automount the snapshot.  See zpl_snapdir_lookup().
405  */
406 const struct inode_operations zpl_ops_snapdir = {
407         .lookup         = zpl_snapdir_lookup,
408         .getattr        = zpl_snapdir_getattr,
409         .rename         = zpl_snapdir_rename,
410         .rmdir          = zpl_snapdir_rmdir,
411         .mkdir          = zpl_snapdir_mkdir,
412 };
413
414 static struct dentry *
415 #ifdef HAVE_LOOKUP_NAMEIDATA
416 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
417     struct nameidata *nd)
418 #else
419 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
420     unsigned int flags)
421 #endif
422 {
423         fstrans_cookie_t cookie;
424         cred_t *cr = CRED();
425         struct inode *ip = NULL;
426         int error;
427
428         crhold(cr);
429         cookie = spl_fstrans_mark();
430         error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
431             0, cr, NULL, NULL);
432         ASSERT3S(error, <=, 0);
433         spl_fstrans_unmark(cookie);
434         crfree(cr);
435
436         if (error) {
437                 if (error == -ENOENT)
438                         return (d_splice_alias(NULL, dentry));
439                 else
440                         return (ERR_PTR(error));
441         }
442
443         return (d_splice_alias(ip, dentry));
444 }
445
446 static int
447 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
448 {
449         fstrans_cookie_t cookie;
450         cred_t *cr = CRED();
451         zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
452         znode_t *dzp;
453         int error = 0;
454
455         ZFS_ENTER(zsb);
456         cookie = spl_fstrans_mark();
457
458         if (zsb->z_shares_dir == 0) {
459                 dir_emit_dots(filp, ctx);
460                 goto out;
461         }
462
463         error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
464         if (error)
465                 goto out;
466
467         crhold(cr);
468         error = -zfs_readdir(ZTOI(dzp), ctx, cr);
469         crfree(cr);
470
471         iput(ZTOI(dzp));
472 out:
473         spl_fstrans_unmark(cookie);
474         ZFS_EXIT(zsb);
475         ASSERT3S(error, <=, 0);
476
477         return (error);
478 }
479
480 #if !defined(HAVE_VFS_ITERATE)
481 static int
482 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
483 {
484         struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
485         int error;
486
487         error = zpl_shares_iterate(filp, &ctx);
488         filp->f_pos = ctx.pos;
489
490         return (error);
491 }
492 #endif /* HAVE_VFS_ITERATE */
493
494 /* ARGSUSED */
495 static int
496 zpl_shares_getattr(struct vfsmount *mnt, struct dentry *dentry,
497     struct kstat *stat)
498 {
499         struct inode *ip = dentry->d_inode;
500         zfs_sb_t *zsb = ITOZSB(ip);
501         znode_t *dzp;
502         int error;
503
504         ZFS_ENTER(zsb);
505
506         if (zsb->z_shares_dir == 0) {
507                 error = simple_getattr(mnt, dentry, stat);
508                 stat->nlink = stat->size = 2;
509                 stat->atime = CURRENT_TIME;
510                 ZFS_EXIT(zsb);
511                 return (error);
512         }
513
514         error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
515         if (error == 0) {
516                 error = -zfs_getattr_fast(ZTOI(dzp), stat);
517                 iput(ZTOI(dzp));
518         }
519
520         ZFS_EXIT(zsb);
521         ASSERT3S(error, <=, 0);
522
523         return (error);
524 }
525
526 /*
527  * The '.zfs/shares' directory file operations.
528  */
529 const struct file_operations zpl_fops_shares = {
530         .open           = zpl_common_open,
531         .llseek         = generic_file_llseek,
532         .read           = generic_read_dir,
533 #ifdef HAVE_VFS_ITERATE
534         .iterate        = zpl_shares_iterate,
535 #else
536         .readdir        = zpl_shares_readdir,
537 #endif
538
539 };
540
541 /*
542  * The '.zfs/shares' directory inode operations.
543  */
544 const struct inode_operations zpl_ops_shares = {
545         .lookup         = zpl_shares_lookup,
546         .getattr        = zpl_shares_getattr,
547 };