Fix O_APPEND for Linux 3.15 and older kernels
[zfs.git] / module / os / linux / zfs / zpl_ctldir.c
blobf4e1ab7aef08e4c425a3ff8583ee32f475ba50da
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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>
30 #include <sys/zfs_znode.h>
31 #include <sys/zfs_vfsops.h>
32 #include <sys/zfs_vnops.h>
33 #include <sys/zfs_ctldir.h>
34 #include <sys/zpl.h>
37 * Common open routine. Disallow any write access.
39 static int
40 zpl_common_open(struct inode *ip, struct file *filp)
42 if (filp->f_mode & FMODE_WRITE)
43 return (-EACCES);
45 return (generic_file_open(ip, filp));
49 * Get root directory contents.
51 static int
52 zpl_root_iterate(struct file *filp, zpl_dir_context_t *ctx)
54 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
55 int error = 0;
57 ZPL_ENTER(zfsvfs);
59 if (!zpl_dir_emit_dots(filp, ctx))
60 goto out;
62 if (ctx->pos == 2) {
63 if (!zpl_dir_emit(ctx, ZFS_SNAPDIR_NAME,
64 strlen(ZFS_SNAPDIR_NAME), ZFSCTL_INO_SNAPDIR, DT_DIR))
65 goto out;
67 ctx->pos++;
70 if (ctx->pos == 3) {
71 if (!zpl_dir_emit(ctx, ZFS_SHAREDIR_NAME,
72 strlen(ZFS_SHAREDIR_NAME), ZFSCTL_INO_SHARES, DT_DIR))
73 goto out;
75 ctx->pos++;
77 out:
78 ZPL_EXIT(zfsvfs);
80 return (error);
83 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
84 static int
85 zpl_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
87 zpl_dir_context_t ctx =
88 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
89 int error;
91 error = zpl_root_iterate(filp, &ctx);
92 filp->f_pos = ctx.pos;
94 return (error);
96 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
99 * Get root directory attributes.
101 static int
102 #ifdef HAVE_USERNS_IOPS_GETATTR
103 zpl_root_getattr_impl(struct user_namespace *user_ns,
104 const struct path *path, struct kstat *stat, u32 request_mask,
105 unsigned int query_flags)
106 #else
107 zpl_root_getattr_impl(const struct path *path, struct kstat *stat,
108 u32 request_mask, unsigned int query_flags)
109 #endif
111 (void) request_mask, (void) query_flags;
112 struct inode *ip = path->dentry->d_inode;
114 #ifdef HAVE_USERNS_IOPS_GETATTR
115 #ifdef HAVE_GENERIC_FILLATTR_USERNS
116 generic_fillattr(user_ns, ip, stat);
117 #else
118 (void) user_ns;
119 #endif
120 #else
121 generic_fillattr(ip, stat);
122 #endif
123 stat->atime = current_time(ip);
125 return (0);
127 ZPL_GETATTR_WRAPPER(zpl_root_getattr);
129 static struct dentry *
130 zpl_root_lookup(struct inode *dip, struct dentry *dentry, unsigned int flags)
132 cred_t *cr = CRED();
133 struct inode *ip;
134 int error;
136 crhold(cr);
137 error = -zfsctl_root_lookup(dip, dname(dentry), &ip, 0, cr, NULL, NULL);
138 ASSERT3S(error, <=, 0);
139 crfree(cr);
141 if (error) {
142 if (error == -ENOENT)
143 return (d_splice_alias(NULL, dentry));
144 else
145 return (ERR_PTR(error));
148 return (d_splice_alias(ip, dentry));
152 * The '.zfs' control directory file and inode operations.
154 const struct file_operations zpl_fops_root = {
155 .open = zpl_common_open,
156 .llseek = generic_file_llseek,
157 .read = generic_read_dir,
158 #ifdef HAVE_VFS_ITERATE_SHARED
159 .iterate_shared = zpl_root_iterate,
160 #elif defined(HAVE_VFS_ITERATE)
161 .iterate = zpl_root_iterate,
162 #else
163 .readdir = zpl_root_readdir,
164 #endif
167 const struct inode_operations zpl_ops_root = {
168 .lookup = zpl_root_lookup,
169 .getattr = zpl_root_getattr,
172 static struct vfsmount *
173 zpl_snapdir_automount(struct path *path)
175 int error;
177 error = -zfsctl_snapshot_mount(path, 0);
178 if (error)
179 return (ERR_PTR(error));
182 * Rather than returning the new vfsmount for the snapshot we must
183 * return NULL to indicate a mount collision. This is done because
184 * the user space mount calls do_add_mount() which adds the vfsmount
185 * to the name space. If we returned the new mount here it would be
186 * added again to the vfsmount list resulting in list corruption.
188 return (NULL);
192 * Negative dentries must always be revalidated so newly created snapshots
193 * can be detected and automounted. Normal dentries should be kept because
194 * as of the 3.18 kernel revaliding the mountpoint dentry will result in
195 * the snapshot being immediately unmounted.
197 static int
198 #ifdef HAVE_D_REVALIDATE_NAMEIDATA
199 zpl_snapdir_revalidate(struct dentry *dentry, struct nameidata *i)
200 #else
201 zpl_snapdir_revalidate(struct dentry *dentry, unsigned int flags)
202 #endif
204 return (!!dentry->d_inode);
207 static const dentry_operations_t zpl_dops_snapdirs = {
209 * Auto mounting of snapshots is only supported for 2.6.37 and
210 * newer kernels. Prior to this kernel the ops->follow_link()
211 * callback was used as a hack to trigger the mount. The
212 * resulting vfsmount was then explicitly grafted in to the
213 * name space. While it might be possible to add compatibility
214 * code to accomplish this it would require considerable care.
216 .d_automount = zpl_snapdir_automount,
217 .d_revalidate = zpl_snapdir_revalidate,
220 static struct dentry *
221 zpl_snapdir_lookup(struct inode *dip, struct dentry *dentry,
222 unsigned int flags)
224 fstrans_cookie_t cookie;
225 cred_t *cr = CRED();
226 struct inode *ip = NULL;
227 int error;
229 crhold(cr);
230 cookie = spl_fstrans_mark();
231 error = -zfsctl_snapdir_lookup(dip, dname(dentry), &ip,
232 0, cr, NULL, NULL);
233 ASSERT3S(error, <=, 0);
234 spl_fstrans_unmark(cookie);
235 crfree(cr);
237 if (error && error != -ENOENT)
238 return (ERR_PTR(error));
240 ASSERT(error == 0 || ip == NULL);
241 d_clear_d_op(dentry);
242 d_set_d_op(dentry, &zpl_dops_snapdirs);
243 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
245 return (d_splice_alias(ip, dentry));
248 static int
249 zpl_snapdir_iterate(struct file *filp, zpl_dir_context_t *ctx)
251 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
252 fstrans_cookie_t cookie;
253 char snapname[MAXNAMELEN];
254 boolean_t case_conflict;
255 uint64_t id, pos;
256 int error = 0;
258 ZPL_ENTER(zfsvfs);
259 cookie = spl_fstrans_mark();
261 if (!zpl_dir_emit_dots(filp, ctx))
262 goto out;
264 /* Start the position at 0 if it already emitted . and .. */
265 pos = (ctx->pos == 2 ? 0 : ctx->pos);
266 while (error == 0) {
267 dsl_pool_config_enter(dmu_objset_pool(zfsvfs->z_os), FTAG);
268 error = -dmu_snapshot_list_next(zfsvfs->z_os, MAXNAMELEN,
269 snapname, &id, &pos, &case_conflict);
270 dsl_pool_config_exit(dmu_objset_pool(zfsvfs->z_os), FTAG);
271 if (error)
272 goto out;
274 if (!zpl_dir_emit(ctx, snapname, strlen(snapname),
275 ZFSCTL_INO_SHARES - id, DT_DIR))
276 goto out;
278 ctx->pos = pos;
280 out:
281 spl_fstrans_unmark(cookie);
282 ZPL_EXIT(zfsvfs);
284 if (error == -ENOENT)
285 return (0);
287 return (error);
290 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
291 static int
292 zpl_snapdir_readdir(struct file *filp, void *dirent, filldir_t filldir)
294 zpl_dir_context_t ctx =
295 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
296 int error;
298 error = zpl_snapdir_iterate(filp, &ctx);
299 filp->f_pos = ctx.pos;
301 return (error);
303 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
305 static int
306 #ifdef HAVE_IOPS_RENAME_USERNS
307 zpl_snapdir_rename2(struct user_namespace *user_ns, struct inode *sdip,
308 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
309 unsigned int flags)
310 #else
311 zpl_snapdir_rename2(struct inode *sdip, struct dentry *sdentry,
312 struct inode *tdip, struct dentry *tdentry, unsigned int flags)
313 #endif
315 cred_t *cr = CRED();
316 int error;
318 /* We probably don't want to support renameat2(2) in ctldir */
319 if (flags)
320 return (-EINVAL);
322 crhold(cr);
323 error = -zfsctl_snapdir_rename(sdip, dname(sdentry),
324 tdip, dname(tdentry), cr, 0);
325 ASSERT3S(error, <=, 0);
326 crfree(cr);
328 return (error);
331 #if !defined(HAVE_RENAME_WANTS_FLAGS) && !defined(HAVE_IOPS_RENAME_USERNS)
332 static int
333 zpl_snapdir_rename(struct inode *sdip, struct dentry *sdentry,
334 struct inode *tdip, struct dentry *tdentry)
336 return (zpl_snapdir_rename2(sdip, sdentry, tdip, tdentry, 0));
338 #endif
340 static int
341 zpl_snapdir_rmdir(struct inode *dip, struct dentry *dentry)
343 cred_t *cr = CRED();
344 int error;
346 crhold(cr);
347 error = -zfsctl_snapdir_remove(dip, dname(dentry), cr, 0);
348 ASSERT3S(error, <=, 0);
349 crfree(cr);
351 return (error);
354 static int
355 #ifdef HAVE_IOPS_MKDIR_USERNS
356 zpl_snapdir_mkdir(struct user_namespace *user_ns, struct inode *dip,
357 struct dentry *dentry, umode_t mode)
358 #else
359 zpl_snapdir_mkdir(struct inode *dip, struct dentry *dentry, umode_t mode)
360 #endif
362 cred_t *cr = CRED();
363 vattr_t *vap;
364 struct inode *ip;
365 int error;
367 crhold(cr);
368 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
369 zpl_vap_init(vap, dip, mode | S_IFDIR, cr);
371 error = -zfsctl_snapdir_mkdir(dip, dname(dentry), vap, &ip, cr, 0);
372 if (error == 0) {
373 d_clear_d_op(dentry);
374 d_set_d_op(dentry, &zpl_dops_snapdirs);
375 d_instantiate(dentry, ip);
378 kmem_free(vap, sizeof (vattr_t));
379 ASSERT3S(error, <=, 0);
380 crfree(cr);
382 return (error);
386 * Get snapshot directory attributes.
388 static int
389 #ifdef HAVE_USERNS_IOPS_GETATTR
390 zpl_snapdir_getattr_impl(struct user_namespace *user_ns,
391 const struct path *path, struct kstat *stat, u32 request_mask,
392 unsigned int query_flags)
393 #else
394 zpl_snapdir_getattr_impl(const struct path *path, struct kstat *stat,
395 u32 request_mask, unsigned int query_flags)
396 #endif
398 (void) request_mask, (void) query_flags;
399 struct inode *ip = path->dentry->d_inode;
400 zfsvfs_t *zfsvfs = ITOZSB(ip);
402 ZPL_ENTER(zfsvfs);
403 #ifdef HAVE_USERNS_IOPS_GETATTR
404 #ifdef HAVE_GENERIC_FILLATTR_USERNS
405 generic_fillattr(user_ns, ip, stat);
406 #else
407 (void) user_ns;
408 #endif
409 #else
410 generic_fillattr(ip, stat);
411 #endif
413 stat->nlink = stat->size = 2;
414 stat->ctime = stat->mtime = dmu_objset_snap_cmtime(zfsvfs->z_os);
415 stat->atime = current_time(ip);
416 ZPL_EXIT(zfsvfs);
418 return (0);
420 ZPL_GETATTR_WRAPPER(zpl_snapdir_getattr);
423 * The '.zfs/snapshot' directory file operations. These mainly control
424 * generating the list of available snapshots when doing an 'ls' in the
425 * directory. See zpl_snapdir_readdir().
427 const struct file_operations zpl_fops_snapdir = {
428 .open = zpl_common_open,
429 .llseek = generic_file_llseek,
430 .read = generic_read_dir,
431 #ifdef HAVE_VFS_ITERATE_SHARED
432 .iterate_shared = zpl_snapdir_iterate,
433 #elif defined(HAVE_VFS_ITERATE)
434 .iterate = zpl_snapdir_iterate,
435 #else
436 .readdir = zpl_snapdir_readdir,
437 #endif
442 * The '.zfs/snapshot' directory inode operations. These mainly control
443 * creating an inode for a snapshot directory and initializing the needed
444 * infrastructure to automount the snapshot. See zpl_snapdir_lookup().
446 const struct inode_operations zpl_ops_snapdir = {
447 .lookup = zpl_snapdir_lookup,
448 .getattr = zpl_snapdir_getattr,
449 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
450 .rename = zpl_snapdir_rename2,
451 #else
452 .rename = zpl_snapdir_rename,
453 #endif
454 .rmdir = zpl_snapdir_rmdir,
455 .mkdir = zpl_snapdir_mkdir,
458 static struct dentry *
459 zpl_shares_lookup(struct inode *dip, struct dentry *dentry,
460 unsigned int flags)
462 fstrans_cookie_t cookie;
463 cred_t *cr = CRED();
464 struct inode *ip = NULL;
465 int error;
467 crhold(cr);
468 cookie = spl_fstrans_mark();
469 error = -zfsctl_shares_lookup(dip, dname(dentry), &ip,
470 0, cr, NULL, NULL);
471 ASSERT3S(error, <=, 0);
472 spl_fstrans_unmark(cookie);
473 crfree(cr);
475 if (error) {
476 if (error == -ENOENT)
477 return (d_splice_alias(NULL, dentry));
478 else
479 return (ERR_PTR(error));
482 return (d_splice_alias(ip, dentry));
485 static int
486 zpl_shares_iterate(struct file *filp, zpl_dir_context_t *ctx)
488 fstrans_cookie_t cookie;
489 cred_t *cr = CRED();
490 zfsvfs_t *zfsvfs = ITOZSB(file_inode(filp));
491 znode_t *dzp;
492 int error = 0;
494 ZPL_ENTER(zfsvfs);
495 cookie = spl_fstrans_mark();
497 if (zfsvfs->z_shares_dir == 0) {
498 zpl_dir_emit_dots(filp, ctx);
499 goto out;
502 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
503 if (error)
504 goto out;
506 crhold(cr);
507 error = -zfs_readdir(ZTOI(dzp), ctx, cr);
508 crfree(cr);
510 iput(ZTOI(dzp));
511 out:
512 spl_fstrans_unmark(cookie);
513 ZPL_EXIT(zfsvfs);
514 ASSERT3S(error, <=, 0);
516 return (error);
519 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
520 static int
521 zpl_shares_readdir(struct file *filp, void *dirent, filldir_t filldir)
523 zpl_dir_context_t ctx =
524 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
525 int error;
527 error = zpl_shares_iterate(filp, &ctx);
528 filp->f_pos = ctx.pos;
530 return (error);
532 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
534 static int
535 #ifdef HAVE_USERNS_IOPS_GETATTR
536 zpl_shares_getattr_impl(struct user_namespace *user_ns,
537 const struct path *path, struct kstat *stat, u32 request_mask,
538 unsigned int query_flags)
539 #else
540 zpl_shares_getattr_impl(const struct path *path, struct kstat *stat,
541 u32 request_mask, unsigned int query_flags)
542 #endif
544 (void) request_mask, (void) query_flags;
545 struct inode *ip = path->dentry->d_inode;
546 zfsvfs_t *zfsvfs = ITOZSB(ip);
547 znode_t *dzp;
548 int error;
550 ZPL_ENTER(zfsvfs);
552 if (zfsvfs->z_shares_dir == 0) {
553 #ifdef HAVE_USERNS_IOPS_GETATTR
554 #ifdef HAVE_GENERIC_FILLATTR_USERNS
555 generic_fillattr(user_ns, path->dentry->d_inode, stat);
556 #else
557 (void) user_ns;
558 #endif
559 #else
560 generic_fillattr(path->dentry->d_inode, stat);
561 #endif
562 stat->nlink = stat->size = 2;
563 stat->atime = current_time(ip);
564 ZPL_EXIT(zfsvfs);
565 return (0);
568 error = -zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &dzp);
569 if (error == 0) {
570 #ifdef HAVE_USERNS_IOPS_GETATTR
571 #ifdef HAVE_GENERIC_FILLATTR_USERNS
572 error = -zfs_getattr_fast(user_ns, ZTOI(dzp), stat);
573 #else
574 (void) user_ns;
575 #endif
576 #else
577 error = -zfs_getattr_fast(kcred->user_ns, ZTOI(dzp), stat);
578 #endif
579 iput(ZTOI(dzp));
582 ZPL_EXIT(zfsvfs);
583 ASSERT3S(error, <=, 0);
585 return (error);
587 ZPL_GETATTR_WRAPPER(zpl_shares_getattr);
590 * The '.zfs/shares' directory file operations.
592 const struct file_operations zpl_fops_shares = {
593 .open = zpl_common_open,
594 .llseek = generic_file_llseek,
595 .read = generic_read_dir,
596 #ifdef HAVE_VFS_ITERATE_SHARED
597 .iterate_shared = zpl_shares_iterate,
598 #elif defined(HAVE_VFS_ITERATE)
599 .iterate = zpl_shares_iterate,
600 #else
601 .readdir = zpl_shares_readdir,
602 #endif
607 * The '.zfs/shares' directory inode operations.
609 const struct inode_operations zpl_ops_shares = {
610 .lookup = zpl_shares_lookup,
611 .getattr = zpl_shares_getattr,