Linux 3.11 compat: fops->iterate()
[zfs.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         cred_t *cr = CRED();
230         struct inode *ip = NULL;
231         int error;
232
233         crhold(cr);
234         error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
235             0, cr, NULL, NULL);
236         ASSERT3S(error, <=, 0);
237         crfree(cr);
238
239         if (error && error != -ENOENT)
240                 return ERR_PTR(error);
241
242         ASSERT(error == 0 || ip == NULL);
243         d_clear_d_op(dentry);
244         d_set_d_op(dentry, &zpl_dops_snapdirs);
245
246         return d_splice_alias(ip, dentry);
247 }
248
249 static int
250 zpl_snapdir_iterate(struct file *filp, struct dir_context *ctx)
251 {
252         zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
253         char snapname[MAXNAMELEN];
254         boolean_t case_conflict;
255         uint64_t id;
256         int error = 0;
257
258         ZFS_ENTER(zsb);
259
260         if (!dir_emit_dots(filp, ctx))
261                 goto out;
262
263         while (error == 0) {
264                 error = -dmu_snapshot_list_next(zsb->z_os, MAXNAMELEN,
265                     snapname, &id, &(ctx->pos), &case_conflict);
266                 if (error)
267                         goto out;
268
269                 if (!dir_emit(ctx, snapname, strlen(snapname),
270                     ZFSCTL_INO_SHARES - id, DT_DIR))
271                         goto out;
272         }
273 out:
274         ZFS_EXIT(zsb);
275
276         if (error == -ENOENT)
277                 return (0);
278
279         return (error);
280 }
281
282 #if !defined(HAVE_VFS_ITERATE)
283 static int
284 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
285 {
286         struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
287         int error;
288
289         error = zpl_snapdir_iterate(filp, &ctx);
290         filp->f_pos = ctx.pos;
291
292         return (error);
293 }
294 #endif /* HAVE_VFS_ITERATE */
295
296 int
297 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
298     struct inode *tdip, struct dentry *tdentry)
299 {
300         cred_t *cr = CRED();
301         int error;
302
303         crhold(cr);
304         error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
305             tdip, dname(tdentry), cr, 0);
306         ASSERT3S(error, <=, 0);
307         crfree(cr);
308
309         return (error);
310 }
311
312 static int
313 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
314 {
315         cred_t *cr = CRED();
316         int error;
317
318         crhold(cr);
319         error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
320         ASSERT3S(error, <=, 0);
321         crfree(cr);
322
323         return (error);
324 }
325
326 static int
327 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, zpl_umode_t mode)
328 {
329         cred_t *cr = CRED();
330         vattr_t *vap;
331         struct inode *ip;
332         int error;
333
334         crhold(cr);
335         vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
336         zpl_vap_init(vap, dip, mode | S_IFDIR, cr);
337
338         error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
339         if (error == 0) {
340                 d_clear_d_op(dentry);
341                 d_set_d_op(dentry, &zpl_dops_snapdirs);
342                 d_instantiate(dentry, ip);
343         }
344
345         kmem_free(vap, sizeof(vattr_t));
346         ASSERT3S(error, <=, 0);
347         crfree(cr);
348
349         return (error);
350 }
351
352 /*
353  * Get snapshot directory attributes.
354  */
355 /* ARGSUSED */
356 static int
357 zpl_snapdir_getattr(struct vfsmount *mnt, struct dentry *dentry,
358     struct kstat *stat)
359 {
360         zfs_sb_t *zsb = ITOZSB(dentry->d_inode);
361         int error;
362
363         ZFS_ENTER(zsb);
364         error = simple_getattr(mnt, dentry, stat);
365         stat->nlink = stat->size = avl_numnodes(&zsb->z_ctldir_snaps) + 2;
366         stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zsb->z_os);
367         stat->atime = CURRENT_TIME;
368         ZFS_EXIT(zsb);
369
370         return (error);
371 }
372
373 /*
374  * The '.zfs/snapshot' directory file operations.  These mainly control
375  * generating the list of available snapshots when doing an 'ls' in the
376  * directory.  See zpl_snapdir_readdir().
377  */
378 const struct file_operations zpl_fops_snapdir = {
379         .open           = zpl_common_open,
380         .llseek         = generic_file_llseek,
381         .read           = generic_read_dir,
382 #ifdef HAVE_VFS_ITERATE
383         .iterate        = zpl_snapdir_iterate,
384 #else
385         .readdir        = zpl_snapdir_readdir,
386 #endif
387
388 };
389
390 /*
391  * The '.zfs/snapshot' directory inode operations.  These mainly control
392  * creating an inode for a snapshot directory and initializing the needed
393  * infrastructure to automount the snapshot.  See zpl_snapdir_lookup().
394  */
395 const struct inode_operations zpl_ops_snapdir = {
396         .lookup         = zpl_snapdir_lookup,
397         .getattr        = zpl_snapdir_getattr,
398         .rename         = zpl_snapdir_rename,
399         .rmdir          = zpl_snapdir_rmdir,
400         .mkdir          = zpl_snapdir_mkdir,
401 };
402
403 static struct dentry *
404 #ifdef HAVE_LOOKUP_NAMEIDATA
405 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
406     struct nameidata *nd)
407 #else
408 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
409     unsigned int flags)
410 #endif
411 {
412         cred_t *cr = CRED();
413         struct inode *ip = NULL;
414         int error;
415
416         crhold(cr);
417         error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
418             0, cr, NULL, NULL);
419         ASSERT3S(error, <=, 0);
420         crfree(cr);
421
422         if (error) {
423                 if (error == -ENOENT)
424                         return d_splice_alias(NULL, dentry);
425                 else
426                         return ERR_PTR(error);
427         }
428
429         return d_splice_alias(ip, dentry);
430 }
431
432 static int
433 zpl_shares_iterate(struct file *filp, struct dir_context *ctx)
434 {
435         cred_t *cr = CRED();
436         zfs_sb_t *zsb = ITOZSB(filp->f_path.dentry->d_inode);
437         znode_t *dzp;
438         int error = 0;
439
440         ZFS_ENTER(zsb);
441
442         if (zsb->z_shares_dir == 0) {
443                 dir_emit_dots(filp, ctx);
444                 goto out;
445         }
446
447         error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
448         if (error)
449                 goto out;
450
451         crhold(cr);
452         error = -zfs_readdir(ZTOI(dzp), ctx, cr);
453         crfree(cr);
454
455         iput(ZTOI(dzp));
456 out:
457         ZFS_EXIT(zsb);
458         ASSERT3S(error, <=, 0);
459
460         return (error);
461 }
462
463 #if !defined(HAVE_VFS_ITERATE)
464 static int
465 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
466 {
467         struct dir_context ctx = DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
468         int error;
469
470         error = zpl_shares_iterate(filp, &ctx);
471         filp->f_pos = ctx.pos;
472
473         return (error);
474 }
475 #endif /* HAVE_VFS_ITERATE */
476
477 /* ARGSUSED */
478 static int
479 zpl_shares_getattr(struct vfsmount *mnt, struct dentry *dentry,
480     struct kstat *stat)
481 {
482         struct inode *ip = dentry->d_inode;
483         zfs_sb_t *zsb = ITOZSB(ip);
484         znode_t *dzp;
485         int error;
486
487         ZFS_ENTER(zsb);
488
489         if (zsb->z_shares_dir == 0) {
490                 error = simple_getattr(mnt, dentry, stat);
491                 stat->nlink = stat->size = 2;
492                 stat->atime = CURRENT_TIME;
493                 ZFS_EXIT(zsb);
494                 return (error);
495         }
496
497         error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
498         if (error == 0)
499                 error = -zfs_getattr_fast(dentry->d_inode, stat);
500
501         iput(ZTOI(dzp));
502         ZFS_EXIT(zsb);
503         ASSERT3S(error, <=, 0);
504
505         return (error);
506 }
507
508 /*
509  * The '.zfs/shares' directory file operations.
510  */
511 const struct file_operations zpl_fops_shares = {
512         .open           = zpl_common_open,
513         .llseek         = generic_file_llseek,
514         .read           = generic_read_dir,
515 #ifdef HAVE_VFS_ITERATE
516         .iterate        = zpl_shares_iterate,
517 #else
518         .readdir        = zpl_shares_readdir,
519 #endif
520
521 };
522
523 /*
524  * The '.zfs/shares' directory inode operations.
525  */
526 const struct inode_operations zpl_ops_shares = {
527         .lookup         = zpl_shares_lookup,
528         .getattr        = zpl_shares_getattr,
529 };