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