]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_ctldir.c
Avoid ELOOP on auto-mounted snapshots
[mirror_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 static struct dentry *
187 #ifdef HAVE_LOOKUP_NAMEIDATA
188 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
189 struct nameidata *nd)
190 #else
191 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
192 unsigned int flags)
193 #endif
194
195 {
196 cred_t *cr = CRED();
197 struct inode *ip;
198 int error;
199
200 crhold(cr);
201 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
202 0, cr, NULL, NULL);
203 ASSERT3S(error, <=, 0);
204 crfree(cr);
205
206 if (error) {
207 if (error == -ENOENT)
208 return d_splice_alias(NULL, dentry);
209 else
210 return ERR_PTR(error);
211 }
212
213 /*
214 * Auto mounting of snapshots is only supported for 2.6.37 and
215 * newer kernels. Prior to this kernel the ops->follow_link()
216 * callback was used as a hack to trigger the mount. The
217 * resulting vfsmount was then explicitly grafted in to the
218 * name space. While it might be possible to add compatibility
219 * code to accomplish this it would require considerable care.
220 */
221 #ifdef HAVE_AUTOMOUNT
222 dentry->d_op = &zpl_dops_snapdirs;
223 #endif /* HAVE_AUTOMOUNT */
224
225 return d_splice_alias(ip, dentry);
226 }
227
228 /* ARGSUSED */
229 static int
230 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
231 {
232 struct dentry *dentry = filp->f_path.dentry;
233 struct inode *dip = dentry->d_inode;
234 zfs_sb_t *zsb = ITOZSB(dip);
235 char snapname[MAXNAMELEN];
236 uint64_t id, cookie;
237 boolean_t case_conflict;
238 int error = 0;
239
240 ZFS_ENTER(zsb);
241
242 cookie = filp->f_pos;
243 switch (filp->f_pos) {
244 case 0:
245 error = filldir(dirent, ".", 1, 0, dip->i_ino, DT_DIR);
246 if (error)
247 goto out;
248
249 filp->f_pos++;
250 /* fall-thru */
251 case 1:
252 error = filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR);
253 if (error)
254 goto out;
255
256 filp->f_pos++;
257 /* fall-thru */
258 default:
259 while (error == 0) {
260 error = -dmu_snapshot_list_next(zsb->z_os, MAXNAMELEN,
261 snapname, &id, &cookie, &case_conflict);
262 if (error)
263 goto out;
264
265 error = filldir(dirent, snapname, strlen(snapname),
266 filp->f_pos, ZFSCTL_INO_SHARES - id, DT_DIR);
267 if (error)
268 goto out;
269
270 filp->f_pos = cookie;
271 }
272 }
273 out:
274 ZFS_EXIT(zsb);
275
276 if (error == -ENOENT)
277 return (0);
278
279 return (error);
280 }
281
282 int
283 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
284 struct inode *tdip, struct dentry *tdentry)
285 {
286 cred_t *cr = CRED();
287 int error;
288
289 crhold(cr);
290 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
291 tdip, dname(tdentry), cr, 0);
292 ASSERT3S(error, <=, 0);
293 crfree(cr);
294
295 return (error);
296 }
297
298 static int
299 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
300 {
301 cred_t *cr = CRED();
302 int error;
303
304 crhold(cr);
305 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
306 ASSERT3S(error, <=, 0);
307 crfree(cr);
308
309 return (error);
310 }
311
312 static int
313 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, zpl_umode_t mode)
314 {
315 cred_t *cr = CRED();
316 vattr_t *vap;
317 struct inode *ip;
318 int error;
319
320 crhold(cr);
321 vap = kmem_zalloc(sizeof(vattr_t), KM_SLEEP);
322 zpl_vap_init(vap, dip, dentry, mode | S_IFDIR, cr);
323
324 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
325 if (error == 0) {
326 #ifdef HAVE_AUTOMOUNT
327 dentry->d_op = &zpl_dops_snapdirs;
328 #endif /* HAVE_AUTOMOUNT */
329 d_instantiate(dentry, ip);
330 }
331
332 kmem_free(vap, sizeof(vattr_t));
333 ASSERT3S(error, <=, 0);
334 crfree(cr);
335
336 return (error);
337 }
338
339 #ifdef HAVE_AUTOMOUNT
340 static struct vfsmount *
341 zpl_snapdir_automount(struct path *path)
342 {
343 struct dentry *dentry = path->dentry;
344 int error;
345
346 /*
347 * We must briefly disable automounts for this dentry because the
348 * user space mount utility will trigger another lookup on this
349 * directory. That will result in zpl_snapdir_automount() being
350 * called repeatedly. The DCACHE_NEED_AUTOMOUNT flag can be
351 * safely reset once the mount completes.
352 */
353 dentry->d_flags &= ~DCACHE_NEED_AUTOMOUNT;
354 error = -zfsctl_mount_snapshot(path, 0);
355 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
356 if (error)
357 return ERR_PTR(error);
358
359 /*
360 * Ensure path->dentry points to the dentry for the root of the
361 * newly-mounted snapshot, otherwise this function may be called
362 * repeatedly which can lead to an incorrect ELOOP error return.
363 */
364 follow_up(path);
365
366 /*
367 * Rather than returning the new vfsmount for the snapshot we must
368 * return NULL to indicate a mount collision. This is done because
369 * the user space mount calls do_add_mount() which adds the vfsmount
370 * to the name space. If we returned the new mount here it would be
371 * added again to the vfsmount list resulting in list corruption.
372 */
373 return (NULL);
374 }
375 #endif /* HAVE_AUTOMOUNT */
376
377 /*
378 * Get snapshot directory attributes.
379 */
380 /* ARGSUSED */
381 static int
382 zpl_snapdir_getattr(struct vfsmount *mnt, struct dentry *dentry,
383 struct kstat *stat)
384 {
385 zfs_sb_t *zsb = ITOZSB(dentry->d_inode);
386 int error;
387
388 ZFS_ENTER(zsb);
389 error = simple_getattr(mnt, dentry, stat);
390 stat->nlink = stat->size = avl_numnodes(&zsb->z_ctldir_snaps) + 2;
391 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zsb->z_os);
392 stat->atime = CURRENT_TIME;
393 ZFS_EXIT(zsb);
394
395 return (error);
396 }
397
398 /*
399 * The '.zfs/snapshot' directory file operations. These mainly control
400 * generating the list of available snapshots when doing an 'ls' in the
401 * directory. See zpl_snapdir_readdir().
402 */
403 const struct file_operations zpl_fops_snapdir = {
404 .open = zpl_common_open,
405 .llseek = generic_file_llseek,
406 .read = generic_read_dir,
407 .readdir = zpl_snapdir_readdir,
408 };
409
410 /*
411 * The '.zfs/snapshot' directory inode operations. These mainly control
412 * creating an inode for a snapshot directory and initializing the needed
413 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
414 */
415 const struct inode_operations zpl_ops_snapdir = {
416 .lookup = zpl_snapdir_lookup,
417 .getattr = zpl_snapdir_getattr,
418 .rename = zpl_snapdir_rename,
419 .rmdir = zpl_snapdir_rmdir,
420 .mkdir = zpl_snapdir_mkdir,
421 };
422
423 #ifdef HAVE_AUTOMOUNT
424 const struct dentry_operations zpl_dops_snapdirs = {
425 .d_automount = zpl_snapdir_automount,
426 };
427 #endif /* HAVE_AUTOMOUNT */
428
429 static struct dentry *
430 #ifdef HAVE_LOOKUP_NAMEIDATA
431 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
432 struct nameidata *nd)
433 #else
434 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
435 unsigned int flags)
436 #endif
437 {
438 cred_t *cr = CRED();
439 struct inode *ip = NULL;
440 int error;
441
442 crhold(cr);
443 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
444 0, cr, NULL, NULL);
445 ASSERT3S(error, <=, 0);
446 crfree(cr);
447
448 if (error) {
449 if (error == -ENOENT)
450 return d_splice_alias(NULL, dentry);
451 else
452 return ERR_PTR(error);
453 }
454
455 return d_splice_alias(ip, dentry);
456 }
457
458 /* ARGSUSED */
459 static int
460 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
461 {
462 cred_t *cr = CRED();
463 struct dentry *dentry = filp->f_path.dentry;
464 struct inode *ip = dentry->d_inode;
465 zfs_sb_t *zsb = ITOZSB(ip);
466 znode_t *dzp;
467 int error;
468
469 ZFS_ENTER(zsb);
470
471 if (zsb->z_shares_dir == 0) {
472 error = zpl_common_readdir(filp, dirent, filldir);
473 ZFS_EXIT(zsb);
474 return (error);
475 }
476
477 error = -zfs_zget(zsb, zsb->z_shares_dir, &dzp);
478 if (error) {
479 ZFS_EXIT(zsb);
480 return (error);
481 }
482
483 crhold(cr);
484 error = -zfs_readdir(ZTOI(dzp), dirent, filldir, &filp->f_pos, cr);
485 crfree(cr);
486
487 iput(ZTOI(dzp));
488 ZFS_EXIT(zsb);
489 ASSERT3S(error, <=, 0);
490
491 return (error);
492 }
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(dentry->d_inode, stat);
517
518 iput(ZTOI(dzp));
519 ZFS_EXIT(zsb);
520 ASSERT3S(error, <=, 0);
521
522 return (error);
523 }
524
525 /*
526 * The '.zfs/shares' directory file operations.
527 */
528 const struct file_operations zpl_fops_shares = {
529 .open = zpl_common_open,
530 .llseek = generic_file_llseek,
531 .read = generic_read_dir,
532 .readdir = zpl_shares_readdir,
533 };
534
535 /*
536 * The '.zfs/shares' directory inode operations.
537 */
538 const struct inode_operations zpl_ops_shares = {
539 .lookup = zpl_shares_lookup,
540 .getattr = zpl_shares_getattr,
541 };