usr/src/uts/common/fs/zfs/zfs_vnops.c
author Neil Perrin <Neil.Perrin@Sun.COM>
Tue, 25 Nov 2008 13:18:25 -0700
changeset 8227 f7d7be9b1f56
parent 8190 6eeea43ced42
child 8636 7e4ce9158df3
permissions -rw-r--r--
6765294 Refactor replay
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1460
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
     5
 * Common Development and Distribution License (the "License").
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
    22
 * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Use is subject to license terms.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
4144
068f395736ad 6465105 ZFS does not update timestamps upon the creat() of an existing file
peteh
parents: 3912
diff changeset
    26
/* Portions Copyright 2007 Jeremy Teo */
068f395736ad 6465105 ZFS does not update timestamps upon the creat() of an existing file
peteh
parents: 3912
diff changeset
    27
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
#include <sys/types.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/param.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/time.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/systm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/sysmacros.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/resource.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/vfs.h>
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
    35
#include <sys/vfs_opreg.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/vnode.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/file.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/kmem.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <sys/taskq.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <sys/uio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <sys/vmsystm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <sys/atomic.h>
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
    44
#include <sys/vm.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
#include <vm/seg_vn.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
#include <vm/pvn.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include <vm/as.h>
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
    48
#include <vm/kpm.h>
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
    49
#include <vm/seg_kpm.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
#include <sys/mman.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#include <sys/pathname.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
#include <sys/cmn_err.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
#include <sys/errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
#include <sys/unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
#include <sys/zfs_dir.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
#include <sys/zfs_acl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
#include <sys/zfs_ioctl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
#include <sys/fs/zfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
#include <sys/txg.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
#include <sys/dbuf.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
#include <sys/zap.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
#include <sys/dirent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
#include <sys/policy.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
#include <sys/sunddi.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
#include <sys/filio.h>
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
    68
#include <sys/sid.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
#include "fs/fs_subr.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
#include <sys/zfs_ctldir.h>
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    71
#include <sys/zfs_fuid.h>
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1472
diff changeset
    72
#include <sys/dnlc.h>
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
    73
#include <sys/zfs_rlock.h>
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    74
#include <sys/extdirent.h>
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    75
#include <sys/kidmap.h>
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    76
#include <sys/cred_impl.h>
5663
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
    77
#include <sys/attr.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
 * Programming rules.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
 * Each vnode op performs some logical unit of work.  To do this, the ZPL must
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
 * properly lock its in-core state, create a DMU transaction, do the work,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 * record this work in the intent log (ZIL), commit the DMU transaction,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    85
 * and wait for the intent log to commit if it is a synchronous operation.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
    86
 * Moreover, the vnode ops must work in both normal and log replay context.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
 * The ordering of events is important to avoid deadlocks and references
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
 * to freed memory.  The example below illustrates the following Big Rules:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
 *  (1) A check must be made in each zfs thread for a mounted file system.
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
    91
 *	This is done avoiding races using ZFS_ENTER(zfsvfs).
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
    92
 *      A ZFS_EXIT(zfsvfs) is needed before all returns.  Any znodes
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
    93
 *      must be checked with ZFS_VERIFY_ZP(zp).  Both of these macros
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
    94
 *      can return EIO from the calling function.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
 *  (2)	VN_RELE() should always be the last thing except for zil_commit()
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
    97
 *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
 *	First, if it's the last reference, the vnode/znode
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
 *	can be freed, so the zp may point to freed memory.  Second, the last
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
 *	reference will call zfs_zinactive(), which may induce a lot of work --
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   101
 *	pushing cached pages (which acquires range locks) and syncing out
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
 *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
 *	which could deadlock the system if you were already holding one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
 *
1757
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   105
 *  (3)	All range locks must be grabbed before calling dmu_tx_assign(),
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   106
 *	as they can span dmu_tx_assign() calls.
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   107
 *
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   108
 *  (4)	Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
 *	This is critical because we don't want to block while holding locks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
 *	Note, in particular, that if a lock is sometimes acquired before
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
 *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
 *	use a non-blocking assign can deadlock the system.  The scenario:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
 *	Thread A has grabbed a lock before calling dmu_tx_assign().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
 *	Thread B is in an already-assigned tx, and blocks for this lock.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
 *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
 *	forever, because the previous txg can't quiesce until B's tx commits.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
 *	If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   120
 *	then drop all locks, call dmu_tx_wait(), and try again.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
 *
1757
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   122
 *  (5)	If the operation succeeded, generate the intent log entry for it
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
 *	before dropping locks.  This ensures that the ordering of events
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
 *	in the intent log matches the order in which they actually occurred.
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   125
 *      During ZIL replay the zfs_log_* functions will update the sequence
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   126
 *	number to indicate the zil transaction has replayed.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
 *
