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