1757
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   128
 *  (6)	At the end of each vnode op, the DMU tx must always commit,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 *	regardless of whether there were any errors.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
 *
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   131
 *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
 *	to ensure that synchronous semantics are provided when necessary.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
 * In general, this is how things should be ordered in each vnode op:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
 *	ZFS_ENTER(zfsvfs);		// exit if unmounted
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
 * top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
 *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
 *	rw_enter(...);			// grab any other locks you need
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
 *	tx = dmu_tx_create(...);	// get DMU tx
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
 *	dmu_tx_hold_*();		// hold each object you might modify
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   142
 *	error = dmu_tx_assign(tx, TXG_NOWAIT);	// try to assign
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
 *	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
 *		rw_exit(...);		// drop locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
 *		zfs_dirent_unlock(dl);	// unlock directory entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
 *		VN_RELE(...);		// release held vnodes
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   147
 *		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   148
 *			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   149
 *			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
 *			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
 *		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   152
 *		dmu_tx_abort(tx);	// abort DMU tx
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
 *		ZFS_EXIT(zfsvfs);	// finished in zfs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
 *		return (error);		// really out of space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
 *	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
 *	error = do_real_work();		// do whatever this VOP does
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
 *	if (error == 0)
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   158
 *		zfs_log_*(...);		// on success, make ZIL entry
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
 *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
 *	rw_exit(...);			// drop locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
 *	zfs_dirent_unlock(dl);		// unlock directory entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
 *	VN_RELE(...);			// release held vnodes
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   163
 *	zil_commit(zilog, seq, foid);	// synchronous when necessary
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
 *	ZFS_EXIT(zfsvfs);		// finished in zfs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
 *	return (error);			// done, report error
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
 */
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   167
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   170
zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   172
	znode_t	*zp = VTOZ(*vpp);
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   173
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   174
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   175
	ZFS_ENTER(zfsvfs);
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   176
	ZFS_VERIFY_ZP(zp);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   177
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   178
	if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   179
	    ((flag & FAPPEND) == 0)) {
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   180
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   181
		return (EPERM);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   182
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   183
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   184
	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   185
	    ZTOV(zp)->v_type == VREG &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   186
	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   187
	    zp->z_phys->zp_size > 0) {
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   188
		if (fs_vscan(*vpp, cr, 0) != 0) {
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   189
			ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   190
			return (EACCES);
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   191
		}
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   192
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   193
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   194
	/* Keep a count of the synchronous opens in the znode */
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   195
	if (flag & (FSYNC | FDSYNC))
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   196
		atomic_inc_32(&zp->z_sync_cnt);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   197
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   198
	ZFS_EXIT(zfsvfs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   204
zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   205
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   207
	znode_t	*zp = VTOZ(vp);
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   208
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   209
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   210
	ZFS_ENTER(zfsvfs);
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   211
	ZFS_VERIFY_ZP(zp);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   212
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   213
	/* Decrement the synchronous opens in the znode */
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   214
	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   215
		atomic_dec_32(&zp->z_sync_cnt);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   216
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
	 * Clean up any locks held by this process on the vp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
	cleanlocks(vp, ddi_get_pid(), 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
	cleanshares(vp, ddi_get_pid());
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   223
	if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   224
	    ZTOV(zp)->v_type == VREG &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   225
	    !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   226
	    zp->z_phys->zp_size > 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   227
		VERIFY(fs_vscan(vp, cr, 1) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   228
7844
effed23820ae 6755435 zfs_open() and zfs_close() needs to use ZFS_ENTER/ZFS_VERIFY_ZP
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7757
diff changeset
   229
	ZFS_EXIT(zfsvfs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
zfs_holey(vnode_t *vp, int cmd, offset_t *off)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
	uint64_t noff = (uint64_t)*off; /* new offset */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
	uint64_t file_sz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
	boolean_t hole;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
	file_sz = zp->z_phys->zp_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
	if (noff >= file_sz)  {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
	if (cmd == _FIO_SEEK_HOLE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
		hole = B_TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
		hole = B_FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
	/* end of file? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
	if ((error == ESRCH) || (noff > file_sz)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
		 * Handle the virtual hole at the end of file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
		if (hole) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
			*off = file_sz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	if (noff < *off)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	*off = noff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   279
    int *rvalp, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
	offset_t off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
	zfsvfs_t *zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   284
	znode_t *zp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
	switch (com) {
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   287
	case _FIOFFS:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
		return (zfs_sync(vp->v_vfsp, 0, cred));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   290
		/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   291
		 * The following two ioctls are used by bfu.  Faking out,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   292
		 * necessary to avoid bfu errors.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   293
		 */
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   294
	case _FIOGDIO:
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   295
	case _FIOSDIO:
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   296
		return (0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   297
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   298
	case _FIO_SEEK_DATA:
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   299
	case _FIO_SEEK_HOLE:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
			return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   303
		zp = VTOZ(vp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   304
		zfsvfs = zp->z_zfsvfs;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   305
		ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   306
		ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
		/* offset parameter is in/out */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
		error = zfs_holey(vp, com, &off);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
			return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
	return (ENOTTY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
/*
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   321
 * Utility functions to map and unmap a single physical page.  These
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   322
 * are used to manage the mappable copies of ZFS file data, and therefore
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   323
 * do not update ref/mod bits.
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   324
 */
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   325
caddr_t
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   326
zfs_map_page(page_t *pp, enum seg_rw rw)
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   327
{
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   328
	if (kpm_enable)
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   329
		return (hat_kpm_mapin(pp, 0));
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   330
	ASSERT(rw == S_READ || rw == S_WRITE);
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   331
	return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   332
	    (caddr_t)-1));
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   333
}
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   334
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   335
void
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   336
zfs_unmap_page(page_t *pp, caddr_t addr)
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   337
{
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   338
	if (kpm_enable) {
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   339
		hat_kpm_mapout(pp, 0, addr);
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   340
	} else {
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   341
		ppmapout(addr);
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   342
	}
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   343
}
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   344
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   345
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
 * When a file is memory mapped, we must keep the IO data synchronized
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
 * between the DMU cache and the memory mapped pages.  What this means:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
 * On Write:	If we find a memory mapped page, we write to *both*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
 *		the page and the dmu buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
 *	the file is memory mapped.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
static int
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   356
mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
	int64_t	start, off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
	int len = nbytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
	start = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
	off = start & PAGEOFFSET;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
		page_t *pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
		uint64_t bytes = MIN(PAGESIZE - off, len);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   369
		uint64_t woff = uio->uio_loffset;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
		 * We don't want a new page to "appear" in the middle of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
		 * the file update (because it may not get the write
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
		 * update data), so we grab a lock to block
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
		 * zfs_getpage().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
		rw_enter(&zp->z_map_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
		if (pp = page_lookup(vp, start, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
			caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
			rw_exit(&zp->z_map_lock);
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   382
			va = zfs_map_page(pp, S_WRITE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
			error = uiomove(va+off, bytes, UIO_WRITE, uio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
			if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
				dmu_write(zfsvfs->z_os, zp->z_id,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
				    woff, bytes, va+off, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
			}
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   388
			zfs_unmap_page(pp, va);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
			page_unlock(pp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   392
			    uio, bytes, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
			rw_exit(&zp->z_map_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
		len -= bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
		off = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
 * When a file is memory mapped, we must keep the IO data synchronized
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
 * between the DMU cache and the memory mapped pages.  What this means:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
 * On Read:	We "read" preferentially from memory mapped pages,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
 *		else we default from the dmu buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
 *	the file is memory mapped.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
static int
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   414
mappedread(vnode_t *vp, int nbytes, uio_t *uio)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
{
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   416
	znode_t *zp = VTOZ(vp);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   417
	objset_t *os = zp->z_zfsvfs->z_os;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   418
	int64_t	start, off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
	int len = nbytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
	start = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
	off = start & PAGEOFFSET;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
		page_t *pp;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   426
		uint64_t bytes = MIN(PAGESIZE - off, len);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   427
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
		if (pp = page_lookup(vp, start, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
			caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   431
			va = zfs_map_page(pp, S_READ);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
			error = uiomove(va + off, bytes, UIO_READ, uio);
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
   433
			zfs_unmap_page(pp, va);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
			page_unlock(pp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
		} else {
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   436
			error = dmu_read_uio(os, zp->z_id, uio, bytes);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
		len -= bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
		off = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   446
offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
 * Read bytes from specified file into supplied buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
 *	IN:	vp	- vnode of file to be read from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
 *		uio	- structure supplying read location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
 *			  and return buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
 *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   456
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
 *	OUT:	uio	- updated offset and range, buffer filled.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
 * Side Effects:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
 *	vp - atime updated if byte count > 0
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   472
	objset_t	*os;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   473
	ssize_t		n, nbytes;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   474
	int		error;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   475
	rl_t		*rl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   477
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   478
	ZFS_VERIFY_ZP(zp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   479
	os = zfsvfs->z_os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
5929
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
   481
	if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) {
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
   482
		ZFS_EXIT(zfsvfs);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
   483
		return (EACCES);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
   484
	}
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
   485
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	 * Validate file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	if (uio->uio_loffset < (offset_t)0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
	 * Fasttrack empty reads
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
	if (uio->uio_resid == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   503
	 * Check for mandatory locks
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
		if (error = chklock(vp, FREAD,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
	 * If we're in FRSYNC mode, sync out this znode before reading it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
	 */
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   516
	if (ioflag & FRSYNC)
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   517
		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   520
	 * Lock the range against changes.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   522
	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   523
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
	 * If we are reading past end-of-file we can skip
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	 * to the end; but we might still need to set atime.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
	if (uio->uio_loffset >= zp->z_phys->zp_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   533
	ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   534
	n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   535
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   536
	while (n > 0) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   537
		nbytes = MIN(n, zfs_read_chunk_size -
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   538
		    P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   539
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   540
		if (vn_has_cached_data(vp))
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   541
			error = mappedread(vp, nbytes, uio);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   542
		else
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   543
			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
   544
		if (error) {
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
   545
			/* convert checksum errors into IO errors */
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
   546
			if (error == ECKSUM)
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
   547
				error = EIO;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   548
			break;
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
   549
		}
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   550
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   551
		n -= nbytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
	}
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   553
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   555
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
 * Write the bytes to a file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
 *	IN:	vp	- vnode of file to be written to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
 *		uio	- structure supplying write location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
 *			  and data buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
 *		ioflag	- FAPPEND flag set if in append mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   570
 *		ct	- caller context (NFS/CIFS fem monitor only)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
 *	OUT:	uio	- updated offset and range.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
 *	vp - ctime|mtime updated if byte count > 0
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
	rlim64_t	limit = uio->uio_llimit;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
	ssize_t		start_resid = uio->uio_resid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
	ssize_t		tx_bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
	uint64_t	end_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   591
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
	offset_t	woff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
	ssize_t		n, nbytes;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   594
	rl_t		*rl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
	int		max_blksz = zfsvfs->z_max_blksz;
6743
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   596
	uint64_t	pflags;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   597
	int		error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
	 * Fasttrack empty write
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   602
	n = start_resid;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	if (n == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   606
	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   607
		limit = MAXOFFSET_T;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   608
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   609
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   610
	ZFS_VERIFY_ZP(zp);
6743
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   611
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   612
	/*
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   613
	 * If immutable or not appending then return EPERM
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   614
	 */
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   615
	pflags = zp->z_phys->zp_flags;
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   616
	if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   617
	    ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   618
	    (uio->uio_loffset < zp->z_phys->zp_size))) {
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   619
		ZFS_EXIT(zfsvfs);
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   620
		return (EPERM);
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   621
	}
5176703cbaff 6703554 panic with ZFS and Greenplum: BAD TRAP: type=e (#pf Page fault)
marks
parents: 6492
diff changeset
   622
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   623
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
	/*
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   626
	 * Pre-fault the pages to ensure slow (eg NFS) pages
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   627
	 * don't hold up txg.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
	 */
8059
92e2183c5863 1246893 mmap and write to the same file deadlocks.
Donghai Qiao <Donghai.Qiao@Sun.COM>
parents: 7876
diff changeset
   629
	uio_prefaultpages(n, uio);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
	 * If in append mode, set the io offset pointer to eof.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   634
	if (ioflag & FAPPEND) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   635
		/*
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   636
		 * Range lock for a file append:
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   637
		 * The value for the start of range will be determined by
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   638
		 * zfs_range_lock() (to guarantee append semantics).
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   639
		 * If this write will cause the block size to increase,
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   640
		 * zfs_range_lock() will lock the entire file, so we must
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   641
		 * later reduce the range after we grow the block size.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   642
		 */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   643
		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   644
		if (rl->r_len == UINT64_MAX) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   645
			/* overlocked, zp_size can't change */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   646
			woff = uio->uio_loffset = zp->z_phys->zp_size;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   647
		} else {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   648
			woff = uio->uio_loffset = rl->r_off;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   649
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
		woff = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
		 * Validate file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
		if (woff < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
		/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   661
		 * If we need to grow the block size then zfs_range_lock()
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   662
		 * will lock a wider range than we request here.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   663
		 * Later after growing the block size we reduce the range.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
		 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   665
		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
	if (woff >= limit) {
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   669
		zfs_range_unlock(rl);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   670
		ZFS_EXIT(zfsvfs);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   671
		return (EFBIG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
	if ((woff + n) > limit || woff > (limit - n))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
		n = limit - woff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   678
	 * Check for mandatory locks
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   681
	    (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   682
		zfs_range_unlock(rl);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   683
		ZFS_EXIT(zfsvfs);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   684
		return (error);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   685
	}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   686
	end_size = MAX(zp->z_phys->zp_size, woff + n);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   688
	/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   689
	 * Write the file in reasonable size chunks.  Each chunk is written
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   690
	 * in a separate transaction; this keeps the intent log records small
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   691
	 * and allows us to do more fine-grained space accounting.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
	while (n > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
		/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   695
		 * Start a transaction.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
		woff = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
		tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
		dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   701
		error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
		if (error) {
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   703
			if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   704
				dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   705
				dmu_tx_abort(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   706
				continue;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
			}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   708
			dmu_tx_abort(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   709
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   710
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   711
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   712
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   713
		 * If zfs_range_lock() over-locked we grow the blocksize
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   714
		 * and then reduce the lock range.  This will only happen
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   715
		 * on the first iteration since zfs_range_reduce() will
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   716
		 * shrink down r_len to the appropriate size.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   717
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   718
		if (rl->r_len == UINT64_MAX) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   719
			uint64_t new_blksz;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   720
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   721
			if (zp->z_blksz > max_blksz) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   722
				ASSERT(!ISP2(zp->z_blksz));
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   723
				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   724
			} else {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   725
				new_blksz = MIN(end_size, max_blksz);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   726
			}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   727
			zfs_grow_blocksize(zp, new_blksz, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   728
			zfs_range_reduce(rl, woff, n);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   729
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   730
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   731
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   732
		 * XXX - should we really limit each write to z_max_blksz?
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   733
		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   734
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   735
		nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   736
		rw_enter(&zp->z_map_lock, RW_READER);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   737
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   738
		tx_bytes = uio->uio_resid;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   739
		if (vn_has_cached_data(vp)) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   740
			rw_exit(&zp->z_map_lock);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   741
			error = mappedwrite(vp, nbytes, uio, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   742
		} else {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   743
			error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   744
			    uio, nbytes, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   745
			rw_exit(&zp->z_map_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
		}
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   747
		tx_bytes -= uio->uio_resid;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   748
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   749
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   750
		 * If we made no progress, we're done.  If we made even
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   751
		 * partial progress, update the znode and ZIL accordingly.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   752
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   753
		if (tx_bytes == 0) {
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
   754
			dmu_tx_commit(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   755
			ASSERT(error != 0);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   756
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   757
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   758
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
		/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   760
		 * Clear Set-UID/Set-GID bits on successful write if not
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   761
		 * privileged and at least one of the excute bits is set.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   762
		 *
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   763
		 * It would be nice to to this after all writes have
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   764
		 * been done, but that would still expose the ISUID/ISGID
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   765
		 * to another app after the partial write is committed.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   766
		 *
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   767
		 * Note: we don't call zfs_fuid_map_id() here because
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   768
		 * user 0 is not an ephemeral uid.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
		 */
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   770
		mutex_enter(&zp->z_acl_lock);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   771
		if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) |
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   772
		    (S_IXUSR >> 6))) != 0 &&
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   773
		    (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 &&
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   774
		    secpolicy_vnode_setid_retain(cr,
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   775
		    (zp->z_phys->zp_mode & S_ISUID) != 0 &&
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   776
		    zp->z_phys->zp_uid == 0) != 0) {
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   777
			zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   778
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   779
		mutex_exit(&zp->z_acl_lock);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   780
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   781
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   782
		 * Update time stamp.  NOTE: This marks the bonus buffer as
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   783
		 * dirty, so we don't have to do it again for zp_size.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   784
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   785
		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   786
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   787
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   788
		 * Update the file size (zp_size) if it has changed;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   789
		 * account for possible concurrent updates.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   790
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   791
		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
			    uio->uio_loffset);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   794
		zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   795
		dmu_tx_commit(tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   796
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   797
		if (error != 0)
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   798
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   799
		ASSERT(tx_bytes == nbytes);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   800
		n -= nbytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   803
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
	 * If we're in replay mode, or we made no progress, return error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
	 * Otherwise, it's at least a partial write, so it's successful.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
	 */
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
   809
	if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   814
	if (ioflag & (FSYNC | FDSYNC))
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   815
		zil_commit(zilog, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   821
void
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   822
zfs_get_done(dmu_buf_t *db, void *vzgd)
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   823
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   824
	zgd_t *zgd = (zgd_t *)vzgd;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   825
	rl_t *rl = zgd->zgd_rl;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   826
	vnode_t *vp = ZTOV(rl->r_zp);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   827
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   828
	dmu_buf_rele(db, vzgd);
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   829
	zfs_range_unlock(rl);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   830
	VN_RELE(vp);
5688
c0b02c8fd2c0 6640580 spa_get_random() is insanely slow in userland
bonwick
parents: 5663
diff changeset
   831
	zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   832
	kmem_free(zgd, sizeof (zgd_t));
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   833
}
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   834
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
 * Get data to generate a TX_WRITE intent log record.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
int
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   839
zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
	zfsvfs_t *zfsvfs = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
	objset_t *os = zfsvfs->z_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	znode_t *zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
	uint64_t off = lr->lr_offset;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   845
	dmu_buf_t *db;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   846
	rl_t *rl;
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   847
	zgd_t *zgd;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   848
	int dlen = lr->lr_length;		/* length of user data */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   851
	ASSERT(zio);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
	ASSERT(dlen != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   855
	 * Nothing to do if the file has been removed
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
		return (ENOENT);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   859
	if (zp->z_unlinked) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
		VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
		return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
	 * Write records come in two flavors: immediate and indirect.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	 * For small writes it's cheaper to store the data with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
	 * log record (immediate); for large writes it's cheaper to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
	 * sync the data and get a pointer to it (indirect) so that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
	 * we don't have to write the data twice.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   871
	if (buf != NULL) { /* immediate write */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   872
		rl = zfs_range_lock(zp, off, dlen, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   873
		/* test for truncation needs to be done while range locked */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   874
		if (off >= zp->z_phys->zp_size) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   875
			error = ENOENT;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   876
			goto out;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   877
		}
2449
6adc99f24a96 6452420 zfs_get_data() of page data panics when blocksize is less than pagesize
maybee
parents: 2391
diff changeset
   878
		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   879
	} else { /* indirect write */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   880
		uint64_t boff; /* block starting offset */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   881
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
		/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   883
		 * Have to lock the whole block to ensure when it's
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   884
		 * written out and it's checksum is being calculated
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   885
		 * that no one can change the data. We need to re-check
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   886
		 * blocksize after we get the lock in case it's changed!
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
		 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   888
		for (;;) {
1941
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   889
			if (ISP2(zp->z_blksz)) {
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   890
				boff = P2ALIGN_TYPED(off, zp->z_blksz,
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   891
				    uint64_t);
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   892
			} else {
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   893
				boff = 0;
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   894
			}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   895
			dlen = zp->z_blksz;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   896
			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   897
			if (zp->z_blksz == dlen)
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   898
				break;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   899
			zfs_range_unlock(rl);
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   900
		}
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   901
		/* test for truncation needs to be done while range locked */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   902
		if (off >= zp->z_phys->zp_size) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   903
			error = ENOENT;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   904
			goto out;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   905
		}
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   906
		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   907
		zgd->zgd_rl = rl;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   908
		zgd->zgd_zilog = zfsvfs->z_log;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   909
		zgd->zgd_bp = &lr->lr_blkptr;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   910
		VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db));
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   911
		ASSERT(boff == db->db_offset);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   912
		lr->lr_blkoff = off - boff;
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   913
		error = dmu_sync(zio, db, &lr->lr_blkptr,
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   914
		    lr->lr_common.lrc_txg, zfs_get_done, zgd);
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
   915
		ASSERT((error && error != EINPROGRESS) ||
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
   916
		    lr->lr_length <= zp->z_blksz);
5688
c0b02c8fd2c0 6640580 spa_get_random() is insanely slow in userland
bonwick
parents: 5663
diff changeset
   917
		if (error == 0)
c0b02c8fd2c0 6640580 spa_get_random() is insanely slow in userland
bonwick
parents: 5663
diff changeset
   918
			zil_add_block(zfsvfs->z_log, &lr->lr_blkptr);
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   919
		/*
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   920
		 * If we get EINPROGRESS, then we need to wait for a
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   921
		 * write IO initiated by dmu_sync() to complete before
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   922
		 * we can release this dbuf.  We will finish everything
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   923
		 * up in the zfs_get_done() callback.
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   924
		 */
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   925
		if (error == EINPROGRESS)
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   926
			return (0);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   927
		dmu_buf_rele(db, zgd);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   928
		kmem_free(zgd, sizeof (zgd_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
	}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   930
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   931
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
	VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   938
zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   939
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   945
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   946
	ZFS_VERIFY_ZP(zp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   947
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   948
	if (flag & V_ACE_MASK)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   949
		error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   950
	else
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   951
		error = zfs_zaccess_rwx(zp, mode, flag, cr);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   952
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   955
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
 * Lookup an entry in a directory, or an extended attribute directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
 * If it exists, return a held vnode reference for it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
 *	IN:	dvp	- vnode of directory to search.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
 *		nm	- name of entry to lookup.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
 *		pnp	- full pathname to lookup [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
 *		flags	- LOOKUP_XATTR set if looking for an attribute.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   965
 *		rdir	- root directory vnode [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   966
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   967
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   968
 *		direntflags - directory lookup flags
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   969
 *		realpnp - returned pathname.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   970
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   971
 *	OUT:	vpp	- vnode of located entry, NULL if not found.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   977
 *	NA
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   982
    int flags, vnode_t *rdir, cred_t *cr,  caller_context_t *ct,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
   983
    int *direntflags, pathname_t *realpnp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
	znode_t *zdp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
	int	error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   989
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
   990
	ZFS_VERIFY_ZP(zdp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   991
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
	*vpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
	if (flags & LOOKUP_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
		/*
3234
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   996
		 * If the xattr property is off, refuse the lookup request.
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   997
		 */
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   998
		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   999
			ZFS_EXIT(zfsvfs);
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
  1000
			return (EINVAL);
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
  1001
		}
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
  1002
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
  1003
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1004
		 * We don't allow recursive attributes..
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1005
		 * Maybe someday we will.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1006
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
3280
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 3271
diff changeset
  1012
		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1016
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1017
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
		 * Do we have permission to get into attribute directory?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1021
		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1022
		    B_FALSE, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
			VN_RELE(*vpp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1024
			*vpp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
  1031
	if (dvp->v_type != VDIR) {
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
  1032
		ZFS_EXIT(zfsvfs);
1460
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
  1033
		return (ENOTDIR);
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
  1034
	}
1460
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
  1035
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
	 * Check accessibility of directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1040
	if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1043
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1044
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  1045
	if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1046
	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1047
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1048
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1049
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1050
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1051
	error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1052
	if (error == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1053
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
		 * Convert device special files
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
		if (IS_DEVVP(*vpp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1057
			vnode_t	*svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1058
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1059
			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1060
			VN_RELE(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1061
			if (svp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
				error = ENOSYS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
				*vpp = svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
 * Attempt to create a new entry in a directory.  If the entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
 * already exists, truncate the file if permissible, else return
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
 * an error.  Return the vp of the created or trunc'd file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1077
 *	IN:	dvp	- vnode of directory to put new file entry in.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1078
 *		name	- name of new file entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
 *		vap	- attributes of new file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
 *		excl	- flag indicating exclusive or non-exclusive mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
 *		mode	- mode to open file with.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
 *		flag	- large file flag [UNUSED].
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1084
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1085
 *		vsecp 	- ACL to be set
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
 *	OUT:	vpp	- vnode of created or trunc'd entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1090
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1092
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
 *	dvp - ctime|mtime updated if new entry created
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1094
 *	 vp - ctime|mtime always, atime if new
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1096
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1099
zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1100
    int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1101
    vsecattr_t *vsecp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1105
	zilog_t		*zilog;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1106
	objset_t	*os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1108
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1110
	zfs_acl_t	*aclp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1111
	zfs_fuid_info_t *fuidp = NULL;
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1112
	ksid_t		*ksid;
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1113
	uid_t		uid;
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1114
	gid_t		gid = crgetgid(cr);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1115
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1116
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1117
	 * If we have an ephemeral id, ACL, or XVATTR then
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1118
	 * make sure file system is at proper version
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1119
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1120
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1121
	ksid = crgetsid(cr, KSID_OWNER);
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1122
	if (ksid)
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1123
		uid = ksid_getid(ksid);
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1124
	else
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1125
		uid = crgetuid(cr);
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1126
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1127
	if (zfsvfs->z_use_fuids == B_FALSE &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1128
	    (vsecp || (vap->va_mask & AT_XVATTR) ||
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1129
	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1130
		return (EINVAL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1131
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1132
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1133
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1134
	os = zfsvfs->z_os;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1135
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  1137
	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1138
	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1139
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1140
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1141
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1142
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1143
	if (vap->va_mask & AT_XVATTR) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1144
		if ((error = secpolicy_xvattr((xvattr_t *)vap,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1145
		    crgetuid(cr), cr, vap->va_type)) != 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1146
			ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1147
			return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1148
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1149
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
	*vpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
		vap->va_mode &= ~VSVTX;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
	if (*name == '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
		 * Null component name refers to the directory itself.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
		VN_HOLD(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
		zp = dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
		dl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
		/* possible VN_HOLD(zp) */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1166
		int zflg = 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1167
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1168
		if (flag & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1169
			zflg |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1170
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1171
		error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1172
		    NULL, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1173
		if (error) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
			if (strcmp(name, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
				error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
			ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1177
			if (aclp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1178
				zfs_acl_free(aclp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1182
	if (vsecp && aclp == NULL) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1183
		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1184
		if (error) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1185
			ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1186
			if (dl)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1187
				zfs_dirent_unlock(dl);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1188
			return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1189
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1190
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
	if (zp == NULL) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1193
		uint64_t txtype;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1194
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
		 * Create a new file object and update the directory
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
		 * to reference it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
		 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1199
		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
		 * We only support the creation of regular files in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
		 * extended attribute directories.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
		    (vap->va_type != VREG)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
			error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1210
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1213
		tx = dmu_tx_create(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1214
		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1215
		if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) ||
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1216
		    IS_EPHEMERAL(gid)) {
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1217
			if (zfsvfs->z_fuid_obj == 0) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1218
				dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1219
				dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1220
				    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1221
				dmu_tx_hold_zap(tx, MASTER_NODE_OBJ,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1222
				    FALSE, NULL);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1223
			} else {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1224
				dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1225
				dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1226
				    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1227
			}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1228
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1229
		dmu_tx_hold_bonus(tx, dzp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1230
		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1231
		if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
			    0, SPA_MAXBLOCKSIZE);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1234
		}
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1235
		error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
			zfs_dirent_unlock(dl);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1238
			if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1239
				dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1240
				dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
				goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
			}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1243
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1244
			ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1245
			if (aclp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1246
				zfs_acl_free(aclp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
		}
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  1249
		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1250
		(void) zfs_link_create(dl, zp, tx, ZNEW);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1251
		txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1252
		if (flag & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1253
			txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1254
		zfs_log_create(zilog, tx, txtype, dzp, zp, name,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1255
		    vsecp, fuidp, vap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1256
		if (fuidp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1257
			zfs_fuid_info_free(fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1258
		dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
	} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1260
		int aflags = (flag & FAPPEND) ? V_APPEND : 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1261
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1262
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1263
		 * A directory entry already exists for this name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1264
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1265
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1266
		 * Can't truncate an existing file if in exclusive mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1267
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1268
		if (excl == EXCL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1269
			error = EEXIST;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1272
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1273
		 * Can't open a directory for writing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1274
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1275
		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1276
			error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1277
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1279
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1280
		 * Verify requested access to file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1281
		 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1282
		if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1283
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1285
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1286
		mutex_enter(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1287
		dzp->z_seq++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
		mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1289
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1290
		/*
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1291
		 * Truncate regular files if requested.
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1292
		 */
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1293
		if ((ZTOV(zp)->v_type == VREG) &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1294
		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1295
			/* we can't hold any locks when calling zfs_freesp() */
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1296
			zfs_dirent_unlock(dl);
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1297
			dl = NULL;
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1298
			error = zfs_freesp(zp, 0, 0, mode, TRUE);
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1299
			if (error == 0) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1300
				vnevent_create(ZTOV(zp), ct);
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1301
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1302
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1303
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1304
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1305
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1306
	if (dl)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1307
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1308
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1309
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1310
		if (zp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1311
			VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1312
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1313
		*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1314
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1315
		 * If vnode is for a device return a specfs vnode instead.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1316
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1317
		if (IS_DEVVP(*vpp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1318
			struct vnode *svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1319
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1320
			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1321
			VN_RELE(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1322
			if (svp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1323
				error = ENOSYS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1324
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1325
			*vpp = svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1328
	if (aclp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1329
		zfs_acl_free(aclp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1331
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1334
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1335
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1336
 * Remove an entry from a directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1337
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1338
 *	IN:	dvp	- vnode of directory to remove entry from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1339
 *		name	- name of entry to remove.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1340
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1341
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1342
 *		flags	- case flags
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1344
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1345
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1346
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1347
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1348
 *	dvp - ctime|mtime
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1349
 *	 vp - ctime (if nlink > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1351
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1353
zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1354
    int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1355
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1356
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1357
	znode_t		*xzp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1358
	vnode_t		*vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1359
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1360
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1361
	uint64_t	acl_obj, xattr_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1362
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
	dmu_tx_t	*tx;
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1364
	boolean_t	may_delete_now, delete_now = FALSE;
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1365
	boolean_t	unlinked, toobig = FALSE;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1366
	uint64_t	txtype;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1367
	pathname_t	*realnmp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1368
	pathname_t	realnm;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1369
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1370
	int		zflg = ZEXISTS;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1371
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1372
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1373
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1374
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1375
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1376
	if (flags & FIGNORECASE) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1377
		zflg |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1378
		pn_alloc(&realnm);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1379
		realnmp = &realnm;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1380
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1381
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1382
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1383
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1384
	 * Attempt to lock directory; fail if entry doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1385
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1386
	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1387
	    NULL, realnmp)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1388
		if (realnmp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1389
			pn_free(realnmp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1390
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1391
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1392
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1393
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1394
	vp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1395
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1396
	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1397
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1398
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1399
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1400
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1401
	 * Need to use rmdir for removing directories.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1402
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
	if (vp->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
		error = EPERM;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1405
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1406
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1407
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1408
	vnevent_remove(vp, dvp, name, ct);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1409
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1410
	if (realnmp)
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6180
diff changeset
  1411
		dnlc_remove(dvp, realnmp->pn_buf);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1412
	else
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1413
		dnlc_remove(dvp, name);
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1472
diff changeset
  1414
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1415
	mutex_enter(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1416
	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1417
	mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1418
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1419
	/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1420
	 * We may delete the znode now, or we may put it in the unlinked set;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1421
	 * it depends on whether we're the last link, and on whether there are
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1422
	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1423
	 * allow for either case.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1424
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1425
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1426
	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1427
	dmu_tx_hold_bonus(tx, zp->z_id);
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1428
	if (may_delete_now) {
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1429
		toobig =
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1430
		    zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1431
		/* if the file is too big, only hold_free a token amount */
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1432
		dmu_tx_hold_free(tx, zp->z_id, 0,
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1433
		    (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1434
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1435
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1436
	/* are there any extended attributes? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1437
	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1438
		/* XXX - do we need this if we are deleting? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1439
		dmu_tx_hold_bonus(tx, xattr_obj);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1440
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1442
	/* are there any additional acls */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1443
	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1444
	    may_delete_now)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1445
		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1446
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1447
	/* charge as an update -- would be nice not to charge at all */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1448
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1449
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1450
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1451
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1452
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1453
		VN_RELE(vp);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1454
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1455
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1456
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1457
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1458
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1459
		if (realnmp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1460
			pn_free(realnmp);
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1461
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1462
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1463
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1464
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1465
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1466
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1467
	 * Remove the directory entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1468
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1469
	error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1470
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1471
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1472
		dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1473
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1474
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1475
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1476
	if (unlinked) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
		mutex_enter(&vp->v_lock);
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1478
		delete_now = may_delete_now && !toobig &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1479
		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1480
		    zp->z_phys->zp_xattr == xattr_obj &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1481
		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1482
		mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1483
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1484
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1485
	if (delete_now) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1486
		if (zp->z_phys->zp_xattr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1487
			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1488
			ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1489
			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1490
			dmu_buf_will_dirty(xzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1491
			mutex_enter(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1492
			xzp->z_unlinked = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
			xzp->z_phys->zp_links = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
			mutex_exit(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1495
			zfs_unlinked_add(xzp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
		mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
		mutex_enter(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1500
		vp->v_count--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1501
		ASSERT3U(vp->v_count, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1502
		mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1503
		mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1504
		zfs_znode_delete(zp, tx);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1505
	} else if (unlinked) {
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1506
		zfs_unlinked_add(zp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1507
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1508
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1509
	txtype = TX_REMOVE;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1510
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1511
		txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1512
	zfs_log_remove(zilog, tx, txtype, dzp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1513
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1514
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1515
out:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1516
	if (realnmp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1517
		pn_free(realnmp);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1518
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1519
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1520
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1521
	if (!delete_now) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1522
		VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1523
	} else if (xzp) {
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  1524
		/* this rele is delayed to prevent nesting transactions */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
		VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1529
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1530
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1532
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1533
 * Create a new directory and insert it into dvp using the name
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1534
 * provided.  Return a pointer to the inserted directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1535
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1536
 *	IN:	dvp	- vnode of directory to add subdir to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1537
 *		dirname	- name of new directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1538
 *		vap	- attributes of new directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1539
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1540
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1541
 *		vsecp	- ACL to be set
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1542
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1543
 *	OUT:	vpp	- vnode of created directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1545
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1546
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1547
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
 *	 vp - ctime|mtime|atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1551
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1552
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1553
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1554
zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1555
    caller_context_t *ct, int flags, vsecattr_t *vsecp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1556
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1559
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
	zfs_dirlock_t	*dl;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1561
	uint64_t	txtype;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1563
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1564
	zfs_acl_t	*aclp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1565
	zfs_fuid_info_t	*fuidp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1566
	int		zf = ZNEW;
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1567
	ksid_t		*ksid;
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1568
	uid_t		uid;
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1569
	gid_t		gid = crgetgid(cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1570
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
	ASSERT(vap->va_type == VDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1572
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1573
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1574
	 * If we have an ephemeral id, ACL, or XVATTR then
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1575
	 * make sure file system is at proper version
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1576
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1577
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1578
	ksid = crgetsid(cr, KSID_OWNER);
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1579
	if (ksid)
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1580
		uid = ksid_getid(ksid);
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1581
	else
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1582
		uid = crgetuid(cr);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1583
	if (zfsvfs->z_use_fuids == B_FALSE &&
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1584
	    (vsecp || (vap->va_mask & AT_XVATTR) ||
7876
c8456365db46 6760970 zfs_mkdir falsely returns EINVAL
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7847
diff changeset
  1585
	    IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1586
		return (EINVAL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1587
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1588
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1589
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1590
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1591
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1592
	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1593
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1594
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1595
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1596
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  1597
	if (zfsvfs->z_utf8 && u8_validate(dirname,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1598
	    strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1599
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1600
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1601
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1602
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1603
		zf |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1604
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1605
	if (vap->va_mask & AT_XVATTR)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1606
		if ((error = secpolicy_xvattr((xvattr_t *)vap,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1607
		    crgetuid(cr), cr, vap->va_type)) != 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1608
			ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1609
			return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1610
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1611
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1612
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1613
	 * First make sure the new directory doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1614
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1615
top:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1616
	*vpp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1617
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1618
	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1619
	    NULL, NULL)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1623
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1624
	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1625
		zfs_dirent_unlock(dl);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1626
		ZFS_EXIT(zfsvfs);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1627
		return (error);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1628
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1629
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1630
	if (vsecp && aclp == NULL) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1631
		error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1632
		if (error) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1633
			zfs_dirent_unlock(dl);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1634
			ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1635
			return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1636
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1637
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1638
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
	 * Add a new entry to the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1642
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1643
	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
7847
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1644
	if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) ||
3b5331e410c6 6754013 ZFS should always use KSID_OWNER when it's set
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 7844
diff changeset
  1645
	    IS_EPHEMERAL(gid)) {
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1646
		if (zfsvfs->z_fuid_obj == 0) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1647
			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1648
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1649
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1650
			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1651
		} else {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1652
			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1653
			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1654
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  1655
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1656
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1657
	if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
		    0, SPA_MAXBLOCKSIZE);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1660
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1662
		zfs_dirent_unlock(dl);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1663
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1664
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1665
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1666
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1667
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1668
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1669
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1670
		if (aclp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1671
			zfs_acl_free(aclp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1672
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1673
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1674
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1675
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1676
	 * Create new node.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1677
	 */
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  1678
	zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1679
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1680
	if (aclp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1681
		zfs_acl_free(aclp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
	 * Now put new name in parent dir.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
	(void) zfs_link_create(dl, zp, tx, ZNEW);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1687
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1688
	*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1690
	txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1691
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1692
		txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1693
	zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1694
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1695
	if (fuidp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1696
		zfs_fuid_info_free(fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1699
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1700
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1701
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1702
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1704
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1705
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1706
 * Remove a directory subdir entry.  If the current working
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1707
 * directory is the same as the subdir to be removed, the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1708
 * remove will fail.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1709
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1710
 *	IN:	dvp	- vnode of directory to remove from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1711
 *		name	- name of directory to be removed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1712
 *		cwd	- vnode of current working directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1713
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1714
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1715
 *		flags	- case flags
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1716
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1719
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1721
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1723
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1724
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1725
zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1726
    caller_context_t *ct, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1727
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1728
	znode_t		*dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1729
	znode_t		*zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1730
	vnode_t		*vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1731
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1732
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1733
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1734
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1735
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1736
	int		zflg = ZEXISTS;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1737
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1738
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1739
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1740
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1741
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1742
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1743
		zflg |= ZCILOOK;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
	zp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
	 * Attempt to lock directory; fail if entry doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1750
	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1751
	    NULL, NULL)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1752
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1756
	vp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1757
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1758
	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1759
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1760
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
	if (vp->v_type != VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1763
		error = ENOTDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1764
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1765
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1766
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1767
	if (vp == cwd) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
		error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1771
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1772
	vnevent_rmdir(vp, dvp, name, ct);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
	/*
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1775
	 * Grab a lock on the directory to make sure that noone is
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1776
	 * trying to add (or lookup) entries while we are removing it.
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1777
	 */
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1778
	rw_enter(&zp->z_name_lock, RW_WRITER);
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1779
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1780
	/*
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1781
	 * Grab a lock on the parent pointer to make sure we play well
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1782
	 * with the treewalk and directory rename code.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
	rw_enter(&zp->z_parent_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1785
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1786
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1787
	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
	dmu_tx_hold_bonus(tx, zp->z_id);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1789
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1790
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1791
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
		rw_exit(&zp->z_parent_lock);
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1793
		rw_exit(&zp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1794
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
		VN_RELE(vp);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  1796
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1797
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1798
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1801
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1802
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1803
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1804
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1805
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1806
	error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1807
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1808
	if (error == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1809
		uint64_t txtype = TX_RMDIR;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1810
		if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1811
			txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1812
		zfs_log_remove(zilog, tx, txtype, dzp, name);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1813
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1814
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1815
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1816
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1817
	rw_exit(&zp->z_parent_lock);
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1818
	rw_exit(&zp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1820
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1821
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1822
	VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1823
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1824
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1825
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1826
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1827
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1828
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1829
 * Read as many directory entries as will fit into the provided
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1830
 * buffer from the given directory cursor position (specified in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1831
 * the uio structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1832
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1833
 *	IN:	vp	- vnode of directory to read.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1834
 *		uio	- structure supplying read location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1835
 *			  and return buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1837
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1838
 *		flags	- case flags
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1839
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
 *	OUT:	uio	- updated offset and range, buffer filled.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
 *		eofp	- set to true if end-of-file detected.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1842
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1843
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1844
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1845
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1846
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1848
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
 * Note that the low 4 bits of the cookie returned by zap is always zero.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1850
 * This allows us to use the low range for "special" directory entries:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1851
 * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1852
 * we use the offset 2 for the '.zfs' directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1854
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1855
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1856
zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1857
    caller_context_t *ct, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1858
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1860
	iovec_t		*iovp;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1861
	edirent_t	*eodp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1862
	dirent64_t	*odp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1863
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1864
	objset_t	*os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1865
	caddr_t		outbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1866
	size_t		bufsize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1867
	zap_cursor_t	zc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1868
	zap_attribute_t	zap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1869
	uint_t		bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1870
	uint64_t	offset; /* must be unsigned; checks for < 1 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1871
	int		local_eof;
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1872
	int		outcount;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1873
	int		error;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1874
	uint8_t		prefetch;
5663
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1875
	boolean_t	check_sysattrs;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1877
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  1878
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1879
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1881
	 * If we are not given an eof variable,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1882
	 * use a local one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1883
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1884
	if (eofp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1885
		eofp = &local_eof;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1886
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1887
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1888
	 * Check for valid iov_len.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1889
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1890
	if (uio->uio_iov->iov_len <= 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1891
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1892
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1893
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1894
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1895
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1896
	 * Quit if directory has been removed (posix)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1897
	 */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1898
	if ((*eofp = zp->z_unlinked) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1899
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1900
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1901
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1902
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1903
	error = 0;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1904
	os = zfsvfs->z_os;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1905
	offset = uio->uio_loffset;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1906
	prefetch = zp->z_zn_prefetch;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1907
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1909
	 * Initialize the iterator cursor.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1910
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1911
	if (offset <= 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1913
		 * Start iteration from the beginning of the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
		 */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1915
		zap_cursor_init(&zc, os, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
		 * The offset is a serialized cursor.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
		 */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1920
		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1921
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1922
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
	 * Get space to change directory entries into fs independent format.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
	iovp = uio->uio_iov;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
	bytes_wanted = iovp->iov_len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1928
	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1929
		bufsize = bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
		outbuf = kmem_alloc(bufsize, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1931
		odp = (struct dirent64 *)outbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1932
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
		bufsize = bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
		odp = (struct dirent64 *)iovp->iov_base;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1936
	eodp = (struct edirent *)odp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
	/*
7757
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  1939
	 * If this VFS supports the system attribute view interface; and
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  1940
	 * we're looking at an extended attribute directory; and we care
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  1941
	 * about normalization conflicts on this vfs; then we must check
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  1942
	 * for normalization conflicts with the sysattr name space.
5663
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1943
	 */
7757
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  1944
	check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5663
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1945
	    (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1946
	    (flags & V_RDDIR_ENTFLAGS);
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1947
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1948
	/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
	 * Transform to file-system independent format
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1950
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1951
	outcount = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1952
	while (outcount < bytes_wanted) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1953
		ino64_t objnum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1954
		ushort_t reclen;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1955
		off64_t *next;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1956
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
		 * Special case `.', `..', and `.zfs'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
		if (offset == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1961
			(void) strcpy(zap.za_name, ".");
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1962
			zap.za_normalization_conflict = 0;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1963
			objnum = zp->z_id;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1964
		} else if (offset == 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1965
			(void) strcpy(zap.za_name, "..");
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1966
			zap.za_normalization_conflict = 0;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1967
			objnum = zp->z_phys->zp_parent;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
		} else if (offset == 2 && zfs_show_ctldir(zp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1969
			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  1970
			zap.za_normalization_conflict = 0;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1971
			objnum = ZFSCTL_INO_ROOT;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1974
			 * Grab next entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1976
			if (error = zap_cursor_retrieve(&zc, &zap)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
				if ((*eofp = (error == ENOENT)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1978
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1979
				else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1980
					goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1981
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1982
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1983
			if (zap.za_integer_length != 8 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1984
			    zap.za_num_integers != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1985
				cmn_err(CE_WARN, "zap_readdir: bad directory "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1986
				    "entry, obj = %lld, offset = %lld\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1987
				    (u_longlong_t)zp->z_id,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1988
				    (u_longlong_t)offset);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1989
				error = ENXIO;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1990
				goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1991
			}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1992
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1993
			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1994
			/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1995
			 * MacOS X can extract the object type here such as:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1996
			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1997
			 */
5663
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1998
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  1999
			if (check_sysattrs && !zap.za_normalization_conflict) {
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  2000
				zap.za_normalization_conflict =
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  2001
				    xattr_sysattr_casechk(zap.za_name);
029cc4273b57 6627223 gfs needs to support extended dirent flags
ck153898
parents: 5642
diff changeset
  2002
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2003
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2004
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2005
		if (flags & V_RDDIR_ENTFLAGS)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2006
			reclen = EDIRENT_RECLEN(strlen(zap.za_name));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2007
		else
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2008
			reclen = DIRENT64_RECLEN(strlen(zap.za_name));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2010
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2011
		 * Will this entry fit in the buffer?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
		 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  2013
		if (outcount + reclen > bufsize) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2014
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
			 * Did we manage to fit anything in the buffer?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
			if (!outcount) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
				error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
				goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2021
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2022
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2023
		if (flags & V_RDDIR_ENTFLAGS) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2024
			/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2025
			 * Add extended flag entry:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2026
			 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2027
			eodp->ed_ino = objnum;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2028
			eodp->ed_reclen = reclen;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2029
			/* NOTE: ed_off is the offset for the *next* entry */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2030
			next = &(eodp->ed_off);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2031
			eodp->ed_eflags = zap.za_normalization_conflict ?
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2032
			    ED_CASE_CONFLICT : 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2033
			(void) strncpy(eodp->ed_name, zap.za_name,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2034
			    EDIRENT_NAMELEN(reclen));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2035
			eodp = (edirent_t *)((intptr_t)eodp + reclen);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2036
		} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2037
			/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2038
			 * Add normal entry:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2039
			 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2040
			odp->d_ino = objnum;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2041
			odp->d_reclen = reclen;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2042
			/* NOTE: d_off is the offset for the *next* entry */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2043
			next = &(odp->d_off);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2044
			(void) strncpy(odp->d_name, zap.za_name,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2045
			    DIRENT64_NAMELEN(reclen));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2046
			odp = (dirent64_t *)((intptr_t)odp + reclen);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2047
		}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  2048
		outcount += reclen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2049
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2050
		ASSERT(outcount <= bufsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2052
		/* Prefetch znode */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  2053
		if (prefetch)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  2054
			dmu_prefetch(os, objnum, 0, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2055
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2056
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2057
		 * Move to the next entry, fill in the previous offset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2058
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2059
		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2060
			zap_cursor_advance(&zc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2061
			offset = zap_cursor_serialize(&zc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2062
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2063
			offset += 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2064
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2065
		*next = offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2066
	}
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  2067
	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2068
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2069
	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2070
		iovp->iov_base += outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2071
		iovp->iov_len -= outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2072
		uio->uio_resid -= outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2073
	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2074
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2075
		 * Reset the pointer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2076
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2077
		offset = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2079
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2080
update:
885
d925b21dba78 6347493 tar of 25K empty directory entries in ZFS takes 30+ seconds ...
ahrens
parents: 869
diff changeset
  2081
	zap_cursor_fini(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2082
	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2083
		kmem_free(outbuf, bufsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2084
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2085
	if (error == ENOENT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2086
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2088
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
	uio->uio_loffset = offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2092
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2093
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
4720
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  2095
ulong_t zfs_fsync_sync_cnt = 4;
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  2096
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2098
zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2099
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2100
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2101
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2103
	/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2104
	 * Regardless of whether this is required for standards conformance,
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2105
	 * this is the logical behavior when fsync() is called on a file with
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2106
	 * dirty pages.  We use B_ASYNC since the ZIL transactions are already
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2107
	 * going to be pushed out as part of the zil_commit().
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2108
	 */
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2109
	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2110
	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2111
		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  2112
4720
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  2113
	(void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  2114
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2115
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2116
	ZFS_VERIFY_ZP(zp);
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  2117
	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2118
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2119
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2120
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2121
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2122
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2123
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2124
 * Get the requested file attributes and place them in the provided
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2125
 * vattr structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2126
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2127
 *	IN:	vp	- vnode of file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2128
 *		vap	- va_mask identifies requested attributes.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2129
 *			  If AT_XVATTR set, then optional attrs are requested
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2130
 *		flags	- ATTR_NOACLCHECK (CIFS server context)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2131
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2132
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
 *	OUT:	vap	- attribute values.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2135
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2136
 *	RETURN:	0 (always succeeds)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2137
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2138
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2139
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2140
zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2141
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2142
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2143
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2144
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2145
	znode_phys_t *pzp;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2146
	int	error = 0;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2147
	uint64_t links;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2148
	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2149
	xoptattr_t *xoap = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2150
	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2151
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2152
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2153
	ZFS_VERIFY_ZP(zp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2154
	pzp = zp->z_phys;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2155
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2156
	mutex_enter(&zp->z_lock);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2157
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2158
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2159
	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2160
	 * Also, if we are the owner don't bother, since owner should
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2161
	 * always be allowed to read basic attributes of file.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2162
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2163
	if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2164
	    (pzp->zp_uid != crgetuid(cr))) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2165
		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2166
		    skipaclchk, cr)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2167
			mutex_exit(&zp->z_lock);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2168
			ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2169
			return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2170
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2171
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2172
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2173
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2174
	 * Return all attributes.  It's cheaper to provide the answer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
	 * than to determine whether we were asked the question.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2176
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2177
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2178
	vap->va_type = vp->v_type;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2179
	vap->va_mode = pzp->zp_mode & MODEMASK;
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2180
	zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2181
	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2182
	vap->va_nodeid = zp->z_id;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2183
	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2184
		links = pzp->zp_links + 1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2185
	else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2186
		links = pzp->zp_links;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  2187
	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2188
	vap->va_size = pzp->zp_size;
1816
8c14b56c8515 6408482 64-bit system can't read some 32-bit dev_ts created on zfs
marks
parents: 1773
diff changeset
  2189
	vap->va_rdev = vp->v_rdev;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2190
	vap->va_seq = zp->z_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2191
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2192
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2193
	 * Add in any requested optional attributes and the create time.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2194
	 * Also set the corresponding bits in the returned attribute bitmap.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2195
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2196
	if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2197
		if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2198
			xoap->xoa_archive =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2199
			    ((pzp->zp_flags & ZFS_ARCHIVE) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2200
			XVA_SET_RTN(xvap, XAT_ARCHIVE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2201
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2202
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2203
		if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2204
			xoap->xoa_readonly =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2205
			    ((pzp->zp_flags & ZFS_READONLY) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2206
			XVA_SET_RTN(xvap, XAT_READONLY);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2207
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2208
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2209
		if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2210
			xoap->xoa_system =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2211
			    ((pzp->zp_flags & ZFS_SYSTEM) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2212
			XVA_SET_RTN(xvap, XAT_SYSTEM);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2213
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2214
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2215
		if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2216
			xoap->xoa_hidden =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2217
			    ((pzp->zp_flags & ZFS_HIDDEN) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2218
			XVA_SET_RTN(xvap, XAT_HIDDEN);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2219
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2220
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2221
		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2222
			xoap->xoa_nounlink =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2223
			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2224
			XVA_SET_RTN(xvap, XAT_NOUNLINK);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2225
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2226
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2227
		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2228
			xoap->xoa_immutable =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2229
			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2230
			XVA_SET_RTN(xvap, XAT_IMMUTABLE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2231
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2232
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2233
		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2234
			xoap->xoa_appendonly =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2235
			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2236
			XVA_SET_RTN(xvap, XAT_APPENDONLY);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2237
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2238
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2239
		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2240
			xoap->xoa_nodump =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2241
			    ((pzp->zp_flags & ZFS_NODUMP) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2242
			XVA_SET_RTN(xvap, XAT_NODUMP);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2243
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2244
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2245
		if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2246
			xoap->xoa_opaque =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2247
			    ((pzp->zp_flags & ZFS_OPAQUE) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2248
			XVA_SET_RTN(xvap, XAT_OPAQUE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2249
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2250
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2251
		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2252
			xoap->xoa_av_quarantined =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2253
			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2254
			XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2255
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2256
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2257
		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2258
			xoap->xoa_av_modified =
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2259
			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2260
			XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2261
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2262
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2263
		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2264
		    vp->v_type == VREG &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2265
		    (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2266
			size_t len;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2267
			dmu_object_info_t doi;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2268
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2269
			/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2270
			 * Only VREG files have anti-virus scanstamps, so we
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2271
			 * won't conflict with symlinks in the bonus buffer.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2272
			 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2273
			dmu_object_info_from_db(zp->z_dbuf, &doi);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2274
			len = sizeof (xoap->xoa_av_scanstamp) +
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2275
			    sizeof (znode_phys_t);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2276
			if (len <= doi.doi_bonus_size) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2277
				/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2278
				 * pzp points to the start of the
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2279
				 * znode_phys_t. pzp + 1 points to the
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2280
				 * first byte after the znode_phys_t.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2281
				 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2282
				(void) memcpy(xoap->xoa_av_scanstamp,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2283
				    pzp + 1,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2284
				    sizeof (xoap->xoa_av_scanstamp));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2285
				XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2286
			}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2287
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2288
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2289
		if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2290
			ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2291
			XVA_SET_RTN(xvap, XAT_CREATETIME);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2292
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2293
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2294
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2295
	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2296
	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2297
	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2298
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2299
	mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2300
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2301
	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2302
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2303
	if (zp->z_blksz == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2304
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2305
		 * Block size hasn't been set; suggest maximal I/O transfers.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2306
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2307
		vap->va_blksize = zfsvfs->z_max_blksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2308
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2309
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2310
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2312
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2313
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2314
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
 * Set the file attributes to the values contained in the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2316
 * vattr structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
 *	IN:	vp	- vnode of file to be modified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2319
 *		vap	- new attribute values.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2320
 *			  If AT_XVATTR set, then optional attrs are being set
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2321
 *		flags	- ATTR_UTIME set if non-default time values provided.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2322
 *			- ATTR_NOACLCHECK (CIFS context only).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2323
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2324
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2325
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2326
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2327
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2328
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2329
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2330
 *	vp - ctime updated, mtime updated if size changed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2331
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2332
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2333
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2334
zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2335
	caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2336
{
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2337
	znode_t		*zp = VTOZ(vp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2338
	znode_phys_t	*pzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2339
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2340
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2341
	dmu_tx_t	*tx;
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2342
	vattr_t		oldva;
8190
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2343
	xvattr_t	tmpxvattr;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2344
	uint_t		mask = vap->va_mask;
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2345
	uint_t		saved_mask;
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2346
	int		trim_mask = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2347
	uint64_t	new_mode;
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2348
	znode_t		*attrzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
	int		need_policy = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
	int		err;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2351
	zfs_fuid_info_t *fuidp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2352
	xvattr_t *xvap = (xvattr_t *)vap;	/* vap may be an xvattr_t * */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2353
	xoptattr_t	*xoap;
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2354
	zfs_acl_t	*aclp = NULL;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2355
	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2356
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2357
	if (mask == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2359
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
	if (mask & AT_NOSET)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2362
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2363
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2364
	ZFS_VERIFY_ZP(zp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2365
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2366
	pzp = zp->z_phys;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2367
	zilog = zfsvfs->z_log;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2368
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2369
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2370
	 * Make sure that if we have ephemeral uid/gid or xvattr specified
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2371
	 * that file system is at proper version level
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2372
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2373
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2374
	if (zfsvfs->z_use_fuids == B_FALSE &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2375
	    (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2376
	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2377
	    (mask & AT_XVATTR))) {
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2378
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2379
		return (EINVAL);
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2380
	}
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2381
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2382
	if (mask & AT_SIZE && vp->v_type == VDIR) {
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2383
		ZFS_EXIT(zfsvfs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
		return (EISDIR);
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2385
	}
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2387
	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2388
		ZFS_EXIT(zfsvfs);
1308
997e6a49c409 6362908 nfsv4-test: RENAME does not check PERM with NFSv4/ZFS
marks
parents: 1298
diff changeset
  2389
		return (EINVAL);
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2390
	}
1308
997e6a49c409 6362908 nfsv4-test: RENAME does not check PERM with NFSv4/ZFS
marks
parents: 1298
diff changeset
  2391
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2392
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2393
	 * If this is an xvattr_t, then get a pointer to the structure of
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2394
	 * optional attributes.  If this is NULL, then we have a vattr_t.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2395
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2396
	xoap = xva_getxoptattr(xvap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2397
8190
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2398
	xva_init(&tmpxvattr);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2399
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2400
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2401
	 * Immutable files can only alter immutable bit and atime
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2402
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2403
	if ((pzp->zp_flags & ZFS_IMMUTABLE) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2404
	    ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2405
	    ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2406
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2407
		return (EPERM);
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2408
	}
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2409
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2410
	if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) {
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2411
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2412
		return (EPERM);
5386
fa920350a914 6623402 command hang during 'zfs unmount -a' after run CIFS test cases
timh
parents: 5367
diff changeset
  2413
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2414
6064
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2415
	/*
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2416
	 * Verify timestamps doesn't overflow 32 bits.
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2417
	 * ZFS can handle large timestamps, but 32bit syscalls can't
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2418
	 * handle times greater than 2039.  This check should be removed
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2419
	 * once large timestamps are fully supported.
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2420
	 */
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2421
	if (mask & (AT_ATIME | AT_MTIME)) {
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2422
		if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2423
		    ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2424
			ZFS_EXIT(zfsvfs);
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2425
			return (EOVERFLOW);
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2426
		}
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2427
	}
45d29804ab93 6629604 ZFS: lstat64() on ZFS file returns EOVERFLOW
marks
parents: 6036
diff changeset
  2428
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2429
top:
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2430
	attrzp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2431
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2432
	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2434
		return (EROFS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2435
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2436
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2437
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2438
	 * First validate permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2439
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2440
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2441
	if (mask & AT_SIZE) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2442
		err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2443
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2444
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2445
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2446
		}
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2447
		/*
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2448
		 * XXX - Note, we are not providing any open
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2449
		 * mode flags here (like FNDELAY), so we may
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2450
		 * block if there are locks present... this
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2451
		 * should be addressed in openat().
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2452
		 */
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  2453
		/* XXX - would it be OK to generate a log record here? */
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  2454
		err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2455
		if (err) {
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2456
			ZFS_EXIT(zfsvfs);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2457
			return (err);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2458
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2460
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2461
	if (mask & (AT_ATIME|AT_MTIME) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2462
	    ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2463
	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2464
	    XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2465
	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2466
	    XVA_ISSET_REQ(xvap, XAT_SYSTEM))))
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2467
		need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2468
		    skipaclchk, cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2470
	if (mask & (AT_UID|AT_GID)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2471
		int	idmask = (mask & (AT_UID|AT_GID));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2472
		int	take_owner;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2473
		int	take_group;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2474
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2475
		/*
913
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2476
		 * NOTE: even if a new mode is being set,
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2477
		 * we may clear S_ISUID/S_ISGID bits.
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2478
		 */
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2479
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2480
		if (!(mask & AT_MODE))
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2481
			vap->va_mode = pzp->zp_mode;
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2482
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2483
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2484
		 * Take ownership or chgrp to group we are a member of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2485
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2486
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2487
		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2488
		take_group = (mask & AT_GID) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2489
		    zfs_groupmember(zfsvfs, vap->va_gid, cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2490
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2491
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2492
		 * If both AT_UID and AT_GID are set then take_owner and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2493
		 * take_group must both be set in order to allow taking
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2494
		 * ownership.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2495
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2496
		 * Otherwise, send the check through secpolicy_vnode_setattr()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2497
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2498
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2499
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2500
		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
		    ((idmask == AT_UID) && take_owner) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2502
		    ((idmask == AT_GID) && take_group)) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2503
			if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2504
			    skipaclchk, cr) == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2505
				/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2506
				 * Remove setuid/setgid for non-privileged users
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2507
				 */
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2508
				secpolicy_setid_clear(vap, cr);
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2509
				trim_mask = (mask & (AT_UID|AT_GID));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2510
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2511
				need_policy =  TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2512
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2513
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2514
			need_policy =  TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2515
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2516
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2517
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2518
	mutex_enter(&zp->z_lock);
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2519
	oldva.va_mode = pzp->zp_mode;
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2520
	zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2521
	if (mask & AT_XVATTR) {
8190
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2522
		/*
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2523
		 * Update xvattr mask to include only those attributes
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2524
		 * that are actually changing.
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2525
		 *
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2526
		 * the bits will be restored prior to actually setting
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2527
		 * the attributes so the caller thinks they were set.
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2528
		 */
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2529
		if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2530
			if (xoap->xoa_appendonly !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2531
			    ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2532
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2533
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2534
				XVA_CLR_REQ(xvap, XAT_APPENDONLY);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2535
				XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2536
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2537
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2538
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2539
		if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2540
			if (xoap->xoa_nounlink !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2541
			    ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2542
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2543
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2544
				XVA_CLR_REQ(xvap, XAT_NOUNLINK);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2545
				XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2546
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2547
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2548
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2549
		if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2550
			if (xoap->xoa_immutable !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2551
			    ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2552
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2553
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2554
				XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2555
				XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2556
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2557
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2558
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2559
		if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2560
			if (xoap->xoa_nodump !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2561
			    ((pzp->zp_flags & ZFS_NODUMP) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2562
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2563
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2564
				XVA_CLR_REQ(xvap, XAT_NODUMP);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2565
				XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2566
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2567
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2568
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2569
		if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2570
			if (xoap->xoa_av_modified !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2571
			    ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2572
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2573
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2574
				XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2575
				XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2576
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2577
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2578
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2579
		if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2580
			if ((vp->v_type != VREG &&
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2581
			    xoap->xoa_av_quarantined) ||
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2582
			    xoap->xoa_av_quarantined !=
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2583
			    ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2584
				need_policy = TRUE;
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2585
			} else {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2586
				XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2587
				XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2588
			}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2589
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2590
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2591
		if (need_policy == FALSE &&
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2592
		    (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2593
		    XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2594
			need_policy = TRUE;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2595
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2596
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2597
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2598
	mutex_exit(&zp->z_lock);
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2599
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2600
	if (mask & AT_MODE) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2601
		if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2602
			err = secpolicy_setid_setsticky_clear(vp, vap,
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2603
			    &oldva, cr);
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2604
			if (err) {
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2605
				ZFS_EXIT(zfsvfs);
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2606
				return (err);
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2607
			}
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2608
			trim_mask |= AT_MODE;
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2609
		} else {
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2610
			need_policy = TRUE;
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2611
		}
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2612
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2613
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2614
	if (need_policy) {
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2615
		/*
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2616
		 * If trim_mask is set then take ownership
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2617
		 * has been granted or write_acl is present and user
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2618
		 * has the ability to modify mode.  In that case remove
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2619
		 * UID|GID and or MODE from mask so that
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2620
		 * secpolicy_vnode_setattr() doesn't revoke it.
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2621
		 */
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2622
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2623
		if (trim_mask) {
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2624
			saved_mask = vap->va_mask;
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2625
			vap->va_mask &= ~trim_mask;
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2626
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2627
		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2628
		    (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2629
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2630
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
		}
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2633
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2634
		if (trim_mask)
2796
95109e1ced2b 6445680 Having write_acl allowed in an ACL doesn't give the ability to set the mode via chmod
marks
parents: 2752
diff changeset
  2635
			vap->va_mask |= saved_mask;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2636
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2638
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2639
	 * secpolicy_vnode_setattr, or take ownership may have
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2640
	 * changed va_mask
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2641
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2642
	mask = vap->va_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2644
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2645
	dmu_tx_hold_bonus(tx, zp->z_id);
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2646
	if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2647
	    ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2648
		if (zfsvfs->z_fuid_obj == 0) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2649
			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2650
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2651
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2652
			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2653
		} else {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2654
			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2655
			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2656
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2657
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2658
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2659
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2660
	if (mask & AT_MODE) {
1576
0364d1928a7f 6380036 zfs does not clear S_ISUID and S_ISGID bits on successful writes
marks
parents: 1544
diff changeset
  2661
		uint64_t pmode = pzp->zp_mode;
0364d1928a7f 6380036 zfs does not clear S_ISUID and S_ISGID bits on successful writes
marks
parents: 1544
diff changeset
  2662
0364d1928a7f 6380036 zfs does not clear S_ISUID and S_ISGID bits on successful writes
marks
parents: 1544
diff changeset
  2663
		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2664
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2665
		if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2666
			dmu_tx_abort(tx);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2667
			ZFS_EXIT(zfsvfs);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2668
			return (err);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2669
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2670
		if (pzp->zp_acl.z_acl_extern_obj) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2671
			/* Are we upgrading ACL from old V0 format to new V1 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2672
			if (zfsvfs->z_version <= ZPL_VERSION_FUID &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2673
			    pzp->zp_acl.z_acl_version ==
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2674
			    ZFS_ACL_VERSION_INITIAL) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2675
				dmu_tx_hold_free(tx,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2676
				    pzp->zp_acl.z_acl_extern_obj, 0,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2677
				    DMU_OBJECT_END);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2678
				dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2679
				    0, aclp->z_acl_bytes);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2680
			} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2681
				dmu_tx_hold_write(tx,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2682
				    pzp->zp_acl.z_acl_extern_obj, 0,
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2683
				    aclp->z_acl_bytes);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2684
			}
6180
629c55a31613 6674446 missing dmu_tx_hold_write() in zfs_setattr()
marks
parents: 6064
diff changeset
  2685
		} else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) {
629c55a31613 6674446 missing dmu_tx_hold_write() in zfs_setattr()
marks
parents: 6064
diff changeset
  2686
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
629c55a31613 6674446 missing dmu_tx_hold_write() in zfs_setattr()
marks
parents: 6064
diff changeset
  2687
			    0, aclp->z_acl_bytes);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2688
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2689
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2690
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2691
	if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2692
		err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2693
		if (err) {
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2694
			dmu_tx_abort(tx);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2695
			ZFS_EXIT(zfsvfs);
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2696
			if (aclp)
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2697
				zfs_acl_free(aclp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2698
			return (err);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2699
		}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2700
		dmu_tx_hold_bonus(tx, attrzp->z_id);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2701
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2702
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  2703
	err = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2704
	if (err) {
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2705
		if (attrzp)
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2706
			VN_RELE(ZTOV(attrzp));
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2707
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2708
		if (aclp) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2709
			zfs_acl_free(aclp);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2710
			aclp = NULL;
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2711
		}
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2712
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  2713
		if (err == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2714
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2715
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2716
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2717
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2718
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2719
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2720
		return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2721
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2722
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2723
	dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2724
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2725
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2726
	 * Set each attribute requested.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2727
	 * We group settings according to the locks they need to acquire.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2729
	 * Note: you cannot set ctime directly, although it will be
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2730
	 * updated as a side-effect of calling this function.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
	mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2734
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
	if (mask & AT_MODE) {
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2736
		mutex_enter(&zp->z_acl_lock);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2737
		zp->z_phys->zp_mode = new_mode;
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2738
		err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2739
		ASSERT3U(err, ==, 0);
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2740
		mutex_exit(&zp->z_acl_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2741
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2742
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2743
	if (attrzp)
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2744
		mutex_enter(&attrzp->z_lock);
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2745
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2746
	if (mask & AT_UID) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2747
		pzp->zp_uid = zfs_fuid_create(zfsvfs,
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2748
		    vap->va_uid, cr, ZFS_OWNER, tx, &fuidp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2749
		if (attrzp) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2750
			attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs,
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2751
			    vap->va_uid,  cr, ZFS_OWNER, tx, &fuidp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2752
		}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2753
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2754
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2755
	if (mask & AT_GID) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2756
		pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid,
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2757
		    cr, ZFS_GROUP, tx, &fuidp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2758
		if (attrzp)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2759
			attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs,
5771
7ba3a2c57d6a 6552639 Each zone should have it's own idmapd
jp151216
parents: 5688
diff changeset
  2760
			    vap->va_gid, cr, ZFS_GROUP, tx, &fuidp);
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2761
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2762
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2763
	if (aclp)
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2764
		zfs_acl_free(aclp);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  2765
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2766
	if (attrzp)
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2767
		mutex_exit(&attrzp->z_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2768
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2769
	if (mask & AT_ATIME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2770
		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2771
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2772
	if (mask & AT_MTIME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2773
		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2774
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  2775
	/* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2776
	if (mask & AT_SIZE)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2777
		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2778
	else if (mask != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2779
		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2780
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2781
	 * Do this after setting timestamps to prevent timestamp
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2782
	 * update from toggling bit
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2783
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2784
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2785
	if (xoap && (mask & AT_XVATTR)) {
8190
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2786
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2787
		/*
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2788
		 * restore trimmed off masks
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2789
		 * so that return masks can be set for caller.
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2790
		 */
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2791
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2792
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2793
			XVA_SET_REQ(xvap, XAT_APPENDONLY);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2794
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2795
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2796
			XVA_SET_REQ(xvap, XAT_NOUNLINK);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2797
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2798
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2799
			XVA_SET_REQ(xvap, XAT_IMMUTABLE);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2800
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2801
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2802
			XVA_SET_REQ(xvap, XAT_NODUMP);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2803
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2804
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2805
			XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2806
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2807
		if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2808
			XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2809
		}
6eeea43ced42 6774886 zfs_setattr() won't allow ndmp to restore SUNWattr_rw
Mark Shellenbaum <Mark.Shellenbaum@Sun.COM>
parents: 8059
diff changeset
  2810
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2811
		if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2812
			size_t len;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2813
			dmu_object_info_t doi;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2814
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2815
			ASSERT(vp->v_type == VREG);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2816
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2817
			/* Grow the bonus buffer if necessary. */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2818
			dmu_object_info_from_db(zp->z_dbuf, &doi);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2819
			len = sizeof (xoap->xoa_av_scanstamp) +
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2820
			    sizeof (znode_phys_t);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2821
			if (len > doi.doi_bonus_size)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2822
				VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2823
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2824
		zfs_xvattr_set(zp, xvap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2825
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2826
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2827
	if (mask != 0)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2828
		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2829
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2830
	if (fuidp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2831
		zfs_fuid_info_free(fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2832
	mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2833
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2834
	if (attrzp)
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2835
		VN_RELE(ZTOV(attrzp));
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2836
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2837
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2838
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2839
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2840
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2841
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2842
3271
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2843
typedef struct zfs_zlock {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2844
	krwlock_t	*zl_rwlock;	/* lock we acquired */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2845
	znode_t		*zl_znode;	/* znode we held */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2846
	struct zfs_zlock *zl_next;	/* next in list */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2847
} zfs_zlock_t;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2848
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2849
/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2850
 * Drop locks and release vnodes that were held by zfs_rename_lock().
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2851
 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2852
static void
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2853
zfs_rename_unlock(zfs_zlock_t **zlpp)
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2854
{
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2855
	zfs_zlock_t *zl;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2856
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2857
	while ((zl = *zlpp) != NULL) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2858
		if (zl->zl_znode != NULL)
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2859
			VN_RELE(ZTOV(zl->zl_znode));
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2860
		rw_exit(zl->zl_rwlock);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2861
		*zlpp = zl->zl_next;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2862
		kmem_free(zl, sizeof (*zl));
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2863
	}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2864
}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2865
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2866
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2867
 * Search back through the directory tree, using the ".." entries.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2868
 * Lock each directory in the chain to prevent concurrent renames.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2869
 * Fail any attempt to move a directory into one of its own descendants.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2870
 * XXX - z_parent_lock can overlap with map or grow locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2871
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2872
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2873
zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2874
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2875
	zfs_zlock_t	*zl;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
  2876
	znode_t		*zp = tdzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2877
	uint64_t	rootid = zp->z_zfsvfs->z_root;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2878
	uint64_t	*oidp = &zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2879
	krwlock_t	*rwlp = &szp->z_parent_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2880
	krw_t		rw = RW_WRITER;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2881
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2882
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2883
	 * First pass write-locks szp and compares to zp->z_id.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2884
	 * Later passes read-lock zp and compare to zp->z_parent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2885
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2886
	do {
3271
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2887
		if (!rw_tryenter(rwlp, rw)) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2888
			/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2889
			 * Another thread is renaming in this path.
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2890
			 * Note that if we are a WRITER, we don't have any
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2891
			 * parent_locks held yet.
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2892
			 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2893
			if (rw == RW_READER && zp->z_id > szp->z_id) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2894
				/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2895
				 * Drop our locks and restart
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2896
				 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2897
				zfs_rename_unlock(&zl);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2898
				*zlpp = NULL;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2899
				zp = tdzp;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2900
				oidp = &zp->z_id;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2901
				rwlp = &szp->z_parent_lock;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2902
				rw = RW_WRITER;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2903
				continue;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2904
			} else {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2905
				/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2906
				 * Wait for other thread to drop its locks
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2907
				 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2908
				rw_enter(rwlp, rw);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2909
			}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2910
		}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2911
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2912
		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2913
		zl->zl_rwlock = rwlp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2914
		zl->zl_znode = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2915
		zl->zl_next = *zlpp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
		*zlpp = zl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2917
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
		if (*oidp == szp->z_id)		/* We're a descendant of szp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2919
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2920
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2921
		if (*oidp == rootid)		/* We've hit the top */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2922
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2923
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2924
		if (rw == RW_READER) {		/* i.e. not the first pass */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2925
			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2926
			if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2927
				return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2928
			zl->zl_znode = zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2929
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2930
		oidp = &zp->z_phys->zp_parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2931
		rwlp = &zp->z_parent_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2932
		rw = RW_READER;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2933
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2934
	} while (zp->z_id != sdzp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2935
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2936
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2937
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2938
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2939
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2940
 * Move an entry from the provided source directory to the target
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2941
 * directory.  Change the entry name as indicated.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2942
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2943
 *	IN:	sdvp	- Source directory containing the "old entry".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2944
 *		snm	- Old entry name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2945
 *		tdvp	- Target directory to contain the "new entry".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2946
 *		tnm	- New entry name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2947
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2948
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2949
 *		flags	- case flags
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2950
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2951
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2952
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2953
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2954
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2955
 *	sdvp,tdvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2956
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2957
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2958
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2959
zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2960
    caller_context_t *ct, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2961
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2962
	znode_t		*tdzp, *szp, *tzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2963
	znode_t		*sdzp = VTOZ(sdvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2964
	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2965
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2966
	vnode_t		*realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2967
	zfs_dirlock_t	*sdl, *tdl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2968
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2969
	zfs_zlock_t	*zl;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2970
	int		cmp, serr, terr;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2971
	int		error = 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2972
	int		zflg = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2973
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2974
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2975
	ZFS_VERIFY_ZP(sdzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2976
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2977
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2978
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2979
	 * Make sure we have the real vp for the target directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2980
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2981
	if (VOP_REALVP(tdvp, &realvp, ct) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2982
		tdvp = realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2983
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2984
	if (tdvp->v_vfsp != sdvp->v_vfsp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2985
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2986
		return (EXDEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2987
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2988
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2989
	tdzp = VTOZ(tdvp);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  2990
	ZFS_VERIFY_ZP(tdzp);
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  2991
	if (zfsvfs->z_utf8 && u8_validate(tnm,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2992
	    strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2993
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2994
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2995
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2996
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2997
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2998
		zflg |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  2999
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3000
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3001
	szp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3002
	tzp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3003
	zl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3004
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3005
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3006
	 * This is to prevent the creation of links into attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3007
	 * by renaming a linked file into/outof an attribute directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3008
	 * See the comment in zfs_link() for why this is considered bad.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3009
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3010
	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3011
	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3012
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3013
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3014
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3015
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3016
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3017
	 * Lock source and target directory entries.  To prevent deadlock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3018
	 * a lock ordering must be defined.  We lock the directory with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3019
	 * the smallest object id first, or if it's a tie, the one with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3020
	 * the lexically first name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3021
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3022
	if (sdzp->z_id < tdzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3023
		cmp = -1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3024
	} else if (sdzp->z_id > tdzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3025
		cmp = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3026
	} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3027
		/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3028
		 * First compare the two name arguments without
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3029
		 * considering any case folding.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3030
		 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3031
		int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3032
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3033
		cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3034
		ASSERT(error == 0 || !zfsvfs->z_utf8);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3035
		if (cmp == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3036
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3037
			 * POSIX: "If the old argument and the new argument
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3038
			 * both refer to links to the same existing file,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3039
			 * the rename() function shall return successfully
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3040
			 * and perform no other action."
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3041
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3042
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3043
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3044
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3045
		/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3046
		 * If the file system is case-folding, then we may
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3047
		 * have some more checking to do.  A case-folding file
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3048
		 * system is either supporting mixed case sensitivity
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3049
		 * access or is completely case-insensitive.  Note
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3050
		 * that the file system is always case preserving.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3051
		 *
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3052
		 * In mixed sensitivity mode case sensitive behavior
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3053
		 * is the default.  FIGNORECASE must be used to
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3054
		 * explicitly request case insensitive behavior.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3055
		 *
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3056
		 * If the source and target names provided differ only
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3057
		 * by case (e.g., a request to rename 'tim' to 'Tim'),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3058
		 * we will treat this as a special case in the
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3059
		 * case-insensitive mode: as long as the source name
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3060
		 * is an exact match, we will allow this to proceed as
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3061
		 * a name-change request.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3062
		 */
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3063
		if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3064
		    (zfsvfs->z_case == ZFS_CASE_MIXED &&
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3065
		    flags & FIGNORECASE)) &&
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3066
		    u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3067
		    &error) == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3068
			/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3069
			 * case preserving rename request, require exact
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3070
			 * name matches
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3071
			 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3072
			zflg |= ZCIEXACT;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3073
			zflg &= ~ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3074
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3075
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3076
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3077
	if (cmp < 0) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3078
		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3079
		    ZEXISTS | zflg, NULL, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3080
		terr = zfs_dirent_lock(&tdl,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3081
		    tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3082
	} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3083
		terr = zfs_dirent_lock(&tdl,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3084
		    tdzp, tnm, &tzp, zflg, NULL, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3085
		serr = zfs_dirent_lock(&sdl,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3086
		    sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3087
		    NULL, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3088
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3089
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3090
	if (serr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3091
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3092
		 * Source entry invalid or not there.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3093
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3094
		if (!terr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3095
			zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3096
			if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3097
				VN_RELE(ZTOV(tzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3098
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3099
		if (strcmp(snm, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3100
			serr = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3101
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3102
		return (serr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3103
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3104
	if (terr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3105
		zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3106
		VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3107
		if (strcmp(tnm, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3108
			terr = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3109
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3110
		return (terr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3111
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3112
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3113
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3114
	 * Must have write access at the source to remove the old entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3115
	 * and write access at the target to create the new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3116
	 * Note that if target and source are the same, this can be
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3117
	 * done in a single check.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3118
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3119
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3120
	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3121
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3122
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3123
	if (ZTOV(szp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3124
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3125
		 * Check to make sure rename is valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3126
		 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3127
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3128
		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3129
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3130
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3131
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3132
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3133
	 * Does target exist?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3134
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3135
	if (tzp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3136
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3137
		 * Source and target must be the same type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3138
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3139
		if (ZTOV(szp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3140
			if (ZTOV(tzp)->v_type != VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3141
				error = ENOTDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3142
				goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3143
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3144
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3145
			if (ZTOV(tzp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3146
				error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3147
				goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3148
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3149
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3150
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3151
		 * POSIX dictates that when the source and target
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3152
		 * entries refer to the same file object, rename
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3153
		 * must do nothing and exit without error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3154
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3155
		if (szp->z_id == tzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3156
			error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3157
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3158
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3159
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3160
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3161
	vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3162
	if (tzp)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3163
		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3164
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3165
	/*
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3166
	 * notify the target directory if it is not the same
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3167
	 * as source directory.
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3168
	 */
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3169
	if (tdvp != sdvp) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3170
		vnevent_rename_dest_dir(tdvp, ct);
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3171
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3172
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3173
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3174
	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3175
	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3176
	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3177
	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3178
	if (sdzp != tdzp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3179
		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3180
	if (tzp)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3181
		dmu_tx_hold_bonus(tx, tzp->z_id);	/* parent changes */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  3182
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3183
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3184
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3185
		if (zl != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3186
			zfs_rename_unlock(&zl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3187
		zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3188
		zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3189
		VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3190
		if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3191
			VN_RELE(ZTOV(tzp));
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3192
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3193
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3194
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3195
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3196
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3197
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3198
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3199
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3200
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3201
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3202
	if (tzp)	/* Attempt to remove the existing target */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3203
		error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3204
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3205
	if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3206
		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3207
		if (error == 0) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3208
			szp->z_phys->zp_flags |= ZFS_AV_MODIFIED;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3209
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3210
			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3211
			ASSERT(error == 0);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3212
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3213
			zfs_log_rename(zilog, tx,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3214
			    TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3215
			    sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp);
6976
cae5f06df471 PSARC 2008/388 Short circuit for vdev probe failure
eschrock
parents: 6743
diff changeset
  3216
cae5f06df471 PSARC 2008/388 Short circuit for vdev probe failure
eschrock
parents: 6743
diff changeset
  3217
			/* Update path information for the target vnode */
cae5f06df471 PSARC 2008/388 Short circuit for vdev probe failure
eschrock
parents: 6743
diff changeset
  3218
			vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3219
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3220
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3221
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3222
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3223
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3224
	if (zl != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3225
		zfs_rename_unlock(&zl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3226
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3227
	zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3228
	zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3229
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3230
	VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3231
	if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3232
		VN_RELE(ZTOV(tzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3233
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3234
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3235
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3236
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3237
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3238
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3239
 * Insert the indicated symbolic reference entry into the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3240
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3241
 *	IN:	dvp	- Directory to contain new symbolic link.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3242
 *		link	- Name for new symlink entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3243
 *		vap	- Attributes of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3244
 *		target	- Target path of new symlink.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3245
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3246
 *		ct	- caller context
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3247
 *		flags	- case flags
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3248
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3249
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3250
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3251
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3252
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3253
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3254
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3255
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3256
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3257
zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3258
    caller_context_t *ct, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3259
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3260
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3261
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3262
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3263
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3264
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3265
	int		len = strlen(link);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3266
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3267
	int		zflg = ZNEW;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3268
	zfs_fuid_info_t *fuidp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3269
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3270
	ASSERT(vap->va_type == VLNK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3271
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3272
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3273
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3274
	zilog = zfsvfs->z_log;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3275
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3276
	if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3277
	    NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3278
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3279
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3280
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3281
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3282
		zflg |= ZCILOOK;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3283
top:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3284
	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3285
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3286
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3287
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3288
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3289
	if (len > MAXPATHLEN) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3290
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3291
		return (ENAMETOOLONG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3292
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3293
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3294
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3295
	 * Attempt to lock directory; fail if entry already exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3296
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3297
	error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3298
	if (error) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3299
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3300
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3301
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3302
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3303
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3304
	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3305
	dmu_tx_hold_bonus(tx, dzp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3306
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3307
	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3308
		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
5824
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3309
	if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3310
		if (zfsvfs->z_fuid_obj == 0) {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3311
			dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3312
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3313
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3314
			dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3315
		} else {
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3316
			dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3317
			dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0,
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3318
			    FUID_SIZE_ESTIMATE(zfsvfs));
1d2d522d19b5 6603908 can't change mode if FS/dir is out of quota
marks
parents: 5771
diff changeset
  3319
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3320
	}
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3321
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3322
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3323
		zfs_dirent_unlock(dl);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3324
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3325
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3326
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3327
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3328
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3329
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3330
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3331
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3332
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3333
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3334
	dmu_buf_will_dirty(dzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3335
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3336
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3337
	 * Create a new object for the symlink.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3338
	 * Put the link content into bonus buffer if it will fit;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3339
	 * otherwise, store it just like any other file data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3340
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3341
	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3342
		zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3343
		if (len != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3344
			bcopy(link, zp->z_phys + 1, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3345
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3346
		dmu_buf_t *dbp;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3347
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3348
		zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp);
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3349
		/*
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3350
		 * Nothing can access the znode yet so no locking needed
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3351
		 * for growing the znode's blocksize.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3352
		 */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3353
		zfs_grow_blocksize(zp, len, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3354
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3355
		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os,
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3356
		    zp->z_id, 0, FTAG, &dbp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3357
		dmu_buf_will_dirty(dbp, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3358
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3359
		ASSERT3U(len, <=, dbp->db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3360
		bcopy(link, dbp->db_data, len);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3361
		dmu_buf_rele(dbp, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3362
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3363
	zp->z_phys->zp_size = len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3364
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3365
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3366
	 * Insert the new object into the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3367
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3368
	(void) zfs_link_create(dl, zp, tx, ZNEW);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3369
out:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3370
	if (error == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3371
		uint64_t txtype = TX_SYMLINK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3372
		if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3373
			txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3374
		zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3375
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3376
	if (fuidp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3377
		zfs_fuid_info_free(fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3378
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3379
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3381
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3382
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3383
	VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3384
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3385
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3386
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3387
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3388
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3389
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3390
 * Return, in the buffer contained in the provided uio structure,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3391
 * the symbolic path referred to by vp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3392
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3393
 *	IN:	vp	- vnode of symbolic link.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3394
 *		uoip	- structure to contain the link path.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3395
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3396
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3397
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3398
 *	OUT:	uio	- structure to contain the link path.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3399
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3400
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3401
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3402
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3403
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3404
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3405
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3406
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3407
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3408
zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3409
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3410
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3411
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3412
	size_t		bufsz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3413
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3414
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3415
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3416
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3417
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3418
	bufsz = (size_t)zp->z_phys->zp_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3419
	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3420
		error = uiomove(zp->z_phys + 1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3421
		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3422
	} else {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3423
		dmu_buf_t *dbp;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3424
		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3425
		if (error) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3426
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3427
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3428
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3429
		error = uiomove(dbp->db_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3430
		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3431
		dmu_buf_rele(dbp, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3432
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3433
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3434
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3435
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3436
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3437
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3439
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3440
 * Insert a new entry into directory tdvp referencing svp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3441
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3442
 *	IN:	tdvp	- Directory to contain new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3443
 *		svp	- vnode of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3444
 *		name	- name of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3445
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3446
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3447
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3448
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3449
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3450
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3451
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3452
 *	tdvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3453
 *	 svp - ctime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3454
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3455
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3456
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3457
zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3458
    caller_context_t *ct, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3459
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3460
	znode_t		*dzp = VTOZ(tdvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3461
	znode_t		*tzp, *szp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3462
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3463
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3464
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3465
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3466
	vnode_t		*realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3467
	int		error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3468
	int		zf = ZNEW;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3469
	uid_t		owner;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3470
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3471
	ASSERT(tdvp->v_type == VDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3472
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3473
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3474
	ZFS_VERIFY_ZP(dzp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3475
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3476
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3477
	if (VOP_REALVP(svp, &realvp, ct) == 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3478
		svp = realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3479
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3480
	if (svp->v_vfsp != tdvp->v_vfsp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3481
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3482
		return (EXDEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3483
	}
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3484
	szp = VTOZ(svp);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3485
	ZFS_VERIFY_ZP(szp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3486
5498
334b476844ca 6622831 normalization properties are not preserved by "zfs send"
timh
parents: 5446
diff changeset
  3487
	if (zfsvfs->z_utf8 && u8_validate(name,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3488
	    strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3489
		ZFS_EXIT(zfsvfs);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3490
		return (EILSEQ);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3491
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3492
	if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3493
		zf |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3494
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3495
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3496
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3497
	 * We do not support links between attributes and non-attributes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3498
	 * because of the potential security risk of creating links
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3499
	 * into "normal" file space in order to circumvent restrictions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3500
	 * imposed in attribute space.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3501
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3502
	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3503
	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3504
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3505
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3506
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3507
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3508
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3509
	 * POSIX dictates that we return EPERM here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3510
	 * Better choices include ENOTSUP or EISDIR.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3511
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3512
	if (svp->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3513
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3514
		return (EPERM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3515
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3516
5959
1e1904b8526d 6650192 zfs ACL/fuid code could use some minor cleanup
marks
parents: 5929
diff changeset
  3517
	owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3518
	if (owner != crgetuid(cr) &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3519
	    secpolicy_basic_link(cr) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3520
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3521
		return (EPERM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3522
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3523
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3524
	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3525
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3526
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3527
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3528
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3529
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3530
	 * Attempt to lock directory; fail if entry already exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3531
	 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3532
	error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3533
	if (error) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3534
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3535
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3536
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3537
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3538
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3539
	dmu_tx_hold_bonus(tx, szp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3540
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3541
	error = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3542
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3543
		zfs_dirent_unlock(dl);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3544
		if (error == ERESTART) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3545
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3546
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3547
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3548
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3549
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3550
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3551
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3552
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3553
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3554
	error = zfs_link_create(dl, szp, tx, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3555
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3556
	if (error == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3557
		uint64_t txtype = TX_LINK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3558
		if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3559
			txtype |= TX_CI;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3560
		zfs_log_link(zilog, tx, txtype, dzp, szp, name);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3561
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3562
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3563
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3565
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3566
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3567
	if (error == 0) {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3568
		vnevent_link(svp, ct);
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3569
	}
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3570
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3571
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3572
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3573
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3574
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3575
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3576
 * zfs_null_putapage() is used when the file system has been force
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3577
 * unmounted. It just drops the pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3578
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3579
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3580
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3581
zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3582
		size_t *lenp, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3583
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3584
	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3585
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3586
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3587
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3588
/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3589
 * Push a page out to disk, klustering if possible.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3590
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3591
 *	IN:	vp	- file to push page to.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3592
 *		pp	- page to push.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3593
 *		flags	- additional flags.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3594
 *		cr	- credentials of caller.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3595
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3596
 *	OUT:	offp	- start of range pushed.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3597
 *		lenp	- len of range pushed.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3598
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3599
 *	RETURN:	0 if success
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3600
 *		error code if failure
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3601
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3602
 * NOTE: callers must have locked the page to be pushed.  On
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3603
 * exit, the page (and all other pages in the kluster) must be
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3604
 * unlocked.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3605
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3606
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3607
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3608
zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3609
		size_t *lenp, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3610
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3611
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3612
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3613
	zilog_t		*zilog = zfsvfs->z_log;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3614
	dmu_tx_t	*tx;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3615
	rl_t		*rl;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3616
	u_offset_t	off, koff;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3617
	size_t		len, klen;
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3618
	uint64_t	filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3619
	int		err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3620
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3621
	filesz = zp->z_phys->zp_size;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3622
	off = pp->p_offset;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3623
	len = PAGESIZE;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3624
	/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3625
	 * If our blocksize is bigger than the page size, try to kluster
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3626
	 * multiple pages so that we write a full block (thus avoiding
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3627
	 * a read-modify-write).
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3628
	 */
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3629
	if (off < filesz && zp->z_blksz > PAGESIZE) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3630
		if (!ISP2(zp->z_blksz)) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3631
			/* Only one block in the file. */
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3632
			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3633
			koff = 0;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3634
		} else {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3635
			klen = zp->z_blksz;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3636
			koff = P2ALIGN(off, (u_offset_t)klen);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3637
		}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3638
		ASSERT(koff <= filesz);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3639
		if (koff + klen > filesz)
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3640
			klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3641
		pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3642
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3643
	ASSERT3U(btop(len), ==, btopr(len));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3644
top:
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3645
	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3646
	/*
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3647
	 * Can't push pages past end-of-file.
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3648
	 */
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3649
	filesz = zp->z_phys->zp_size;
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3650
	if (off >= filesz) {
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3651
		/* ignore all pages */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3652
		err = 0;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3653
		goto out;
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3654
	} else if (off + len > filesz) {
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3655
		int npages = btopr(filesz - off);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3656
		page_t *trunc;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3657
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3658
		page_list_break(&pp, &trunc, npages);
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3659
		/* ignore pages past end of file */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3660
		if (trunc)
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3661
			pvn_write_done(trunc, flags);
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3662
		len = filesz - off;
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3663
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3664
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3665
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3666
	dmu_tx_hold_write(tx, zp->z_id, off, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3667
	dmu_tx_hold_bonus(tx, zp->z_id);
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3668
	err = dmu_tx_assign(tx, TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3669
	if (err != 0) {
8227
f7d7be9b1f56 6765294 Refactor replay
Neil Perrin <Neil.Perrin@Sun.COM>
parents: 8190
diff changeset
  3670
		if (err == ERESTART) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3671
			zfs_range_unlock(rl);
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3672
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3673
			dmu_tx_abort(tx);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3674
			err = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3675
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3676
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3677
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3678
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3679
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3680
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3681
	if (zp->z_blksz <= PAGESIZE) {
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
  3682
		caddr_t va = zfs_map_page(pp, S_READ);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3683
		ASSERT3U(len, <=, PAGESIZE);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3684
		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
  3685
		zfs_unmap_page(pp, va);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3686
	} else {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3687
		err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3688
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3689
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3690
	if (err == 0) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3691
		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
  3692
		zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3693
		dmu_tx_commit(tx);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3694
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3695
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3696
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
  3697
	zfs_range_unlock(rl);
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  3698
	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3699
	if (offp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3700
		*offp = off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3701
	if (lenp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3702
		*lenp = len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3703
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3704
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3705
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3706
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3707
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3708
 * Copy the portion of the file indicated from pages into the file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3709
 * The pages are stored in a page list attached to the files vnode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3710
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3711
 *	IN:	vp	- vnode of file to push page data to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3712
 *		off	- position in file to put data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3713
 *		len	- amount of data to write.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3714
 *		flags	- flags to control the operation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3715
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3716
 *		ct	- caller context.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3717
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3718
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3719
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3720
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3721
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3722
 *	vp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3723
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3724
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3725
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3726
zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3727
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3728
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3729
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3730
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3731
	page_t		*pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3732
	size_t		io_len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3733
	u_offset_t	io_off;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3734
	uint64_t	filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3735
	int		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3736
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3737
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3738
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3739
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3740
	if (len == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3741
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3742
		 * Search the entire vp list for pages >= off.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3743
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3744
		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3745
		    flags, cr);
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  3746
		goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3747
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3748
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3749
	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3750
	if (off > filesz) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3751
		/* past end of file */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3752
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3753
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3754
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3755
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3756
	len = MIN(len, filesz - off);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3757
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  3758
	for (io_off = off; io_off < off + len; io_off += io_len) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3759
		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3760
			pp = page_lookup(vp, io_off,
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
  3761
			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3762
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3763
			pp = page_lookup_nowait(vp, io_off,
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
  3764
			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3765
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3766
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3767
		if (pp != NULL && pvn_getdirty(pp, flags)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3768
			int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3769
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3770
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3771
			 * Found a dirty page to push
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3772
			 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3773
			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3774
			if (err)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3775
				error = err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3776
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3777
			io_len = PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3778
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3779
	}
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  3780
out:
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  3781
	if ((flags & B_ASYNC) == 0)
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  3782
		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3783
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3784
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3785
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3786
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3787
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3788
void
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3789
zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3790
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3791
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3792
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3793
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3794
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3795
	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3796
	if (zp->z_dbuf == NULL) {
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3797
		/*
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5545
diff changeset
  3798
		 * The fs has been unmounted, or we did a
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5545
diff changeset
  3799
		 * suspend/resume and this file no longer exists.
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3800
		 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3801
		if (vn_has_cached_data(vp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3802
			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3803
			    B_INVAL, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3804
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3805
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3806
		mutex_enter(&zp->z_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3807
		vp->v_count = 0; /* count arrives as 1 */
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3808
		mutex_exit(&zp->z_lock);
5642
504c84876fda 6513209 Destroying pools under stress caused a hang in arc_flush
maybee
parents: 5545
diff changeset
  3809
		rw_exit(&zfsvfs->z_teardown_inactive_lock);
5446
51fbc14b301d 6421210 zfs rollback without unmounting
ahrens
parents: 5386
diff changeset
  3810
		zfs_znode_free(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3811
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3812
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3813
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3814
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3815
	 * Attempt to push any data in the page cache.  If this fails
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3816
	 * we will get kicked out later in zfs_zinactive().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3817
	 */
1298
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3818
	if (vn_has_cached_data(vp)) {
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3819
		(void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3820
		    cr);
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3821
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3822
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  3823
	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3824
		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3825
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3826
		dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3827
		error = dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3828
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3829
			dmu_tx_abort(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3830
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3831
			dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3832
			mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3833
			zp->z_atime_dirty = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3834
			mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3835
			dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3836
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3837
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3838
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3839
	zfs_zinactive(zp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3840
	rw_exit(&zfsvfs->z_teardown_inactive_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3841
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3842
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3843
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3844
 * Bounds-check the seek operation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3845
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3846
 *	IN:	vp	- vnode seeking within
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3847
 *		ooff	- old file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3848
 *		noffp	- pointer to new file offset
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3849
 *		ct	- caller context
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3850
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3851
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3852
 *		EINVAL if new offset invalid
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3853
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3854
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3855
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3856
zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3857
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3858
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3859
	if (vp->v_type == VDIR)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3860
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3861
	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3862
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3863
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3864
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3865
 * Pre-filter the generic locking function to trap attempts to place
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3866
 * a mandatory lock on a memory mapped file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3867
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3868
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3869
zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3870
    flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3871
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3872
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3873
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3874
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3875
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3876
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  3877
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3878
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3879
	/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3880
	 * We are following the UFS semantics with respect to mapcnt
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3881
	 * here: If we see that the file is mapped already, then we will
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3882
	 * return an error, but we don't worry about races between this
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3883
	 * function and zfs_map().
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3884
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3885
	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3886
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3887
		return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3888
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  3889
	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3890
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3891
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3892
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3893
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3894
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3895
 * If we can't find a page in the cache, we will create a new page
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3896
 * and fill it with file data.  For efficiency, we may try to fill
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3897
 * multiple pages at once (klustering).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3898
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3899
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3900
zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3901
    caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3902
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3903
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3904
	page_t *pp, *cur_pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3905
	objset_t *os = zp->z_zfsvfs->z_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3906
	caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3907
	u_offset_t io_off, total;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3908
	uint64_t oid = zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3909
	size_t io_len;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3910
	uint64_t filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3911
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3912
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3913
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3914
	 * If we are only asking for a single page don't bother klustering.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3915
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3916
	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3917
	if (off >= filesz)
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3918
		return (EFAULT);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3919
	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3920
		io_off = off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3921
		io_len = PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3922
		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3923
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3924
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3925
		 * Try to fill a kluster of pages (a blocks worth).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3926
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3927
		size_t klen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3928
		u_offset_t koff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3929
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3930
		if (!ISP2(zp->z_blksz)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3931
			/* Only one block in the file. */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3932
			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3933
			koff = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3934
		} else {
3131
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3935
			/*
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3936
			 * It would be ideal to align our offset to the
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3937
			 * blocksize but doing so has resulted in some
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3938
			 * strange application crashes. For now, we
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3939
			 * leave the offset as is and only adjust the
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3940
			 * length if we are off the end of the file.
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3941
			 */
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3942
			koff = off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3943
			klen = plsz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3944
		}
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3945
		ASSERT(koff <= filesz);
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3946
		if (koff + klen > filesz)
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3947
			klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3948
		ASSERT3U(off, >=, koff);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3949
		ASSERT3U(off, <, koff + klen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3950
		pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
  3951
		    &io_len, koff, klen, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3952
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3953
	if (pp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3954
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3955
		 * Some other thread entered the page before us.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3956
		 * Return to zfs_getpage to retry the lookup.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3957
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3958
		*pl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3959
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3960
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3961
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3962
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3963
	 * Fill the pages in the kluster.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3964
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3965
	cur_pp = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3966
	for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3967
		ASSERT3U(io_off, ==, cur_pp->p_offset);
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
  3968
		va = zfs_map_page(cur_pp, S_WRITE);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3969
		err = dmu_read(os, oid, io_off, PAGESIZE, va);
7315
cdba25672122 6735592 "zio" vmem arenas are confusing, not reported separately
Jonathan W Adams <Jonathan.Adams@Sun.COM>
parents: 7294
diff changeset
  3970
		zfs_unmap_page(cur_pp, va);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3971
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3972
			/* On error, toss the entire kluster */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3973
			pvn_read_done(pp, B_ERROR);
7294
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
  3974
			/* convert checksum errors into IO errors */
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
  3975
			if (err == ECKSUM)
c9c31ef4c960 PSARC 2008/486 Intent log replay failure handling
perrin
parents: 6992
diff changeset
  3976
				err = EIO;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3977
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3978
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3979
		cur_pp = cur_pp->p_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3980
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3981
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3982
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3983
	 * Fill in the page list array from the kluster.  If
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3984
	 * there are too many pages in the kluster, return
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3985
	 * as many pages as possible starting from the desired
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3986
	 * offset `off'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3987
	 * NOTE: the page list will always be null terminated.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3988
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3989
	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3990
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3991
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3992
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3993
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3994
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3995
 * Return pointers to the pages for the file region [off, off + len]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3996
 * in the pl array.  If plsz is greater than len, this function may
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3997
 * also return page pointers from before or after the specified
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3998
 * region (i.e. some region [off', off' + plsz]).  These additional
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3999
 * pages are only returned if they are already in the cache, or were
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4000
 * created as part of a klustered read.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4001
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4002
 *	IN:	vp	- vnode of file to get data from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4003
 *		off	- position in file to get data from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4004
 *		len	- amount of data to retrieve.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4005
 *		plsz	- length of provided page list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4006
 *		seg	- segment to obtain pages for.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4007
 *		addr	- virtual address of fault.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4008
 *		rw	- mode of created pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4009
 *		cr	- credentials of caller.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4010
 *		ct	- caller context.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4011
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4012
 *	OUT:	protp	- protection mode of created pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4013
 *		pl	- list of pages created.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4014
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4015
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4016
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4017
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4018
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4019
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4020
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4021
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4022
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4023
zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4024
	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4025
	enum seg_rw rw, cred_t *cr, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4026
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4027
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4028
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4029
	page_t		*pp, **pl0 = pl;
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4030
	int		need_unlock = 0, err = 0;
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4031
	offset_t	orig_off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4032
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4033
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4034
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4035
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4036
	if (protp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4037
		*protp = PROT_ALL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4038
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4039
	/* no faultahead (for now) */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4040
	if (pl == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4041
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4042
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4043
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4044
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4045
	/* can't fault past EOF */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4046
	if (off >= zp->z_phys->zp_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4047
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4048
		return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4049
	}
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4050
	orig_off = off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4052
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4053
	 * If we already own the lock, then we must be page faulting
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4054
	 * in the middle of a write to this file (i.e., we are writing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4055
	 * to this file using data from a mapped region of the file).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4056
	 */
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4057
	if (rw_owner(&zp->z_map_lock) != curthread) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4058
		rw_enter(&zp->z_map_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4059
		need_unlock = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4060
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4061
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4062
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4063
	 * Loop through the requested range [off, off + len] looking
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4064
	 * for pages.  If we don't find a page, we will need to create
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4065
	 * a new page and fill it with data from the file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4066
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4067
	while (len > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4068
		if (plsz < PAGESIZE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4069
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4070
		if (pp = page_lookup(vp, off, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4071
			*pl++ = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4072
			off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4073
			addr += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4074
			len -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4075
			plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4076
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4077
			err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw);
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4078
			if (err)
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4079
				goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4080
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4081
			 * klustering may have changed our region
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4082
			 * to be block aligned.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4083
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4084
			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4085
				int delta = off - pp->p_offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4086
				len += delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4087
				off -= delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4088
				addr -= delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4089
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4090
			while (*pl) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4091
				pl++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4092
				off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4093
				addr += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4094
				plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4095
				if (len > PAGESIZE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4096
					len -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4097
				else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4098
					len = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4099
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4100
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4101
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4102
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4103
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4104
	 * Fill out the page array with any pages already in the cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4105
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4106
	while (plsz > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4107
		pp = page_lookup_nowait(vp, off, SE_SHARED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4108
		if (pp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4109
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4110
		*pl++ = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4111
		off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4112
		plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4113
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4114
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4115
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4116
out:
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4117
	/*
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4118
	 * We can't grab the range lock for the page as reader which would
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4119
	 * stop truncation as this leads to deadlock. So we need to recheck
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4120
	 * the file size.
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4121
	 */
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4122
	if (orig_off >= zp->z_phys->zp_size)
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4123
		err = EFAULT;
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4124
	if (err) {
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4125
		/*
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4126
		 * Release any pages we have previously locked.
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4127
		 */
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4128
		while (pl > pl0)
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4129
			page_unlock(*--pl);
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4130
	}
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  4131
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4132
	*pl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4133
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4134
	if (need_unlock)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4135
		rw_exit(&zp->z_map_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4136
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4137
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4138
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4139
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4140
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4141
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4142
 * Request a memory map for a section of a file.  This code interacts
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4143
 * with common code and the VM system as follows:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4144
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4145
 *	common code calls mmap(), which ends up in smmap_common()
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4146
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4147
 *	this calls VOP_MAP(), which takes you into (say) zfs
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4148
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4149
 *	zfs_map() calls as_map(), passing segvn_create() as the callback
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4150
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4151
 *	segvn_create() creates the new segment and calls VOP_ADDMAP()
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4152
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4153
 *	zfs_addmap() updates z_mapcnt
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4154
 */
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4155
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4156
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4157
zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4158
    size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4159
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4160
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4161
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4162
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4163
	segvn_crargs_t	vn_a;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4164
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4165
5929
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4166
	ZFS_ENTER(zfsvfs);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4167
	ZFS_VERIFY_ZP(zp);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4168
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4169
	if ((prot & PROT_WRITE) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4170
	    (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY |
5929
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4171
	    ZFS_APPENDONLY))) {
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4172
		ZFS_EXIT(zfsvfs);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4173
		return (EPERM);
5929
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4174
	}
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4175
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4176
	if ((prot & (PROT_READ | PROT_EXEC)) &&
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4177
	    (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) {
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4178
		ZFS_EXIT(zfsvfs);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4179
		return (EACCES);
07d036def59b 6650101 SNAS: Read access is allowed after stream file is quarantined from CIFS share.
marks
parents: 5824
diff changeset
  4180
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4181
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4182
	if (vp->v_flag & VNOMAP) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4183
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4184
		return (ENOSYS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4185
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4186
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4187
	if (off < 0 || len > MAXOFFSET_T - off) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4188
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4189
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4190
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4192
	if (vp->v_type != VREG) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4193
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4194
		return (ENODEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4195
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4196
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4197
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4198
	 * If file is locked, disallow mapping.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4199
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4200
	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4201
		ZFS_EXIT(zfsvfs);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4202
		return (EAGAIN);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4203
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4204
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4205
	as_rangelock(as);
6036
c98c367c32cb PSARC 2006/123 Mmap compatibility
mec
parents: 5959
diff changeset
  4206
	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
c98c367c32cb PSARC 2006/123 Mmap compatibility
mec
parents: 5959
diff changeset
  4207
	if (error != 0) {
c98c367c32cb PSARC 2006/123 Mmap compatibility
mec
parents: 5959
diff changeset
  4208
		as_rangeunlock(as);
c98c367c32cb PSARC 2006/123 Mmap compatibility
mec
parents: 5959
diff changeset
  4209
		ZFS_EXIT(zfsvfs);
c98c367c32cb PSARC 2006/123 Mmap compatibility
mec
parents: 5959
diff changeset
  4210
		return (error);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4211
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4212
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4213
	vn_a.vp = vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4214
	vn_a.offset = (u_offset_t)off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4215
	vn_a.type = flags & MAP_TYPE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4216
	vn_a.prot = prot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4217
	vn_a.maxprot = maxprot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4218
	vn_a.cred = cr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4219
	vn_a.amp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4220
	vn_a.flags = flags & ~MAP_TYPE;
1417
fa316336e215 6377034 setting physmem in /etc/system does not have desired effect on x86
kchow
parents: 1394
diff changeset
  4221
	vn_a.szc = 0;
fa316336e215 6377034 setting physmem in /etc/system does not have desired effect on x86
kchow
parents: 1394
diff changeset
  4222
	vn_a.lgrp_mem_policy_flags = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4223
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4224
	error = as_map(as, *addrp, len, segvn_create, &vn_a);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4225
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4226
	as_rangeunlock(as);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4227
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4228
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4229
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4230
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4231
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4232
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4233
zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4234
    size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4235
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4236
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4237
	uint64_t pages = btopr(len);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4238
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4239
	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4240
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4241
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4242
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4243
/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4244
 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4245
 * more accurate mtime for the associated file.  Since we don't have a way of
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4246
 * detecting when the data was actually modified, we have to resort to
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4247
 * heuristics.  If an explicit msync() is done, then we mark the mtime when the
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4248
 * last page is pushed.  The problem occurs when the msync() call is omitted,
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4249
 * which by far the most common case:
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4250
 *
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4251
 * 	open()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4252
 * 	mmap()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4253
 * 	<modify memory>
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4254
 * 	munmap()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4255
 * 	close()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4256
 * 	<time lapse>
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4257
 * 	putpage() via fsflush
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4258
 *
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4259
 * If we wait until fsflush to come along, we can have a modification time that
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4260
 * is some arbitrary point in the future.  In order to prevent this in the
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4261
 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4262
 * torn down.
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4263
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4264
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4265
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4266
zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4267
    size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4268
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4269
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4270
	uint64_t pages = btopr(len);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4271
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4272
	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  4273
	atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4274
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4275
	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4276
	    vn_has_cached_data(vp))
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4277
		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  4278
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4279
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4280
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4282
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4283
 * Free or allocate space in a file.  Currently, this function only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4284
 * supports the `F_FREESP' command.  However, this command is somewhat
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4285
 * misnamed, as its functionality includes the ability to allocate as
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4286
 * well as free space.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4287
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4288
 *	IN:	vp	- vnode of file to free data in.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4289
 *		cmd	- action to take (only F_FREESP supported).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4290
 *		bfp	- section of file to free/alloc.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4291
 *		flag	- current file open mode flags.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4292
 *		offset	- current file offset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4293
 *		cr	- credentials of caller [UNUSED].
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4294
 *		ct	- caller context.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4295
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4296
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4297
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4298
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4299
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4300
 *	vp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4301
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4302
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4303
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4304
zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4305
    offset_t offset, cred_t *cr, caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4306
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4307
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4308
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4309
	uint64_t	off, len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4310
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4311
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4312
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4313
	ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4314
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4315
	if (cmd != F_FREESP) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4316
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4317
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4318
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4319
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4320
	if (error = convoff(vp, bfp, 0, offset)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4321
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4322
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4323
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4324
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4325
	if (bfp->l_len < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4326
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4327
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4328
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4329
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4330
	off = bfp->l_start;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  4331
	len = bfp->l_len; /* 0 means from off to end of file */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  4332
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6976
diff changeset
  4333
	error = zfs_freesp(zp, off, len, flag, TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4334
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4335
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4336
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4337
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4338
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4339
/*ARGSUSED*/
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4340
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4341
zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4342
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4343
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4344
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  4345
	uint32_t	gen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4346
	uint64_t	object = zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4347
	zfid_short_t	*zfid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4348
	int		size, i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4349
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4350
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4351
	ZFS_VERIFY_ZP(zp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  4352
	gen = (uint32_t)zp->z_gen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4353
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4354
	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4355
	if (fidp->fid_len < size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4356
		fidp->fid_len = size;
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
  4357
		ZFS_EXIT(zfsvfs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4358
		return (ENOSPC);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4359
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4360
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4361
	zfid = (zfid_short_t *)fidp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4362
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4363
	zfid->zf_len = size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4364
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4365
	for (i = 0; i < sizeof (zfid->zf_object); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4366
		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4367
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4368
	/* Must have a non-zero generation number to distinguish from .zfs */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4369
	if (gen == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4370
		gen = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4371
	for (i = 0; i < sizeof (zfid->zf_gen); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4372
		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4373
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4374
	if (size == LONG_FID_LEN) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4375
		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4376
		zfid_long_t	*zlfid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4377
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4378
		zlfid = (zfid_long_t *)fidp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4379
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4380
		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4381
			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4382
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4383
		/* XXX - this should be the generation number for the objset */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4384
		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4385
			zlfid->zf_setgen[i] = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4386
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4387
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4388
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4389
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4390
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4391
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4392
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4393
zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4394
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4395
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4396
	znode_t		*zp, *xzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4397
	zfsvfs_t	*zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4398
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4399
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4400
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4401
	switch (cmd) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4402
	case _PC_LINK_MAX:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4403
		*valp = ULONG_MAX;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4404
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4405
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4406
	case _PC_FILESIZEBITS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4407
		*valp = 64;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4408
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4409
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4410
	case _PC_XATTR_EXISTS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4411
		zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4412
		zfsvfs = zp->z_zfsvfs;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4413
		ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4414
		ZFS_VERIFY_ZP(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4415
		*valp = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4416
		error = zfs_dirent_lock(&dl, zp, "", &xzp,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4417
		    ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4418
		if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4419
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4420
			if (!zfs_dirempty(xzp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4421
				*valp = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4422
			VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4423
		} else if (error == ENOENT) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4424
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4425
			 * If there aren't extended attributes, it's the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4426
			 * same as having zero of them.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4427
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4428
			error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4429
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4430
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4431
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4432
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4433
	case _PC_SATTR_ENABLED:
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4434
	case _PC_SATTR_EXISTS:
7757
bf4a45ecb669 PSARC/2008/588 VFSFT_SYSATTR_VIEWS
Janice Chang <Janice.Chang@Sun.COM>
parents: 7315
diff changeset
  4435
		*valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4436
		    (vp->v_type == VREG || vp->v_type == VDIR);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4437
		return (0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4438
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4439
	case _PC_ACL_ENABLED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4440
		*valp = _ACL_ACE_ENABLED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4441
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4442
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4443
	case _PC_MIN_HOLE_SIZE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4444
		*valp = (ulong_t)SPA_MINBLOCKSIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4445
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4446
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4447
	default:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4448
		return (fs_pathconf(vp, cmd, valp, cr, ct));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4449
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4450
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4451
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4452
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4453
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4454
zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4455
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4456
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4457
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4458
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4459
	int error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4460
	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4461
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4462
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4463
	ZFS_VERIFY_ZP(zp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4464
	error = zfs_getacl(zp, vsecp, skipaclchk, cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4465
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4466
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4467
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4468
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4470
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4471
static int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4472
zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4473
    caller_context_t *ct)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4474
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4475
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4476
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4477
	int error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4478
	boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4479
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4480
	ZFS_ENTER(zfsvfs);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5331
diff changeset
  4481
	ZFS_VERIFY_ZP(zp);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 5326
diff changeset
  4482
	error = zfs_setacl(zp, vsecp, skipaclchk, cr);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4483
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4484
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4485
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4486
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4487
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4488
 * Predeclare these here so that the compiler assumes that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4489
 * this is an "old style" function declaration that does
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4490
 * not include arguments => we won't get type mismatch errors
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4491
 * in the initializations that follow.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4492
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4493
static int zfs_inval();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4494
static int zfs_isdir();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4495
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4496
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4497
zfs_inval()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4498
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4499
	return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4500
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4501
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4502
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4503
zfs_isdir()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4504
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4505
	return (EISDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4506
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4507
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4508
 * Directory vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4509
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4510
vnodeops_t *zfs_dvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4511
const fs_operation_def_t zfs_dvnodeops_template[] = {
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4512
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4513
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4514
	VOPNAME_READ,		{ .error = zfs_isdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4515
	VOPNAME_WRITE,		{ .error = zfs_isdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4516
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4517
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4518
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4519
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4520
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4521
	VOPNAME_CREATE,		{ .vop_create = zfs_create },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4522
	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4523
	VOPNAME_LINK,		{ .vop_link = zfs_link },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4524
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4525
	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4526
	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4527
	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4528
	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4529
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4530
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4531
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4532
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4533
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4534
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4535
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  4536
	VOPNAME_VNEVENT, 	{ .vop_vnevent = fs_vnevent_support },
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4537
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4538
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4539
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4540
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4541
 * Regular file vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4542
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4543
vnodeops_t *zfs_fvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4544
const fs_operation_def_t zfs_fvnodeops_template[] = {
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4545
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4546
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4547
	VOPNAME_READ,		{ .vop_read = zfs_read },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4548
	VOPNAME_WRITE,		{ .vop_write = zfs_write },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4549
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4550
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4551
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4552
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4553
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4554
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4555
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4556
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4557
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4558
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4559
	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4560
	VOPNAME_SPACE,		{ .vop_space = zfs_space },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4561
	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4562
	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4563
	VOPNAME_MAP,		{ .vop_map = zfs_map },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4564
	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4565
	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4566
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4567
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4568
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4569
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4570
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4571
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4572
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4573
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4574
 * Symbolic link vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4575
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4576
vnodeops_t *zfs_symvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4577
const fs_operation_def_t zfs_symvnodeops_template[] = {
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4578
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4579
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4580
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4581
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4582
	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4583
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4584
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4585
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4586
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4587
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4588
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4589
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4590
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4591
 * Extended attribute directory vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4592
 *	This template is identical to the directory vnodes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4593
 *	operation template except for restricted operations:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4594
 *		VOP_MKDIR()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4595
 *		VOP_SYMLINK()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4596
 * Note that there are other restrictions embedded in:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4597
 *	zfs_create()	- restrict type to VREG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4598
 *	zfs_link()	- no links into/out of attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4599
 *	zfs_rename()	- no moves into/out of attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4600
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4601
vnodeops_t *zfs_xdvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4602
const fs_operation_def_t zfs_xdvnodeops_template[] = {
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4603
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4604
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4605
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4606
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4607
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4608
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4609
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4610
	VOPNAME_CREATE,		{ .vop_create = zfs_create },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4611
	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4612
	VOPNAME_LINK,		{ .vop_link = zfs_link },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4613
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4614
	VOPNAME_MKDIR,		{ .error = zfs_inval },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4615
	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4616
	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4617
	VOPNAME_SYMLINK,	{ .error = zfs_inval },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4618
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4619
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4620
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4621
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4622
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4623
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4624
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4625
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4626
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4627
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4628
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4629
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4630
 * Error vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4631
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4632
vnodeops_t *zfs_evnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4633
const fs_operation_def_t zfs_evnodeops_template[] = {
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4634
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4635
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  4636
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  4637
};