usr/src/uts/common/fs/zfs/zfs_vnops.c
author ek110237
Wed, 24 Oct 2007 16:54:46 -0700
changeset 5326 6752aa2bd5bc
parent 4863 7b14ad153d91
child 5331 3047ad28a67b
permissions -rw-r--r--
6425096 want online 'zfs recv' (read only and read/write) 6597182 .zfs/snapshot code could use a little more comments
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
/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
    22
 * Copyright 2007 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
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/types.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/param.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/time.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/systm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/sysmacros.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/resource.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/vfs.h>
3898
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
    37
#include <sys/vfs_opreg.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/vnode.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/file.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <sys/kmem.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <sys/taskq.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <sys/uio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
#include <sys/vmsystm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
#include <sys/atomic.h>
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
    46
#include <sys/vm.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include <vm/seg_vn.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
#include <vm/pvn.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
#include <vm/as.h>
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_vfsops.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
#include <sys/zfs_dir.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
#include <sys/zfs_acl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
#include <sys/zfs_ioctl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
#include <sys/fs/zfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
#include <sys/txg.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
#include <sys/dbuf.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
#include <sys/zap.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
#include <sys/dirent.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
#include <sys/policy.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
#include <sys/sunddi.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
#include <sys/filio.h>
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>
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1472
diff changeset
    71
#include <sys/dnlc.h>
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
    72
#include <sys/zfs_rlock.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
 * Programming rules.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
 * Each vnode op performs some logical unit of work.  To do this, the ZPL must
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
 * properly lock its in-core state, create a DMU transaction, do the work,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
 * record this work in the intent log (ZIL), commit the DMU transaction,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
 * and wait the the intent log to commit if it's is a synchronous operation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
 * Morover, the vnode ops must work in both normal and log replay context.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
 * The ordering of events is important to avoid deadlocks and references
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
 * to freed memory.  The example below illustrates the following Big Rules:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
 *  (1) A check must be made in each zfs thread for a mounted file system.
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
    86
 *	This is done avoiding races using ZFS_ENTER(zfsvfs) or
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
    87
 *      ZFS_ENTER_VERIFY(zfsvfs, zp).  A ZFS_EXIT(zfsvfs) is needed before
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
    88
 *      all returns.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
 *  (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
    91
 *	(if necessary) and ZFS_EXIT(). This is for 3 reasons:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
 *	First, if it's the last reference, the vnode/znode
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
 *	can be freed, so the zp may point to freed memory.  Second, the last
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
 *	reference will call zfs_zinactive(), which may induce a lot of work --
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
    95
 *	pushing cached pages (which acquires range locks) and syncing out
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
 *	cached atime changes.  Third, zfs_zinactive() may require a new tx,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
 *	which could deadlock the system if you were already holding one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
 *
1757
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
    99
 *  (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
   100
 *	as they can span dmu_tx_assign() calls.
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   101
 *
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   102
 *  (4)	Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
 *	In normal operation, this will be TXG_NOWAIT.  During ZIL replay,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
 *	it will be a specific txg.  Either way, dmu_tx_assign() never blocks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
 *	This is critical because we don't want to block while holding locks.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
 *	Note, in particular, that if a lock is sometimes acquired before
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
 *	the tx assigns, and sometimes after (e.g. z_lock), then failing to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
 *	use a non-blocking assign can deadlock the system.  The scenario:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
 *	Thread A has grabbed a lock before calling dmu_tx_assign().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
 *	Thread B is in an already-assigned tx, and blocks for this lock.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
 *	Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
 *	forever, because the previous txg can't quiesce until B's tx commits.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
 *	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
   116
 *	then drop all locks, call dmu_tx_wait(), and try again.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
 *
1757
fe91f3ab3083 6405008 Reboot hung after a stress test
perrin
parents: 1669
diff changeset
   118
 *  (5)	If the operation succeeded, generate the intent log entry for it
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
 *	before dropping locks.  This ensures that the ordering of events
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
 *	in the intent log matches the order in which they actually occurred.
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
 *  (6)	At the end of each vnode op, the DMU tx must always commit,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
 *	regardless of whether there were any errors.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
 *
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   125
 *  (7)	After dropping all locks, invoke zil_commit(zilog, seq, foid)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
 *	to ensure that synchronous semantics are provided when necessary.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
 * In general, this is how things should be ordered in each vnode op:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
 *	ZFS_ENTER(zfsvfs);		// exit if unmounted
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
 * top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
 *	zfs_dirent_lock(&dl, ...)	// lock directory entry (may VN_HOLD())
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
 *	rw_enter(...);			// grab any other locks you need
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
 *	tx = dmu_tx_create(...);	// get DMU tx
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
 *	dmu_tx_hold_*();		// hold each object you might modify
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
 *	error = dmu_tx_assign(tx, zfsvfs->z_assign);	// try to assign
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
 *	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
 *		rw_exit(...);		// drop locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
 *		zfs_dirent_unlock(dl);	// unlock directory entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
 *		VN_RELE(...);		// release held vnodes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
 *		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   142
 *			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   143
 *			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
 *			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
 *		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   146
 *		dmu_tx_abort(tx);	// abort DMU tx
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
 *		ZFS_EXIT(zfsvfs);	// finished in zfs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
 *		return (error);		// really out of space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
 *	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
 *	error = do_real_work();		// do whatever this VOP does
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
 *	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
   152
 *		zfs_log_*(...);		// on success, make ZIL entry
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
 *	dmu_tx_commit(tx);		// commit DMU tx -- error or not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
 *	rw_exit(...);			// drop locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
 *	zfs_dirent_unlock(dl);		// unlock directory entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
 *	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
   157
 *	zil_commit(zilog, seq, foid);	// synchronous when necessary
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
 *	ZFS_EXIT(zfsvfs);		// finished in zfs
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
 *	return (error);			// done, report error
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
zfs_open(vnode_t **vpp, int flag, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   165
	znode_t	*zp = VTOZ(*vpp);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   166
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   167
	/* Keep a count of the synchronous opens in the znode */
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   168
	if (flag & (FSYNC | FDSYNC))
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   169
		atomic_inc_32(&zp->z_sync_cnt);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   177
	znode_t	*zp = VTOZ(vp);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   178
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   179
	/* Decrement the synchronous opens in the znode */
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   180
	if ((flag & (FSYNC | FDSYNC)) && (count == 1))
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   181
		atomic_dec_32(&zp->z_sync_cnt);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   182
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
	 * Clean up any locks held by this process on the vp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
	cleanlocks(vp, ddi_get_pid(), 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
	cleanshares(vp, ddi_get_pid());
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
zfs_holey(vnode_t *vp, int cmd, offset_t *off)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
	uint64_t noff = (uint64_t)*off; /* new offset */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
	uint64_t file_sz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
	boolean_t hole;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
	file_sz = zp->z_phys->zp_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
	if (noff >= file_sz)  {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
	if (cmd == _FIO_SEEK_HOLE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
		hole = B_TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
		hole = B_FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
	error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
	/* end of file? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
	if ((error == ESRCH) || (noff > file_sz)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
		 * Handle the virtual hole at the end of file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
		if (hole) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
			*off = file_sz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
	if (noff < *off)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
	*off = noff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
    int *rvalp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
	offset_t off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
	zfsvfs_t *zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   243
	znode_t *zp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
	switch (com) {
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   246
	case _FIOFFS:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
		return (zfs_sync(vp->v_vfsp, 0, cred));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   249
		/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   250
		 * The following two ioctls are used by bfu.  Faking out,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   251
		 * necessary to avoid bfu errors.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   252
		 */
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   253
	case _FIOGDIO:
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   254
	case _FIOSDIO:
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   255
		return (0);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   256
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   257
	case _FIO_SEEK_DATA:
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   258
	case _FIO_SEEK_HOLE:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
		if (ddi_copyin((void *)data, &off, sizeof (off), flag))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
			return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   262
		zp = VTOZ(vp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   263
		zfsvfs = zp->z_zfsvfs;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   264
		ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
		/* offset parameter is in/out */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
		error = zfs_holey(vp, com, &off);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
		if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
			return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
	return (ENOTTY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
 * When a file is memory mapped, we must keep the IO data synchronized
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
 * between the DMU cache and the memory mapped pages.  What this means:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
 * On Write:	If we find a memory mapped page, we write to *both*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
 *		the page and the dmu buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
 *	the file is memory mapped.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
static int
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   289
mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
	int64_t	start, off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
	int len = nbytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
	start = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
	off = start & PAGEOFFSET;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
		page_t *pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
		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
   302
		uint64_t woff = uio->uio_loffset;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
		 * We don't want a new page to "appear" in the middle of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
		 * the file update (because it may not get the write
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
		 * update data), so we grab a lock to block
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
		 * zfs_getpage().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
		rw_enter(&zp->z_map_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
		if (pp = page_lookup(vp, start, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
			caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
			rw_exit(&zp->z_map_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
			va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
			error = uiomove(va+off, bytes, UIO_WRITE, uio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
			if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
				dmu_write(zfsvfs->z_os, zp->z_id,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
				    woff, bytes, va+off, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
			ppmapout(va);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
			page_unlock(pp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
			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
   325
			    uio, bytes, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
			rw_exit(&zp->z_map_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
		len -= bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
		off = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
 * When a file is memory mapped, we must keep the IO data synchronized
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
 * between the DMU cache and the memory mapped pages.  What this means:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
 * On Read:	We "read" preferentially from memory mapped pages,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
 *		else we default from the dmu buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
 *	the file is memory mapped.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
static int
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   347
mappedread(vnode_t *vp, int nbytes, uio_t *uio)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
{
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   349
	znode_t *zp = VTOZ(vp);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   350
	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
   351
	int64_t	start, off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
	int len = nbytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
	start = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
	off = start & PAGEOFFSET;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
	for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
		page_t *pp;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   359
		uint64_t bytes = MIN(PAGESIZE - off, len);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   360
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
		if (pp = page_lookup(vp, start, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
			caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
   364
			va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
			error = uiomove(va + off, bytes, UIO_READ, uio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
			ppmapout(va);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
			page_unlock(pp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
		} else {
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   369
			error = dmu_read_uio(os, zp->z_id, uio, bytes);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
		len -= bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
		off = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
		if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   379
offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
 * Read bytes from specified file into supplied buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
 *	IN:	vp	- vnode of file to be read from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
 *		uio	- structure supplying read location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
 *			  and return buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
 *		ioflag	- SYNC flags; used to provide FRSYNC semantics.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
 *	OUT:	uio	- updated offset and range, buffer filled.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
 * Side Effects:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
 *	vp - atime updated if byte count > 0
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
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
   401
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   404
	objset_t	*os;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   405
	ssize_t		n, nbytes;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   406
	int		error;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   407
	rl_t		*rl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   409
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   410
	os = zfsvfs->z_os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
	 * Validate file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
	if (uio->uio_loffset < (offset_t)0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
	 * Fasttrack empty reads
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
	if (uio->uio_resid == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   429
	 * Check for mandatory locks
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
		if (error = chklock(vp, FREAD,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
		    uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
	 * If we're in FRSYNC mode, sync out this znode before reading it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
	 */
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   442
	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
   443
		zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   446
	 * Lock the range against changes.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   448
	rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   449
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
	 * If we are reading past end-of-file we can skip
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
	 * to the end; but we might still need to set atime.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
	if (uio->uio_loffset >= zp->z_phys->zp_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   459
	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
   460
	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
   461
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   462
	while (n > 0) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   463
		nbytes = MIN(n, zfs_read_chunk_size -
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   464
		    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
   465
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   466
		if (vn_has_cached_data(vp))
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   467
			error = mappedread(vp, nbytes, uio);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   468
		else
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   469
			error = dmu_read_uio(os, zp->z_id, uio, nbytes);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
   470
		if (error)
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   471
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   472
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   473
		n -= nbytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
	}
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   475
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   477
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
 * Fault in the pages of the first n bytes specified by the uio structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
 * 1 byte in each page is touched and the uio struct is unmodified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
 * Any error will exit this routine as this is only a best
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
 * attempt to get the pages resident. This is a copy of ufs_trans_touch().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
zfs_prefault_write(ssize_t n, struct uio *uio)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
	struct iovec *iov;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
	ulong_t cnt, incr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
	caddr_t p;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
	uint8_t tmp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
	iov = uio->uio_iov;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
	while (n) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
		cnt = MIN(iov->iov_len, n);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
		if (cnt == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
			/* empty iov entry */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
			iov++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
		n -= cnt;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
		 * touch each page in this segment.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
		p = iov->iov_base;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
		while (cnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
			switch (uio->uio_segflg) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
			case UIO_USERSPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
			case UIO_USERISPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
				if (fuword8(p, &tmp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
					return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
			case UIO_SYSSPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
				if (kcopy(p, &tmp, 1))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
					return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
				break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
			incr = MIN(cnt, PAGESIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
			p += incr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
			cnt -= incr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
		 * touch the last byte in case it straddles a page.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
		p--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
		switch (uio->uio_segflg) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
		case UIO_USERSPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
		case UIO_USERISPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
			if (fuword8(p, &tmp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
				return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
		case UIO_SYSSPACE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
			if (kcopy(p, &tmp, 1))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
				return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
		iov++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
 * Write the bytes to a file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
 *	IN:	vp	- vnode of file to be written to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
 *		uio	- structure supplying write location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
 *			  and data buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
 *		ioflag	- FAPPEND flag set if in append mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
 *	OUT:	uio	- updated offset and range.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
 *	vp - ctime|mtime updated if byte count > 0
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
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
   567
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
	rlim64_t	limit = uio->uio_llimit;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
	ssize_t		start_resid = uio->uio_resid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
	ssize_t		tx_bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
	uint64_t	end_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   575
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
	offset_t	woff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
	ssize_t		n, nbytes;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   578
	rl_t		*rl;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
	int		max_blksz = zfsvfs->z_max_blksz;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   580
	int		error;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
	 * Fasttrack empty write
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   585
	n = start_resid;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
	if (n == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   589
	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   590
		limit = MAXOFFSET_T;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   591
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   592
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   593
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
	/*
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   596
	 * Pre-fault the pages to ensure slow (eg NFS) pages
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   597
	 * don't hold up txg.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	 */
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   599
	zfs_prefault_write(n, uio);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
	 * If in append mode, set the io offset pointer to eof.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   604
	if (ioflag & FAPPEND) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   605
		/*
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   606
		 * Range lock for a file append:
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   607
		 * The value for the start of range will be determined by
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   608
		 * zfs_range_lock() (to guarantee append semantics).
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   609
		 * If this write will cause the block size to increase,
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   610
		 * zfs_range_lock() will lock the entire file, so we must
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   611
		 * later reduce the range after we grow the block size.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   612
		 */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   613
		rl = zfs_range_lock(zp, 0, n, RL_APPEND);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   614
		if (rl->r_len == UINT64_MAX) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   615
			/* overlocked, zp_size can't change */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   616
			woff = uio->uio_loffset = zp->z_phys->zp_size;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   617
		} else {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   618
			woff = uio->uio_loffset = rl->r_off;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   619
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
		woff = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
		 * Validate file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
		if (woff < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
		/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   631
		 * If we need to grow the block size then zfs_range_lock()
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   632
		 * will lock a wider range than we request here.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   633
		 * Later after growing the block size we reduce the range.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
		 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   635
		rl = zfs_range_lock(zp, woff, n, RL_WRITER);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	if (woff >= limit) {
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   639
		zfs_range_unlock(rl);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   640
		ZFS_EXIT(zfsvfs);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   641
		return (EFBIG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
	if ((woff + n) > limit || woff > (limit - n))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
		n = limit - woff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   648
	 * Check for mandatory locks
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
	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
   651
	    (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
   652
		zfs_range_unlock(rl);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   653
		ZFS_EXIT(zfsvfs);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   654
		return (error);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   655
	}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   656
	end_size = MAX(zp->z_phys->zp_size, woff + n);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   658
	/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   659
	 * 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
   660
	 * 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
   661
	 * and allows us to do more fine-grained space accounting.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
	while (n > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
		/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   665
		 * Start a transaction.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
		woff = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
		tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
		dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
		dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
		error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
			if (error == ERESTART &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
			    zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   675
				dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   676
				dmu_tx_abort(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   677
				continue;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
			}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
   679
			dmu_tx_abort(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   680
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   681
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   682
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   683
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   684
		 * 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
   685
		 * 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
   686
		 * 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
   687
		 * 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
   688
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   689
		if (rl->r_len == UINT64_MAX) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   690
			uint64_t new_blksz;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   691
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   692
			if (zp->z_blksz > max_blksz) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   693
				ASSERT(!ISP2(zp->z_blksz));
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   694
				new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   695
			} else {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   696
				new_blksz = MIN(end_size, max_blksz);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   697
			}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   698
			zfs_grow_blocksize(zp, new_blksz, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   699
			zfs_range_reduce(rl, woff, n);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   700
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   701
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   702
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   703
		 * 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
   704
		 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   705
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   706
		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
   707
		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
   708
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   709
		tx_bytes = uio->uio_resid;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   710
		if (vn_has_cached_data(vp)) {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   711
			rw_exit(&zp->z_map_lock);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   712
			error = mappedwrite(vp, nbytes, uio, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   713
		} else {
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   714
			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
   715
			    uio, nbytes, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   716
			rw_exit(&zp->z_map_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
		}
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   718
		tx_bytes -= uio->uio_resid;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   719
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 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
   722
		 * 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
   723
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   724
		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
   725
			dmu_tx_commit(tx);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   726
			ASSERT(error != 0);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   727
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   728
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   729
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
		/*
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   731
		 * 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
   732
		 * 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
   733
		 *
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   734
		 * 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
   735
		 * 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
   736
		 * to another app after the partial write is committed.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
		 */
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   738
		mutex_enter(&zp->z_acl_lock);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   739
		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
   740
		    (S_IXUSR >> 6))) != 0 &&
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   741
		    (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
   742
		    secpolicy_vnode_setid_retain(cr,
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   743
		    (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
   744
		    zp->z_phys->zp_uid == 0) != 0) {
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
   745
			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
   746
		}
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   747
		mutex_exit(&zp->z_acl_lock);
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
		 * 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
   751
		 * 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
   752
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   753
		zfs_time_stamper(zp, CONTENT_MODIFIED, tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   754
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   755
		/*
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   756
		 * 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
   757
		 * account for possible concurrent updates.
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   758
		 */
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   759
		while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
			(void) atomic_cas_64(&zp->z_phys->zp_size, end_size,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
			    uio->uio_loffset);
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   762
		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
   763
		dmu_tx_commit(tx);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   764
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   765
		if (error != 0)
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   766
			break;
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   767
		ASSERT(tx_bytes == nbytes);
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   768
		n -= nbytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   771
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	 * If we're in replay mode, or we made no progress, return error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
	 * Otherwise, it's at least a partial write, so it's successful.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
   782
	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
   783
		zil_commit(zilog, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   789
void
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   790
zfs_get_done(dmu_buf_t *db, void *vzgd)
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   791
{
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   792
	zgd_t *zgd = (zgd_t *)vzgd;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   793
	rl_t *rl = zgd->zgd_rl;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   794
	vnode_t *vp = ZTOV(rl->r_zp);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   795
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   796
	dmu_buf_rele(db, vzgd);
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   797
	zfs_range_unlock(rl);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   798
	VN_RELE(vp);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   799
	zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp)));
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   800
	kmem_free(zgd, sizeof (zgd_t));
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   801
}
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   802
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
 * Get data to generate a TX_WRITE intent log record.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
int
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   807
zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
	zfsvfs_t *zfsvfs = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
	objset_t *os = zfsvfs->z_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
	znode_t *zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	uint64_t off = lr->lr_offset;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   813
	dmu_buf_t *db;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   814
	rl_t *rl;
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   815
	zgd_t *zgd;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
   816
	int dlen = lr->lr_length;		/* length of user data */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   819
	ASSERT(zio);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
	ASSERT(dlen != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
	/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   823
	 * Nothing to do if the file has been removed
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
	if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
		return (ENOENT);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   827
	if (zp->z_unlinked) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
		VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
		return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   831
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
	 * Write records come in two flavors: immediate and indirect.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
	 * For small writes it's cheaper to store the data with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
	 * log record (immediate); for large writes it's cheaper to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
	 * sync the data and get a pointer to it (indirect) so that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
	 * we don't have to write the data twice.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   839
	if (buf != NULL) { /* immediate write */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   840
		rl = zfs_range_lock(zp, off, dlen, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   841
		/* test for truncation needs to be done while range locked */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   842
		if (off >= zp->z_phys->zp_size) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   843
			error = ENOENT;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   844
			goto out;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   845
		}
2449
6adc99f24a96 6452420 zfs_get_data() of page data panics when blocksize is less than pagesize
maybee
parents: 2391
diff changeset
   846
		VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf));
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   847
	} else { /* indirect write */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   848
		uint64_t boff; /* block starting offset */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   849
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
		/*
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   851
		 * Have to lock the whole block to ensure when it's
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   852
		 * written out and it's checksum is being calculated
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   853
		 * that no one can change the data. We need to re-check
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   854
		 * blocksize after we get the lock in case it's changed!
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
		 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   856
		for (;;) {
1941
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   857
			if (ISP2(zp->z_blksz)) {
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   858
				boff = P2ALIGN_TYPED(off, zp->z_blksz,
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   859
				    uint64_t);
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   860
			} else {
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   861
				boff = 0;
1843b345392c 6422565 Data corruption possible with range lock for dmu_sync()
perrin
parents: 1878
diff changeset
   862
			}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   863
			dlen = zp->z_blksz;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   864
			rl = zfs_range_lock(zp, boff, dlen, RL_READER);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   865
			if (zp->z_blksz == dlen)
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   866
				break;
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   867
			zfs_range_unlock(rl);
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   868
		}
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   869
		/* test for truncation needs to be done while range locked */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   870
		if (off >= zp->z_phys->zp_size) {
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   871
			error = ENOENT;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   872
			goto out;
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   873
		}
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   874
		zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   875
		zgd->zgd_rl = rl;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   876
		zgd->zgd_zilog = zfsvfs->z_log;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   877
		zgd->zgd_bp = &lr->lr_blkptr;
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   878
		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
   879
		ASSERT(boff == db->db_offset);
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   880
		lr->lr_blkoff = off - boff;
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   881
		error = dmu_sync(zio, db, &lr->lr_blkptr,
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   882
		    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
   883
		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
   884
		    lr->lr_length <= zp->z_blksz);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   885
		if (error == 0) {
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   886
			zil_add_vdev(zfsvfs->z_log,
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   887
			    DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr)));
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   888
		}
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   889
		/*
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   890
		 * If we get EINPROGRESS, then we need to wait for a
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   891
		 * 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
   892
		 * we can release this dbuf.  We will finish everything
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   893
		 * up in the zfs_get_done() callback.
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   894
		 */
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   895
		if (error == EINPROGRESS)
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   896
			return (0);
3063
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   897
		dmu_buf_rele(db, zgd);
b252896b372b 6341569 zio_alloc_blk() vdev distribution performs badly
perrin
parents: 2796
diff changeset
   898
		kmem_free(zgd, sizeof (zgd_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
	}
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
   900
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
   901
	zfs_range_unlock(rl);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
	VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   903
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   904
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   905
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   906
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   907
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   908
zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   909
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   910
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   911
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   912
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   914
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
	error = zfs_zaccess_rwx(zp, mode, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   916
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   917
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   918
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   919
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   920
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
 * Lookup an entry in a directory, or an extended attribute directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   922
 * If it exists, return a held vnode reference for it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   923
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
 *	IN:	dvp	- vnode of directory to search.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   925
 *		nm	- name of entry to lookup.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
 *		pnp	- full pathname to lookup [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
 *		flags	- LOOKUP_XATTR set if looking for an attribute.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
 *		rdir	- root directory vnode [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
 *	OUT:	vpp	- vnode of located entry, NULL if not found.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
 *	NA
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
    int flags, vnode_t *rdir, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   945
	znode_t *zdp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   946
	zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   947
	int	error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
   949
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zdp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   950
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
	*vpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
	if (flags & LOOKUP_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
		/*
3234
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   955
		 * If the xattr property is off, refuse the lookup request.
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   956
		 */
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   957
		if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   958
			ZFS_EXIT(zfsvfs);
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   959
			return (EINVAL);
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   960
		}
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   961
28b36f7bbd7e PSARC/2006/638 noxattr ZFS property
ck153898
parents: 3131
diff changeset
   962
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
		 * We don't allow recursive attributes..
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
		 * Maybe someday we will.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   965
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   966
		if (zdp->z_phys->zp_flags & ZFS_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   968
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   970
3280
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 3271
diff changeset
   971
		if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   977
		 * Do we have permission to get into attribute directory?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
		if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
			VN_RELE(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
   988
	if (dvp->v_type != VDIR) {
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
   989
		ZFS_EXIT(zfsvfs);
1460
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
   990
		return (ENOTDIR);
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
   991
	}
1460
299d0ee75ed4 6371285 panic when nfs lookup operation attempted on plain file
marks
parents: 1417
diff changeset
   992
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
	 * Check accessibility of directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
	if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   998
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
	if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1003
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1004
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1005
		 * Convert device special files
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1006
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
		if (IS_DEVVP(*vpp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
			vnode_t	*svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
			VN_RELE(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
			if (svp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
				error = ENOSYS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
				*vpp = svp;
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
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
 * Attempt to create a new entry in a directory.  If the entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
 * already exists, truncate the file if permissible, else return
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
 * an error.  Return the vp of the created or trunc'd file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
 *	IN:	dvp	- vnode of directory to put new file entry in.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
 *		name	- name of new file entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
 *		vap	- attributes of new file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1031
 *		excl	- flag indicating exclusive or non-exclusive mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
 *		mode	- mode to open file with.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
 *		flag	- large file flag [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
 *	OUT:	vpp	- vnode of created or trunc'd entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
 *	dvp - ctime|mtime updated if new entry created
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1043
 *	 vp - ctime|mtime always, atime if new
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1044
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1045
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1047
zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1048
    int mode, vnode_t **vpp, cred_t *cr, int flag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1052
	zilog_t		*zilog;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1053
	objset_t	*os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1057
	uint64_t	zoid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1058
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1059
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1060
	os = zfsvfs->z_os;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1061
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
	*vpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
	if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
		vap->va_mode &= ~VSVTX;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
	if (*name == '\0') {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
		 * Null component name refers to the directory itself.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
		VN_HOLD(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
		zp = dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
		dl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1077
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1078
		/* possible VN_HOLD(zp) */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
		if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
			if (strcmp(name, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
				error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1085
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
	zoid = zp ? zp->z_id : -1ULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
	if (zp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1090
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
		 * Create a new file object and update the directory
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1092
		 * to reference it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1094
		if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1096
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1099
		 * We only support the creation of regular files in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1100
		 * extended attribute directories.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
		if ((dzp->z_phys->zp_flags & ZFS_XATTR) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
		    (vap->va_type != VREG)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
			error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1108
		tx = dmu_tx_create(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1110
		dmu_tx_hold_bonus(tx, dzp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1111
		dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1112
		if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
			    0, SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
		error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1116
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1117
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1118
			if (error == ERESTART &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1119
			    zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1120
				dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1121
				dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
				goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
			}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1124
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1125
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1126
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1127
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1128
		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1129
		ASSERT(zp->z_id == zoid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1130
		(void) zfs_link_create(dl, zp, tx, ZNEW);
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  1131
		zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1132
		dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1133
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
		 * A directory entry already exists for this name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
		 * Can't truncate an existing file if in exclusive mode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
		if (excl == EXCL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
			error = EEXIST;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
		 * Can't open a directory for writing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1146
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
		if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
			error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1149
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
		 * Verify requested access to file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
		if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
		mutex_enter(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
		dzp->z_seq++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
		mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1162
		/*
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1163
		 * Truncate regular files if requested.
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1164
		 */
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1165
		if ((ZTOV(zp)->v_type == VREG) &&
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
		    (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1167
			error = zfs_freesp(zp, 0, 0, mode, TRUE);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1168
			if (error == ERESTART &&
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1169
			    zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1170
				/* NB: we already did dmu_tx_wait() */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1171
				zfs_dirent_unlock(dl);
2365
2745d773b6bd 6447452 re-creating zfs files can lead to failure to unmount
perrin
parents: 2237
diff changeset
  1172
				VN_RELE(ZTOV(zp));
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1173
				goto top;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
			}
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1175
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1176
			if (error == 0) {
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1177
				vnevent_create(ZTOV(zp));
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1178
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
	if (dl)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1187
		if (zp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
			VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
		*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
		 * If vnode is for a device return a specfs vnode instead.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
		if (IS_DEVVP(*vpp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
			struct vnode *svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
			svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
			VN_RELE(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
			if (svp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
				error = ENOSYS;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
			*vpp = svp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1210
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
 * Remove an entry from a directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1213
 *	IN:	dvp	- vnode of directory to remove entry from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1214
 *		name	- name of entry to remove.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1215
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1217
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1218
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1219
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1220
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1221
 *	dvp - ctime|mtime
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1222
 *	 vp - ctime (if nlink > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1223
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1224
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1225
zfs_remove(vnode_t *dvp, char *name, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1226
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1227
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1228
	znode_t		*xzp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1229
	vnode_t		*vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1230
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1231
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
	uint64_t	acl_obj, xattr_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1234
	dmu_tx_t	*tx;
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1235
	boolean_t	may_delete_now, delete_now = FALSE;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1236
	boolean_t	unlinked;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1239
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1240
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1244
	 * Attempt to lock directory; fail if entry doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1249
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1250
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1251
	vp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1252
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1253
	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1254
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1255
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1256
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1257
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1258
	 * Need to use rmdir for removing directories.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1260
	if (vp->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1261
		error = EPERM;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1262
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1263
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1264
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1265
	vnevent_remove(vp, dvp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1266
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1472
diff changeset
  1267
	dnlc_remove(dvp, name);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1472
diff changeset
  1268
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1269
	mutex_enter(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
	may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
	mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1272
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1273
	/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1274
	 * 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
  1275
	 * it depends on whether we're the last link, and on whether there are
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1276
	 * other holds on the vnode.  So we dmu_tx_hold() the right things to
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1277
	 * allow for either case.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1279
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1280
	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1281
	dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1282
	if (may_delete_now)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1283
		dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1285
	/* are there any extended attributes? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1286
	if ((xattr_obj = zp->z_phys->zp_xattr) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1287
		/* XXX - do we need this if we are deleting? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
		dmu_tx_hold_bonus(tx, xattr_obj);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1289
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1290
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1291
	/* are there any additional acls */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1292
	if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1293
	    may_delete_now)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1294
		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1295
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1296
	/* 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
  1297
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1298
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1299
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1300
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1301
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1302
		VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1303
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1304
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1305
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1306
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1307
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1308
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1309
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1310
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1311
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1312
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1313
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1314
	 * Remove the directory entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1315
	 */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1316
	error = zfs_link_destroy(dl, zp, tx, 0, &unlinked);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1318
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1319
		dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1320
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1321
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1322
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1323
	if (unlinked) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1324
		mutex_enter(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1325
		delete_now = may_delete_now &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
		    vp->v_count == 1 && !vn_has_cached_data(vp) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
		    zp->z_phys->zp_xattr == xattr_obj &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1328
		    zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
		mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1331
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
	if (delete_now) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
		if (zp->z_phys->zp_xattr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1334
			error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1335
			ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1336
			ASSERT3U(xzp->z_phys->zp_links, ==, 2);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1337
			dmu_buf_will_dirty(xzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1338
			mutex_enter(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1339
			xzp->z_unlinked = 1;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1340
			xzp->z_phys->zp_links = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1341
			mutex_exit(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1342
			zfs_unlinked_add(xzp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
			zp->z_phys->zp_xattr = 0; /* probably unnecessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1344
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1345
		mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1346
		mutex_enter(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1347
		vp->v_count--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1348
		ASSERT3U(vp->v_count, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1349
		mutex_exit(&vp->v_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
		mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1351
		zfs_znode_delete(zp, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
		VFS_RELE(zfsvfs->z_vfs);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1353
	} else if (unlinked) {
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1354
		zfs_unlinked_add(zp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1355
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1356
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  1357
	zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1358
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1359
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1360
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1361
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1362
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
	if (!delete_now) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1364
		VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1365
	} else if (xzp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1366
		/* this rele delayed to prevent nesting transactions */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1367
		VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1368
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1369
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1370
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1371
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1372
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1373
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1374
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1375
 * Create a new directory and insert it into dvp using the name
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1376
 * provided.  Return a pointer to the inserted directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1377
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1378
 *	IN:	dvp	- vnode of directory to add subdir to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1379
 *		dirname	- name of new directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1380
 *		vap	- attributes of new directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1381
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1382
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1383
 *	OUT:	vpp	- vnode of created directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1384
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1385
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1386
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1387
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1388
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1389
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1390
 *	 vp - ctime|mtime|atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1391
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1392
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1393
zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1394
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1395
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1396
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1397
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1398
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1399
	uint64_t	zoid = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1400
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1401
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
	ASSERT(vap->va_type == VDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1405
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1406
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1407
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1408
	if (dzp->z_phys->zp_flags & ZFS_XATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1409
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1410
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1411
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1412
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1413
	*vpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1414
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1415
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1416
	 * First make sure the new directory doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1417
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1418
	if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1419
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1420
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1421
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1422
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
  1423
	if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) {
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1424
		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
  1425
		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
  1426
		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
  1427
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  1428
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1429
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1430
	 * Add a new entry to the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1431
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1432
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1433
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1434
	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1435
	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1436
		dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1437
		    0, SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1438
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1439
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1440
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1442
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1443
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1444
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1445
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1446
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1447
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1448
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1449
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1450
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1451
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1452
	 * Create new node.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1453
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1454
	zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1455
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1456
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1457
	 * Now put new name in parent dir.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1458
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1459
	(void) zfs_link_create(dl, zp, tx, ZNEW);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1460
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1461
	*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1462
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  1463
	zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1464
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1465
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1466
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1467
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1468
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1469
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1470
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1471
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1472
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1473
 * Remove a directory subdir entry.  If the current working
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1474
 * directory is the same as the subdir to be removed, the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1475
 * remove will fail.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1476
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
 *	IN:	dvp	- vnode of directory to remove from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1478
 *		name	- name of directory to be removed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1479
 *		cwd	- vnode of current working directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1480
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1481
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1482
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1483
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1484
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1485
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1486
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1487
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1488
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1489
zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1490
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1491
	znode_t		*dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1492
	znode_t		*zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
	vnode_t		*vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1495
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1500
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1501
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1503
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1504
	zp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1505
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1506
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1507
	 * Attempt to lock directory; fail if entry doesn't exist.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1508
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1509
	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1510
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1511
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1512
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1513
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1514
	vp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1515
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1516
	if (error = zfs_zaccess_delete(dzp, zp, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1517
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1518
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1519
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1520
	if (vp->v_type != VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1521
		error = ENOTDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1522
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1523
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1524
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
	if (vp == cwd) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
		error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1529
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  1530
	vnevent_rmdir(vp, dvp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1531
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1532
	/*
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1533
	 * 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
  1534
	 * 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
  1535
	 */
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1536
	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
  1537
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1538
	/*
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3638
diff changeset
  1539
	 * Grab a lock on the parent pointer to make sure we play well
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1540
	 * with the treewalk and directory rename code.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1541
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1542
	rw_enter(&zp->z_parent_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1543
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
	tx = dmu_tx_create(zfsvfs->z_os);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  1545
	dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1546
	dmu_tx_hold_bonus(tx, zp->z_id);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1547
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
		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
  1551
		rw_exit(&zp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1552
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1553
		VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1555
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1556
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1559
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1563
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1564
	error = zfs_link_destroy(dl, zp, tx, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1566
	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
  1567
		zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1570
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
	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
  1572
	rw_exit(&zp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1573
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1575
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1576
	VN_RELE(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1577
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1578
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1579
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1580
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1582
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1583
 * Read as many directory entries as will fit into the provided
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1584
 * buffer from the given directory cursor position (specified in
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1585
 * the uio structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1586
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1587
 *	IN:	vp	- vnode of directory to read.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1588
 *		uio	- structure supplying read location, range info,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1589
 *			  and return buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1590
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1591
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1592
 *	OUT:	uio	- updated offset and range, buffer filled.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1593
 *		eofp	- set to true if end-of-file detected.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1594
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1595
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1596
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1597
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1598
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1599
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1600
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1601
 * Note that the low 4 bits of the cookie returned by zap is always zero.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1602
 * This allows us to use the low range for "special" directory entries:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1603
 * We use 0 for '.', and 1 for '..'.  If this is the root of the filesystem,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1604
 * we use the offset 2 for the '.zfs' directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1605
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1608
zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1609
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1610
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1611
	iovec_t		*iovp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1612
	dirent64_t	*odp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1613
	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
  1614
	objset_t	*os;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
	caddr_t		outbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
	size_t		bufsize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1617
	zap_cursor_t	zc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1618
	zap_attribute_t	zap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1619
	uint_t		bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
	uint64_t	offset; /* must be unsigned; checks for < 1 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
	int		local_eof;
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1622
	int		outcount;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1623
	int		error;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1624
	uint8_t		prefetch;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1626
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1627
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1628
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1629
	 * If we are not given an eof variable,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1630
	 * use a local one.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1631
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1632
	if (eofp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1633
		eofp = &local_eof;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1634
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1636
	 * Check for valid iov_len.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1637
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1638
	if (uio->uio_iov->iov_len <= 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1642
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1643
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1644
	 * Quit if directory has been removed (posix)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1645
	 */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  1646
	if ((*eofp = zp->z_unlinked) != 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1647
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1648
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1649
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1650
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1651
	error = 0;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1652
	os = zfsvfs->z_os;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1653
	offset = uio->uio_loffset;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1654
	prefetch = zp->z_zn_prefetch;
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1655
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1656
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1657
	 * Initialize the iterator cursor.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
	if (offset <= 3) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1660
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
		 * Start iteration from the beginning of the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1662
		 */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1663
		zap_cursor_init(&zc, os, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1664
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1665
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1666
		 * The offset is a serialized cursor.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1667
		 */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1668
		zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1669
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1670
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1671
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1672
	 * Get space to change directory entries into fs independent format.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1673
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1674
	iovp = uio->uio_iov;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1675
	bytes_wanted = iovp->iov_len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1676
	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1677
		bufsize = bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1678
		outbuf = kmem_alloc(bufsize, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1679
		odp = (struct dirent64 *)outbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1680
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1681
		bufsize = bytes_wanted;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
		odp = (struct dirent64 *)iovp->iov_base;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
	 * Transform to file-system independent format
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1687
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1688
	outcount = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
	while (outcount < bytes_wanted) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1690
		ino64_t objnum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1691
		ushort_t reclen;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1692
		off64_t *next;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1693
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1694
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1695
		 * Special case `.', `..', and `.zfs'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1696
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
		if (offset == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
			(void) strcpy(zap.za_name, ".");
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1699
			objnum = zp->z_id;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1700
		} else if (offset == 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1701
			(void) strcpy(zap.za_name, "..");
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1702
			objnum = zp->z_phys->zp_parent;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
		} else if (offset == 2 && zfs_show_ctldir(zp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1704
			(void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1705
			objnum = ZFSCTL_INO_ROOT;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1706
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1707
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1708
			 * Grab next entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1709
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1710
			if (error = zap_cursor_retrieve(&zc, &zap)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1711
				if ((*eofp = (error == ENOENT)) != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1712
					break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1713
				else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1714
					goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1715
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1716
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
			if (zap.za_integer_length != 8 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
			    zap.za_num_integers != 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1719
				cmn_err(CE_WARN, "zap_readdir: bad directory "
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
				    "entry, obj = %lld, offset = %lld\n",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1721
				    (u_longlong_t)zp->z_id,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
				    (u_longlong_t)offset);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1723
				error = ENXIO;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1724
				goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1725
			}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1726
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1727
			objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1728
			/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1729
			 * MacOS X can extract the object type here such as:
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1730
			 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1731
			 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1732
		}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1733
		reclen = DIRENT64_RECLEN(strlen(zap.za_name));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1734
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1735
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1736
		 * Will this entry fit in the buffer?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1737
		 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1738
		if (outcount + reclen > bufsize) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1739
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1740
			 * Did we manage to fit anything in the buffer?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1741
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1742
			if (!outcount) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1743
				error = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
				goto update;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
		 * Add this entry:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1750
		 */
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1751
		odp->d_ino = objnum;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1752
		odp->d_reclen = reclen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
		/* NOTE: d_off is the offset for the *next* entry */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
		next = &(odp->d_off);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
		(void) strncpy(odp->d_name, zap.za_name,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1756
		    DIRENT64_NAMELEN(reclen));
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1757
		outcount += reclen;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1758
		odp = (dirent64_t *)((intptr_t)odp + reclen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1759
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1760
		ASSERT(outcount <= bufsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
		/* Prefetch znode */
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1763
		if (prefetch)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3898
diff changeset
  1764
			dmu_prefetch(os, objnum, 0, 0);
789
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
		 * Move to the next entry, fill in the previous offset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
		if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
			zap_cursor_advance(&zc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1771
			offset = zap_cursor_serialize(&zc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1772
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
			offset += 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1775
		*next = offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1776
	}
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
  1777
	zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1778
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1779
	if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1780
		iovp->iov_base += outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1781
		iovp->iov_len -= outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1782
		uio->uio_resid -= outcount;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
	} else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1785
		 * Reset the pointer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1786
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1787
		offset = uio->uio_loffset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1790
update:
885
d925b21dba78 6347493 tar of 25K empty directory entries in ZFS takes 30+ seconds ...
ahrens
parents: 869
diff changeset
  1791
	zap_cursor_fini(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
	if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
		kmem_free(outbuf, bufsize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1794
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
	if (error == ENOENT)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1796
		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1797
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1798
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
	uio->uio_loffset = offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1801
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1802
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1803
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1804
4720
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  1805
ulong_t zfs_fsync_sync_cnt = 4;
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  1806
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1807
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1808
zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1809
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1810
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1811
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1812
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  1813
	/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  1814
	 * 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
  1815
	 * 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
  1816
	 * 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
  1817
	 * 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
  1818
	 */
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  1819
	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
  1820
	    (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  1821
		(void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr);
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  1822
4720
8edc0d2e6f3f 6535160 Lock contention on zl_lock from zil_commit
fr157268
parents: 4709
diff changeset
  1823
	(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
  1824
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1825
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  1826
	zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1827
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1828
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1829
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1830
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1831
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1832
 * Get the requested file attributes and place them in the provided
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1833
 * vattr structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1834
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1835
 *	IN:	vp	- vnode of file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
 *		vap	- va_mask identifies requested attributes.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1837
 *		flags	- [UNUSED]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1838
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1839
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
 *	OUT:	vap	- attribute values.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1842
 *	RETURN:	0 (always succeeds)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1843
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1844
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1845
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1846
zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1848
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1850
	znode_phys_t *pzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1851
	int	error;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1852
	uint64_t links;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1854
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1855
	pzp = zp->z_phys;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1856
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1857
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1858
	 * Return all attributes.  It's cheaper to provide the answer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
	 * than to determine whether we were asked the question.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1860
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1861
	mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1862
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1863
	vap->va_type = vp->v_type;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1864
	vap->va_mode = pzp->zp_mode & MODEMASK;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1865
	vap->va_uid = zp->z_phys->zp_uid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1866
	vap->va_gid = zp->z_phys->zp_gid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1867
	vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1868
	vap->va_nodeid = zp->z_id;
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1869
	if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1870
		links = pzp->zp_links + 1;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1871
	else
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1872
		links = pzp->zp_links;
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4339
diff changeset
  1873
	vap->va_nlink = MIN(links, UINT32_MAX);	/* nlink_t limit! */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1874
	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
  1875
	vap->va_rdev = vp->v_rdev;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
	vap->va_seq = zp->z_seq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1877
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1878
	ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1879
	ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
	ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1881
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1882
	/*
905
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1883
	 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1884
	 * Also, if we are the owner don't bother, since owner should
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1885
	 * always be allowed to read basic attributes of file.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1886
	 */
905
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1887
	if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) &&
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1888
	    (zp->z_phys->zp_uid != crgetuid(cr))) {
920e9b2e0899 6347134 zfs_zaccess() is killing ZFS stat() performance
marks
parents: 885
diff changeset
  1889
		if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1890
			mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1891
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1892
			return (error);
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
	mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1897
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1898
	dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1899
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1900
	if (zp->z_blksz == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1901
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1902
		 * Block size hasn't been set; suggest maximal I/O transfers.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1903
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1904
		vap->va_blksize = zfsvfs->z_max_blksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1905
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1906
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1907
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1909
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1910
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1911
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
 * Set the file attributes to the values contained in the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1913
 * vattr structure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1915
 *	IN:	vp	- vnode of file to be modified.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
 *		vap	- new attribute values.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
 *		flags	- ATTR_UTIME set if non-default time values provided.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1920
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1921
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1922
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
 *	vp - ctime updated, mtime updated if size changed.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1928
zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1929
	caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
{
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1931
	znode_t		*zp = VTOZ(vp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1932
	znode_phys_t	*pzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1934
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
	dmu_tx_t	*tx;
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1936
	vattr_t		oldva;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
	uint_t		mask = vap->va_mask;
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1938
	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
  1939
	int		trim_mask = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
	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
  1941
	znode_t		*attrzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1942
	int		need_policy = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
	int		err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1944
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1945
	if (mask == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1946
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1947
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1948
	if (mask & AT_NOSET)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1950
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1951
	if (mask & AT_SIZE && vp->v_type == VDIR)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1952
		return (EISDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1953
1394
e8d500b79ced 6381600 tc_open.021 in mstc testsuite is returning -1 when run on a zfs
marks
parents: 1308
diff changeset
  1954
	if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO)
1308
997e6a49c409 6362908 nfsv4-test: RENAME does not check PERM with NFSv4/ZFS
marks
parents: 1298
diff changeset
  1955
		return (EINVAL);
997e6a49c409 6362908 nfsv4-test: RENAME does not check PERM with NFSv4/ZFS
marks
parents: 1298
diff changeset
  1956
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1957
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1958
	pzp = zp->z_phys;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  1959
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1961
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
  1962
	attrzp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1963
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1964
	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1965
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1966
		return (EROFS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1967
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1969
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1970
	 * First validate permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1971
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
	if (mask & AT_SIZE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1974
		err = zfs_zaccess(zp, ACE_WRITE_DATA, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1976
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1978
		}
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1979
		/*
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1980
		 * XXX - Note, we are not providing any open
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1981
		 * mode flags here (like FNDELAY), so we may
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1982
		 * block if there are locks present... this
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1983
		 * should be addressed in openat().
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1984
		 */
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1985
		do {
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1986
			err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  1987
			/* NB: we already did dmu_tx_wait() if necessary */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1988
		} while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1989
		if (err) {
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1990
			ZFS_EXIT(zfsvfs);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1991
			return (err);
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  1992
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1993
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1994
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1995
	if (mask & (AT_ATIME|AT_MTIME))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1996
		need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1997
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1998
	if (mask & (AT_UID|AT_GID)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1999
		int	idmask = (mask & (AT_UID|AT_GID));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2000
		int	take_owner;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2001
		int	take_group;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2002
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2003
		/*
913
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2004
		 * 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
  2005
		 * 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
  2006
		 */
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2007
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2008
		if (!(mask & AT_MODE))
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2009
			vap->va_mode = pzp->zp_mode;
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2010
e99aef503251 6299353 chown(2) fails to clear SUID/SGID bits on ZFS.
marks
parents: 905
diff changeset
  2011
		/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
		 * Take ownership or chgrp to group we are a member of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2013
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2014
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
		take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
		take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
		 * If both AT_UID and AT_GID are set then take_owner and
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
		 * take_group must both be set in order to allow taking
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2021
		 * ownership.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2022
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2023
		 * Otherwise, send the check through secpolicy_vnode_setattr()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2024
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2025
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2026
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2027
		if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2028
		    ((idmask == AT_UID) && take_owner) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2029
		    ((idmask == AT_GID) && take_group)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2030
			if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2031
				/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2032
				 * Remove setuid/setgid for non-privileged users
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2033
				 */
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2034
				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
  2035
				trim_mask = (mask & (AT_UID|AT_GID));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2036
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2037
				need_policy =  TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2038
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2039
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2040
			need_policy =  TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2041
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2042
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2043
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
  2044
	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
  2045
	oldva.va_mode = pzp->zp_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
  2046
	oldva.va_uid = zp->z_phys->zp_uid;
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
  2047
	oldva.va_gid = zp->z_phys->zp_gid;
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
  2048
	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
  2049
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
  2050
	if (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
  2051
		if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) {
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
  2052
			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
  2053
			    &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
  2054
			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
  2055
				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
  2056
				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
  2057
			}
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
  2058
			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
  2059
		} 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
  2060
			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
  2061
		}
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
  2062
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2063
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2064
	if (need_policy) {
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2065
		/*
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2066
		 * 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
  2067
		 * 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
  2068
		 * 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
  2069
		 * UID|GID and or MODE from mask so that
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2070
		 * secpolicy_vnode_setattr() doesn't revoke it.
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2071
		 */
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
  2072
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
  2073
		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
  2074
			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
  2075
			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
  2076
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
  2077
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2078
		err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2079
		    (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2080
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2081
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2082
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2083
		}
1115
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2084
2b9c0c6b5148 6362068 zfs chown/chgrp doesn't always work
marks
parents: 913
diff changeset
  2085
		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
  2086
			vap->va_mask |= saved_mask;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2087
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2088
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2089
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2090
	 * secpolicy_vnode_setattr, or take ownership may have
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2091
	 * changed va_mask
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2092
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2093
	mask = vap->va_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2094
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2095
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2096
	dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2097
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2098
	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
  2099
		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
  2100
0364d1928a7f 6380036 zfs does not clear S_ISUID and S_ISGID bits on successful writes
marks
parents: 1544
diff changeset
  2101
		new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2102
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2103
		if (zp->z_phys->zp_acl.z_acl_extern_obj)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2104
			dmu_tx_hold_write(tx,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2105
			    pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2106
		else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2107
			dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2108
			    0, ZFS_ACL_SIZE(MAX_ACL_SIZE));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2109
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2110
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
  2111
	if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) {
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2112
		err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &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
  2113
		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
  2114
			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
  2115
			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
  2116
			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
  2117
		}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2118
		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
  2119
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2120
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2121
	err = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2122
	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
  2123
		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
  2124
			VN_RELE(ZTOV(attrzp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2125
		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2126
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2127
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2128
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2129
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2130
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2131
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2132
		return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2133
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2134
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2135
	dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2136
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2137
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2138
	 * Set each attribute requested.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2139
	 * We group settings according to the locks they need to acquire.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2140
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2141
	 * Note: you cannot set ctime directly, although it will be
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2142
	 * updated as a side-effect of calling this function.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2143
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2144
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2145
	mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2146
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2147
	if (mask & AT_MODE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2148
		err = zfs_acl_chmod_setattr(zp, new_mode, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2149
		ASSERT3U(err, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2150
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2151
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
  2152
	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
  2153
		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
  2154
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2155
	if (mask & AT_UID) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2156
		zp->z_phys->zp_uid = (uint64_t)vap->va_uid;
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
  2157
		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
  2158
			attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid;
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2159
		}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2160
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2161
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2162
	if (mask & AT_GID) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2163
		zp->z_phys->zp_gid = (uint64_t)vap->va_gid;
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
  2164
		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
  2165
			attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid;
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2166
	}
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2167
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 1141
diff changeset
  2168
	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
  2169
		mutex_exit(&attrzp->z_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2170
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2171
	if (mask & AT_ATIME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2172
		ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2173
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2174
	if (mask & AT_MTIME)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2175
		ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2176
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2177
	if (mask & AT_SIZE)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2178
		zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx);
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2179
	else if (mask != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2180
		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2181
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  2182
	if (mask != 0)
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  2183
		zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2184
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2185
	mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2186
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
  2187
	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
  2188
		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
  2189
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2190
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2191
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2192
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2193
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2194
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2195
3271
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2196
typedef struct zfs_zlock {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2197
	krwlock_t	*zl_rwlock;	/* lock we acquired */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2198
	znode_t		*zl_znode;	/* znode we held */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2199
	struct zfs_zlock *zl_next;	/* next in list */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2200
} zfs_zlock_t;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2201
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2202
/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2203
 * Drop locks and release vnodes that were held by zfs_rename_lock().
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2204
 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2205
static void
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2206
zfs_rename_unlock(zfs_zlock_t **zlpp)
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2207
{
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2208
	zfs_zlock_t *zl;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2209
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2210
	while ((zl = *zlpp) != NULL) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2211
		if (zl->zl_znode != NULL)
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2212
			VN_RELE(ZTOV(zl->zl_znode));
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2213
		rw_exit(zl->zl_rwlock);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2214
		*zlpp = zl->zl_next;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2215
		kmem_free(zl, sizeof (*zl));
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2216
	}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2217
}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2218
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2219
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2220
 * Search back through the directory tree, using the ".." entries.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2221
 * Lock each directory in the chain to prevent concurrent renames.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2222
 * Fail any attempt to move a directory into one of its own descendants.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2223
 * XXX - z_parent_lock can overlap with map or grow locks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2224
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2225
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2226
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
  2227
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2228
	zfs_zlock_t	*zl;
3638
6b28ebc717aa 6496357 spec_fsync() is useless on devices that do write caching
billm
parents: 3461
diff changeset
  2229
	znode_t		*zp = tdzp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2230
	uint64_t	rootid = zp->z_zfsvfs->z_root;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2231
	uint64_t	*oidp = &zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2232
	krwlock_t	*rwlp = &szp->z_parent_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2233
	krw_t		rw = RW_WRITER;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2234
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2235
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2236
	 * First pass write-locks szp and compares to zp->z_id.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2237
	 * Later passes read-lock zp and compare to zp->z_parent.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2238
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2239
	do {
3271
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2240
		if (!rw_tryenter(rwlp, rw)) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2241
			/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2242
			 * Another thread is renaming in this path.
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2243
			 * Note that if we are a WRITER, we don't have any
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2244
			 * parent_locks held yet.
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2245
			 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2246
			if (rw == RW_READER && zp->z_id > szp->z_id) {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2247
				/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2248
				 * Drop our locks and restart
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2249
				 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2250
				zfs_rename_unlock(&zl);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2251
				*zlpp = NULL;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2252
				zp = tdzp;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2253
				oidp = &zp->z_id;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2254
				rwlp = &szp->z_parent_lock;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2255
				rw = RW_WRITER;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2256
				continue;
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2257
			} else {
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2258
				/*
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2259
				 * Wait for other thread to drop its locks
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2260
				 */
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2261
				rw_enter(rwlp, rw);
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2262
			}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2263
		}
bae4d02fdc5b 6502193 race in zfs_rename
maybee
parents: 3234
diff changeset
  2264
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2265
		zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2266
		zl->zl_rwlock = rwlp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2267
		zl->zl_znode = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2268
		zl->zl_next = *zlpp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2269
		*zlpp = zl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2270
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2271
		if (*oidp == szp->z_id)		/* We're a descendant of szp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2272
			return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2273
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2274
		if (*oidp == rootid)		/* We've hit the top */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2275
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2276
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2277
		if (rw == RW_READER) {		/* i.e. not the first pass */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2278
			int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2279
			if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2280
				return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2281
			zl->zl_znode = zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2282
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2283
		oidp = &zp->z_phys->zp_parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2284
		rwlp = &zp->z_parent_lock;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2285
		rw = RW_READER;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2286
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2287
	} while (zp->z_id != sdzp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2288
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2289
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2290
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2291
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2292
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2293
 * Move an entry from the provided source directory to the target
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2294
 * directory.  Change the entry name as indicated.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2295
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2296
 *	IN:	sdvp	- Source directory containing the "old entry".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2297
 *		snm	- Old entry name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2298
 *		tdvp	- Target directory to contain the "new entry".
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2299
 *		tnm	- New entry name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2300
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2301
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2302
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2303
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2304
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2305
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2306
 *	sdvp,tdvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2307
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2308
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2309
zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2310
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2311
	znode_t		*tdzp, *szp, *tzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2312
	znode_t		*sdzp = VTOZ(sdvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2313
	zfsvfs_t	*zfsvfs = sdzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2314
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2315
	vnode_t		*realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2316
	zfs_dirlock_t	*sdl, *tdl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2317
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2318
	zfs_zlock_t	*zl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2319
	int		cmp, serr, terr, error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2320
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2321
	ZFS_ENTER_VERIFY_ZP(zfsvfs, sdzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2322
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2323
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2324
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2325
	 * Make sure we have the real vp for the target directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2326
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2327
	if (VOP_REALVP(tdvp, &realvp) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2328
		tdvp = realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2329
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2330
	if (tdvp->v_vfsp != sdvp->v_vfsp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2331
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2332
		return (EXDEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2333
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2334
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2335
	tdzp = VTOZ(tdvp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2336
	if (!tdzp->z_dbuf_held) {
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2337
		ZFS_EXIT(zfsvfs);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2338
		return (EIO);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2339
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2340
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2341
	szp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2342
	tzp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2343
	zl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2344
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2345
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2346
	 * This is to prevent the creation of links into attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2347
	 * by renaming a linked file into/outof an attribute directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2348
	 * See the comment in zfs_link() for why this is considered bad.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2349
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2350
	if ((tdzp->z_phys->zp_flags & ZFS_XATTR) !=
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2351
	    (sdzp->z_phys->zp_flags & ZFS_XATTR)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2352
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2353
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2354
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2355
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2356
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2357
	 * Lock source and target directory entries.  To prevent deadlock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2358
	 * a lock ordering must be defined.  We lock the directory with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2359
	 * the smallest object id first, or if it's a tie, the one with
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2360
	 * the lexically first name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2361
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2362
	if (sdzp->z_id < tdzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2363
		cmp = -1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2364
	} else if (sdzp->z_id > tdzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2365
		cmp = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2366
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2367
		cmp = strcmp(snm, tnm);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2368
		if (cmp == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2369
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2370
			 * POSIX: "If the old argument and the new argument
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2371
			 * both refer to links to the same existing file,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2372
			 * the rename() function shall return successfully
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2373
			 * and perform no other action."
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2374
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2375
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2376
			return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2377
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2378
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2379
	if (cmp < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2380
		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2381
		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2382
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2383
		terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2384
		serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2385
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2386
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2387
	if (serr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2388
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2389
		 * Source entry invalid or not there.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2390
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2391
		if (!terr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2392
			zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2393
			if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2394
				VN_RELE(ZTOV(tzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2395
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2396
		if (strcmp(snm, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2397
			serr = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2398
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2399
		return (serr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2400
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2401
	if (terr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2402
		zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2403
		VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2404
		if (strcmp(tnm, "..") == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2405
			terr = EINVAL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2406
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2407
		return (terr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2408
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2409
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2410
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2411
	 * Must have write access at the source to remove the old entry
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2412
	 * and write access at the target to create the new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2413
	 * Note that if target and source are the same, this can be
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2414
	 * done in a single check.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2415
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2416
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2417
	if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2418
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2419
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2420
	if (ZTOV(szp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2421
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2422
		 * Check to make sure rename is valid.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2423
		 * 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
  2424
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2425
		if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2426
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2427
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2428
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2429
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2430
	 * Does target exist?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2431
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2432
	if (tzp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2433
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2434
		 * Source and target must be the same type.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2435
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2436
		if (ZTOV(szp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2437
			if (ZTOV(tzp)->v_type != VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2438
				error = ENOTDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2439
				goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2440
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2441
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2442
			if (ZTOV(tzp)->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2443
				error = EISDIR;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2444
				goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2445
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2446
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2447
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2448
		 * POSIX dictates that when the source and target
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2449
		 * entries refer to the same file object, rename
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2450
		 * must do nothing and exit without error.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2451
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2452
		if (szp->z_id == tzp->z_id) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2453
			error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2454
			goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2455
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2456
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2457
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2458
	vnevent_rename_src(ZTOV(szp), sdvp, snm);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2459
	if (tzp)
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2460
		vnevent_rename_dest(ZTOV(tzp), tdvp, tnm);
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2461
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2462
	/*
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2463
	 * notify the target directory if it is not the same
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2464
	 * as source directory.
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2465
	 */
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2466
	if (tdvp != sdvp) {
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2467
		vnevent_rename_dest_dir(tdvp);
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2468
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2470
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2471
	dmu_tx_hold_bonus(tx, szp->z_id);	/* nlink changes */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2472
	dmu_tx_hold_bonus(tx, sdzp->z_id);	/* nlink changes */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2473
	dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2474
	dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2475
	if (sdzp != tdzp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2476
		dmu_tx_hold_bonus(tx, tdzp->z_id);	/* nlink changes */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2477
	if (tzp)
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2478
		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
  2479
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2480
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2481
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2482
		if (zl != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2483
			zfs_rename_unlock(&zl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2484
		zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2485
		zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2486
		VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2487
		if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2488
			VN_RELE(ZTOV(tzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2489
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2490
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2491
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2492
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2493
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2494
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2495
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2496
		return (error);
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
	if (tzp)	/* Attempt to remove the existing target */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2500
		error = zfs_link_destroy(tdl, tzp, tx, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2501
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2502
	if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2503
		error = zfs_link_create(tdl, szp, tx, ZRENAMING);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2504
		if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2505
			error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2506
			ASSERT(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
  2507
			zfs_log_rename(zilog, tx, TX_RENAME, sdzp,
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  2508
			    sdl->dl_name, tdzp, tdl->dl_name, szp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2509
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2510
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2511
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2512
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2513
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2514
	if (zl != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2515
		zfs_rename_unlock(&zl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2516
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2517
	zfs_dirent_unlock(sdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2518
	zfs_dirent_unlock(tdl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2519
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2520
	VN_RELE(ZTOV(szp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2521
	if (tzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2522
		VN_RELE(ZTOV(tzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2523
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2524
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2525
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2526
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2527
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2528
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2529
 * Insert the indicated symbolic reference entry into the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2530
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2531
 *	IN:	dvp	- Directory to contain new symbolic link.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2532
 *		link	- Name for new symlink entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2533
 *		vap	- Attributes of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2534
 *		target	- Target path of new symlink.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2535
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2536
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2537
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2538
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2539
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2540
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2541
 *	dvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2542
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2543
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2544
zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2545
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2546
	znode_t		*zp, *dzp = VTOZ(dvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2547
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2548
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2549
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2550
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2551
	uint64_t	zoid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2552
	int		len = strlen(link);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2553
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2554
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2555
	ASSERT(vap->va_type == VLNK);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2556
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2557
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2558
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2559
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2560
	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2561
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2562
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2563
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2564
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2565
	if (len > MAXPATHLEN) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2566
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2567
		return (ENAMETOOLONG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2568
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2569
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2570
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2571
	 * Attempt to lock directory; fail if entry already exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2572
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2573
	if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2574
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2575
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2576
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2577
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2578
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2579
	dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2580
	dmu_tx_hold_bonus(tx, dzp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2581
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2582
	if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2583
		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2584
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2585
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2586
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2587
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2588
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2589
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2590
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2591
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2592
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2593
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2594
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2595
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2596
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2597
	dmu_buf_will_dirty(dzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2598
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2599
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2600
	 * Create a new object for the symlink.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2601
	 * Put the link content into bonus buffer if it will fit;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2602
	 * otherwise, store it just like any other file data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2603
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2604
	zoid = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2605
	if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2606
		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2607
		if (len != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2608
			bcopy(link, zp->z_phys + 1, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2609
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2610
		dmu_buf_t *dbp;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2611
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2612
		zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2613
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2614
		/*
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2615
		 * Nothing can access the znode yet so no locking needed
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2616
		 * for growing the znode's blocksize.
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2617
		 */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2618
		zfs_grow_blocksize(zp, len, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2619
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2620
		VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2621
		dmu_buf_will_dirty(dbp, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2622
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2623
		ASSERT3U(len, <=, dbp->db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2624
		bcopy(link, dbp->db_data, len);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2625
		dmu_buf_rele(dbp, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2626
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2627
	zp->z_phys->zp_size = len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2628
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2629
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2630
	 * Insert the new object into the directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2631
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2632
	(void) zfs_link_create(dl, zp, tx, ZNEW);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2633
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2634
	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
  2635
		zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2636
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2637
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2638
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2639
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2641
	VN_RELE(ZTOV(zp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2642
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2643
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2644
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2645
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2646
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2647
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2648
 * Return, in the buffer contained in the provided uio structure,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2649
 * the symbolic path referred to by vp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2650
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2651
 *	IN:	vp	- vnode of symbolic link.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2652
 *		uoip	- structure to contain the link path.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2653
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2654
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2655
 *	OUT:	uio	- structure to contain the link path.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2656
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2657
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2658
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2659
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2660
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2661
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2662
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2663
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2664
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2665
zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2666
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2667
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2668
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2669
	size_t		bufsz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2670
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2671
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2672
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2673
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2674
	bufsz = (size_t)zp->z_phys->zp_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2675
	if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2676
		error = uiomove(zp->z_phys + 1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2677
		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2678
	} else {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2679
		dmu_buf_t *dbp;
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2680
		error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2681
		if (error) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2682
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2683
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2684
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2685
		error = uiomove(dbp->db_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2686
		    MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2687
		dmu_buf_rele(dbp, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2688
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2689
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2690
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2691
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2692
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2693
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2694
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2695
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2696
 * Insert a new entry into directory tdvp referencing svp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2697
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2698
 *	IN:	tdvp	- Directory to contain new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2699
 *		svp	- vnode of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2700
 *		name	- name of new entry.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2701
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2702
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2703
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2704
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2705
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2706
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2707
 *	tdvp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2708
 *	 svp - ctime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2709
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2710
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2711
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2712
zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2713
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2714
	znode_t		*dzp = VTOZ(tdvp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2715
	znode_t		*tzp, *szp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2716
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2717
	zilog_t		*zilog;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2718
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2719
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2720
	vnode_t		*realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2721
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2722
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2723
	ASSERT(tdvp->v_type == VDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2724
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2725
	ZFS_ENTER_VERIFY_ZP(zfsvfs, dzp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2726
	zilog = zfsvfs->z_log;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2727
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2728
	if (VOP_REALVP(svp, &realvp) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2729
		svp = realvp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2730
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2731
	if (svp->v_vfsp != tdvp->v_vfsp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2732
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2733
		return (EXDEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2734
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2735
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2736
	szp = VTOZ(svp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2737
	if (!szp->z_dbuf_held) {
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2738
		ZFS_EXIT(zfsvfs);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2739
		return (EIO);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2740
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2741
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2742
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2743
	 * We do not support links between attributes and non-attributes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2744
	 * because of the potential security risk of creating links
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2745
	 * into "normal" file space in order to circumvent restrictions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2746
	 * imposed in attribute space.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2747
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2748
	if ((szp->z_phys->zp_flags & ZFS_XATTR) !=
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2749
	    (dzp->z_phys->zp_flags & ZFS_XATTR)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2750
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2751
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2752
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2753
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2754
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2755
	 * POSIX dictates that we return EPERM here.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2756
	 * Better choices include ENOTSUP or EISDIR.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2757
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2758
	if (svp->v_type == VDIR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2759
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2760
		return (EPERM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2761
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2762
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2763
	if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2764
	    secpolicy_basic_link(cr) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2765
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2766
		return (EPERM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2767
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2768
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2769
	if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2770
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2771
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2772
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2774
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2775
	 * Attempt to lock directory; fail if entry already exists.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2776
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2777
	if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2778
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2779
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2780
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2781
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2782
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2783
	dmu_tx_hold_bonus(tx, szp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  2784
	dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2785
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2786
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2787
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2788
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2789
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2790
			dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2791
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2792
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2793
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2794
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2795
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2796
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2797
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2798
	error = zfs_link_create(dl, szp, tx, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2799
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2800
	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
  2801
		zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2802
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2803
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2804
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2805
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2806
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2807
	if (error == 0) {
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2808
		vnevent_link(svp);
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2809
	}
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  2810
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2811
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2812
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2813
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2814
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2815
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2816
 * zfs_null_putapage() is used when the file system has been force
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2817
 * unmounted. It just drops the pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2818
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2819
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2820
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2821
zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2822
		size_t *lenp, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2823
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2824
	pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2825
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2826
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2827
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2828
/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2829
 * 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
  2830
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2831
 *	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
  2832
 *		pp	- page to push.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2833
 *		flags	- additional flags.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2834
 *		cr	- credentials of caller.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2835
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2836
 *	OUT:	offp	- start of range pushed.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2837
 *		lenp	- len of range pushed.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2838
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2839
 *	RETURN:	0 if success
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2840
 *		error code if failure
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2841
 *
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2842
 * 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
  2843
 * 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
  2844
 * unlocked.
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2845
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2846
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2847
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2848
zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2849
		size_t *lenp, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2850
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2851
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2852
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2853
	zilog_t		*zilog = zfsvfs->z_log;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2854
	dmu_tx_t	*tx;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2855
	rl_t		*rl;
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2856
	u_offset_t	off, koff;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2857
	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
  2858
	uint64_t	filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2859
	int		err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2860
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  2861
	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
  2862
	off = pp->p_offset;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2863
	len = PAGESIZE;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2864
	/*
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2865
	 * If our blocksize is bigger than the page size, try to kluster
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2866
	 * muiltiple pages so that we write a full block (thus avoiding
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2867
	 * a read-modify-write).
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2868
	 */
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  2869
	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
  2870
		if (!ISP2(zp->z_blksz)) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2871
			/* Only one block in the file. */
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2872
			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
  2873
			koff = 0;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2874
		} else {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2875
			klen = zp->z_blksz;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2876
			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
  2877
		}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2878
		ASSERT(koff <= filesz);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2879
		if (koff + klen > filesz)
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2880
			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
  2881
		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
  2882
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2883
	ASSERT3U(btop(len), ==, btopr(len));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2884
top:
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2885
	rl = zfs_range_lock(zp, off, len, RL_WRITER);
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  2886
	/*
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  2887
	 * Can't push pages past end-of-file.
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  2888
	 */
4709
dc10a713d1a0 6453407 rm a file when the root file system is at its quota limit reports ENOSPC
maybee
parents: 4543
diff changeset
  2889
	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
  2890
	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
  2891
		/* ignore all pages */
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2892
		err = 0;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2893
		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
  2894
	} 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
  2895
		int npages = btopr(filesz - off);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2896
		page_t *trunc;
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2897
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2898
		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
  2899
		/* 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
  2900
		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
  2901
			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
  2902
		len = filesz - off;
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  2903
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2904
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2905
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2906
	dmu_tx_hold_write(tx, zp->z_id, off, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2907
	dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2908
	err = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2909
	if (err != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2910
		if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2911
			zfs_range_unlock(rl);
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2912
			dmu_tx_wait(tx);
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2913
			dmu_tx_abort(tx);
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2914
			err = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2915
			goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2916
		}
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  2917
		dmu_tx_abort(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2918
		goto out;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2919
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2920
2688
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2921
	if (zp->z_blksz <= PAGESIZE) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2922
		caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2923
		ASSERT3U(len, <=, PAGESIZE);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2924
		dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2925
		ppmapout(va);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2926
	} else {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2927
		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
  2928
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2929
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2930
	if (err == 0) {
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2931
		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
  2932
		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
  2933
		dmu_tx_commit(tx);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2934
	}
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2935
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  2936
out:
2237
45affe88ed99 6416482 filebench oltp workload hangs in zfs
maybee
parents: 2113
diff changeset
  2937
	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
  2938
	pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2939
	if (offp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2940
		*offp = off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2941
	if (lenp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2942
		*lenp = len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2943
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2944
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2945
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2946
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2947
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2948
 * Copy the portion of the file indicated from pages into the file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2949
 * The pages are stored in a page list attached to the files vnode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2950
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2951
 *	IN:	vp	- vnode of file to push page data to.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2952
 *		off	- position in file to put data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2953
 *		len	- amount of data to write.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2954
 *		flags	- flags to control the operation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2955
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2956
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2957
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2958
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2959
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2960
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2961
 *	vp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2962
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2963
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2964
zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2965
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2966
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2967
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2968
	page_t		*pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2969
	size_t		io_len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2970
	u_offset_t	io_off;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2971
	uint64_t	filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2972
	int		error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2973
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  2974
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2975
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2976
	ASSERT(zp->z_dbuf_held && zp->z_phys);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2977
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2978
	if (len == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2979
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2980
		 * Search the entire vp list for pages >= off.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2981
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2982
		error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2983
		    flags, cr);
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  2984
		goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2985
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2986
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2987
	filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2988
	if (off > filesz) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2989
		/* past end of file */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2990
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2991
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2992
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2993
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2994
	len = MIN(len, filesz - off);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2995
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  2996
	for (io_off = off; io_off < off + len; io_off += io_len) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2997
		if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  2998
			pp = page_lookup(vp, io_off,
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
  2999
			    (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3000
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3001
			pp = page_lookup_nowait(vp, io_off,
4339
24b45a78e29e 6561733 ZFS znode z_sync_cnt field is miscounted
perrin
parents: 4144
diff changeset
  3002
			    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3003
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3004
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3005
		if (pp != NULL && pvn_getdirty(pp, flags)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3006
			int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3007
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3008
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3009
			 * Found a dirty page to push
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3010
			 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3011
			err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3012
			if (err)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3013
				error = err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3014
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3015
			io_len = PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3016
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3017
	}
1472
6d54521a5237 6381994 zfs_putpage() serializes I/O unnecessarily
perrin
parents: 1460
diff changeset
  3018
out:
2638
4f583dfeae92 6413510 zfs: writing to ZFS filesystem slows down fsync() on other files in the same FS
perrin
parents: 2597
diff changeset
  3019
	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
  3020
		zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3021
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3022
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3023
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3024
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3025
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3026
zfs_inactive(vnode_t *vp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3027
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3028
	znode_t	*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3029
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3030
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3031
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3032
	rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3033
	if (zp->z_dbuf_held == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3034
		if (vn_has_cached_data(vp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3035
			(void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3036
			    B_INVAL, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3037
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3038
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3039
		mutex_enter(&zp->z_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3040
		vp->v_count = 0; /* count arrives as 1 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3041
		if (zp->z_dbuf == NULL) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3042
			mutex_exit(&zp->z_lock);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3043
			zfs_znode_free(zp);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3044
		} else {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3045
			mutex_exit(&zp->z_lock);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3046
		}
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3047
		rw_exit(&zfsvfs->z_teardown_inactive_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3048
		VFS_RELE(zfsvfs->z_vfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3049
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3050
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3051
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3052
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3053
	 * Attempt to push any data in the page cache.  If this fails
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3054
	 * we will get kicked out later in zfs_zinactive().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3055
	 */
1298
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3056
	if (vn_has_cached_data(vp)) {
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3057
		(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
  3058
		    cr);
1a70f57b62f0 6368181 some memleaks found related to zfs mount
perrin
parents: 1231
diff changeset
  3059
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3060
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
  3061
	if (zp->z_atime_dirty && zp->z_unlinked == 0) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3062
		dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3063
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3064
		dmu_tx_hold_bonus(tx, zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3065
		error = dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3066
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3067
			dmu_tx_abort(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3068
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3069
			dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3070
			mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3071
			zp->z_atime_dirty = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3072
			mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3073
			dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3074
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3075
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3076
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3077
	zfs_zinactive(zp);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3078
	rw_exit(&zfsvfs->z_teardown_inactive_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3079
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3080
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3081
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3082
 * Bounds-check the seek operation.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3083
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3084
 *	IN:	vp	- vnode seeking within
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3085
 *		ooff	- old file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3086
 *		noffp	- pointer to new file offset
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3087
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3088
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3089
 *		EINVAL if new offset invalid
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3090
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3091
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3092
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3093
zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3094
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3095
	if (vp->v_type == VDIR)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3096
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3097
	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3098
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3099
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3100
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3101
 * Pre-filter the generic locking function to trap attempts to place
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3102
 * a mandatory lock on a memory mapped file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3103
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3104
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3105
zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3106
    flk_callback_t *flk_cbp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3107
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3108
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3109
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3110
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3111
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3112
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3113
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3114
	/*
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3115
	 * We are following the UFS semantics with respect to mapcnt
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3116
	 * here: If we see that the file is mapped already, then we will
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3117
	 * return an error, but we don't worry about races between this
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3118
	 * function and zfs_map().
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3119
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3120
	if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3121
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3122
		return (EAGAIN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3123
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3124
	error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3125
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3126
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3127
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3128
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3129
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3130
 * 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
  3131
 * 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
  3132
 * multiple pages at once (klustering).
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3133
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3134
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3135
zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3136
    caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3137
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3138
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3139
	page_t *pp, *cur_pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3140
	objset_t *os = zp->z_zfsvfs->z_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3141
	caddr_t va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3142
	u_offset_t io_off, total;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3143
	uint64_t oid = zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3144
	size_t io_len;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3145
	uint64_t filesz;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3146
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3147
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3148
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3149
	 * If we are only asking for a single page don't bother klustering.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3150
	 */
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3151
	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
  3152
	if (off >= filesz)
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3153
		return (EFAULT);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3154
	if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3155
		io_off = off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3156
		io_len = PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3157
		pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3158
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3159
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3160
		 * Try to fill a kluster of pages (a blocks worth).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3161
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3162
		size_t klen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3163
		u_offset_t koff;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3165
		if (!ISP2(zp->z_blksz)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3166
			/* Only one block in the file. */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3167
			klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3168
			koff = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3169
		} 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
  3170
			/*
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3171
			 * 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
  3172
			 * 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
  3173
			 * 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
  3174
			 * 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
  3175
			 * 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
  3176
			 */
ef325e37ab22 6471359 zfs_fillpage() when faulting aligns on the range size within the file, getting the wrong large page
gw25295
parents: 3063
diff changeset
  3177
			koff = off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3178
			klen = plsz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3179
		}
1819
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3180
		ASSERT(koff <= filesz);
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3181
		if (koff + klen > filesz)
570a79e4d798 6407842 zfs panic when closing a file
maybee
parents: 1816
diff changeset
  3182
			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
  3183
		ASSERT3U(off, >=, koff);
12e2ad22b970 6447701 ZFS hangs when iSCSI Target attempts to initialize its backing store
maybee
parents: 2638
diff changeset
  3184
		ASSERT3U(off, <, koff + klen);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3185
		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
  3186
		    &io_len, koff, klen, 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3187
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3188
	if (pp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3189
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3190
		 * Some other thread entered the page before us.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3191
		 * Return to zfs_getpage to retry the lookup.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3192
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3193
		*pl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3194
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3195
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3196
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3197
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3198
	 * Fill the pages in the kluster.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3199
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3200
	cur_pp = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3201
	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
  3202
		ASSERT3U(io_off, ==, cur_pp->p_offset);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3203
		va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3204
		err = dmu_read(os, oid, io_off, PAGESIZE, va);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3205
		ppmapout(va);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3206
		if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3207
			/* On error, toss the entire kluster */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3208
			pvn_read_done(pp, B_ERROR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3209
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3210
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3211
		cur_pp = cur_pp->p_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3212
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3213
out:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3214
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3215
	 * Fill in the page list array from the kluster.  If
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3216
	 * there are too many pages in the kluster, return
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3217
	 * as many pages as possible starting from the desired
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3218
	 * offset `off'.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3219
	 * NOTE: the page list will always be null terminated.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3220
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3221
	pvn_plist_init(pp, pl, plsz, off, io_len, rw);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3222
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3223
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3224
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3225
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3226
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3227
 * Return pointers to the pages for the file region [off, off + len]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3228
 * in the pl array.  If plsz is greater than len, this function may
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3229
 * also return page pointers from before or after the specified
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3230
 * region (i.e. some region [off', off' + plsz]).  These additional
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3231
 * pages are only returned if they are already in the cache, or were
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3232
 * created as part of a klustered read.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3233
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3234
 *	IN:	vp	- vnode of file to get data from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3235
 *		off	- position in file to get data from.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3236
 *		len	- amount of data to retrieve.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3237
 *		plsz	- length of provided page list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3238
 *		seg	- segment to obtain pages for.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3239
 *		addr	- virtual address of fault.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3240
 *		rw	- mode of created pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3241
 *		cr	- credentials of caller.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3242
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3243
 *	OUT:	protp	- protection mode of created pages.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3244
 *		pl	- list of pages created.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3245
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3246
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3247
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3248
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3249
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3250
 *	vp - atime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3251
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3252
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3253
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3254
zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3255
	page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3256
	enum seg_rw rw, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3257
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3258
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3259
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3260
	page_t		*pp, **pl0 = pl;
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3261
	int		need_unlock = 0, err = 0;
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3262
	offset_t	orig_off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3263
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3264
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3265
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3266
	if (protp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3267
		*protp = PROT_ALL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3268
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3269
	ASSERT(zp->z_dbuf_held && zp->z_phys);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3270
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3271
	/* no faultahead (for now) */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3272
	if (pl == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3273
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3274
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3275
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3276
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3277
	/* can't fault past EOF */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3278
	if (off >= zp->z_phys->zp_size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3279
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3280
		return (EFAULT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3281
	}
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3282
	orig_off = off;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3283
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3284
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3285
	 * If we already own the lock, then we must be page faulting
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3286
	 * in the middle of a write to this file (i.e., we are writing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3287
	 * to this file using data from a mapped region of the file).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3288
	 */
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3289
	if (rw_owner(&zp->z_map_lock) != curthread) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3290
		rw_enter(&zp->z_map_lock, RW_WRITER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3291
		need_unlock = TRUE;
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
	 * Loop through the requested range [off, off + len] looking
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3296
	 * for pages.  If we don't find a page, we will need to create
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3297
	 * a new page and fill it with data from the file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3298
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3299
	while (len > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3300
		if (plsz < PAGESIZE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3301
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3302
		if (pp = page_lookup(vp, off, SE_SHARED)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3303
			*pl++ = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3304
			off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3305
			addr += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3306
			len -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3307
			plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3308
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3309
			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
  3310
			if (err)
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3311
				goto out;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3312
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3313
			 * klustering may have changed our region
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3314
			 * to be block aligned.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3315
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3316
			if (((pp = *pl) != 0) && (off != pp->p_offset)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3317
				int delta = off - pp->p_offset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3318
				len += delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3319
				off -= delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3320
				addr -= delta;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3321
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3322
			while (*pl) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3323
				pl++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3324
				off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3325
				addr += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3326
				plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3327
				if (len > PAGESIZE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3328
					len -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3329
				else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3330
					len = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3331
			}
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
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3335
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3336
	 * Fill out the page array with any pages already in the cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3337
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3338
	while (plsz > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3339
		pp = page_lookup_nowait(vp, off, SE_SHARED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3340
		if (pp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3341
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3342
		*pl++ = pp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3343
		off += PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3344
		plsz -= PAGESIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3345
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3346
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3347
	ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3348
out:
2752
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3349
	/*
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3350
	 * 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
  3351
	 * 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
  3352
	 * the file size.
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3353
	 */
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3354
	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
  3355
		err = EFAULT;
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3356
	if (err) {
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3357
		/*
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3358
		 * 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
  3359
		 */
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3360
		while (pl > pl0)
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3361
			page_unlock(*--pl);
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3362
	}
2b25a96fc433 6438702 error handling in zfs_getpage() can trigger "page not locked" panic
perrin
parents: 2688
diff changeset
  3363
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3364
	*pl = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3365
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3366
	if (need_unlock)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3367
		rw_exit(&zp->z_map_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3368
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3369
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3370
	return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3371
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3372
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3373
/*
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3374
 * Request a memory map for a section of a file.  This code interacts
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3375
 * with common code and the VM system as follows:
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3376
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3377
 *	common code calls mmap(), which ends up in smmap_common()
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3378
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3379
 *	this calls VOP_MAP(), which takes you into (say) zfs
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3380
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3381
 *	zfs_map() calls as_map(), passing segvn_create() as the callback
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3382
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3383
 *	segvn_create() creates the new segment and calls VOP_ADDMAP()
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3384
 *
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3385
 *	zfs_addmap() updates z_mapcnt
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3386
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3387
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3388
zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3389
    size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3390
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3391
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3392
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3393
	segvn_crargs_t	vn_a;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3394
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3395
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3396
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3397
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3398
	if (vp->v_flag & VNOMAP) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3399
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3400
		return (ENOSYS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3401
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3402
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3403
	if (off < 0 || len > MAXOFFSET_T - off) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3404
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3405
		return (ENXIO);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3406
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3407
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3408
	if (vp->v_type != VREG) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3409
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3410
		return (ENODEV);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3411
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3412
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3413
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3414
	 * If file is locked, disallow mapping.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3415
	 */
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3416
	if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) {
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3417
		ZFS_EXIT(zfsvfs);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3418
		return (EAGAIN);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3419
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3420
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3421
	as_rangelock(as);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3422
	if ((flags & MAP_FIXED) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3423
		map_addr(addrp, len, off, 1, flags);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3424
		if (*addrp == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3425
			as_rangeunlock(as);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3426
			ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3427
			return (ENOMEM);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3428
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3429
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3430
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3431
		 * User specified address - blow away any previous mappings
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3432
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3433
		(void) as_unmap(as, *addrp, len);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3434
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3435
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3436
	vn_a.vp = vp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3437
	vn_a.offset = (u_offset_t)off;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3438
	vn_a.type = flags & MAP_TYPE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3439
	vn_a.prot = prot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3440
	vn_a.maxprot = maxprot;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3441
	vn_a.cred = cr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3442
	vn_a.amp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3443
	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
  3444
	vn_a.szc = 0;
fa316336e215 6377034 setting physmem in /etc/system does not have desired effect on x86
kchow
parents: 1394
diff changeset
  3445
	vn_a.lgrp_mem_policy_flags = 0;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3446
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3447
	error = as_map(as, *addrp, len, segvn_create, &vn_a);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3448
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3449
	as_rangeunlock(as);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3450
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3451
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3452
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3453
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3454
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3455
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3456
zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3457
    size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3458
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3459
	uint64_t pages = btopr(len);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3460
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3461
	atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3462
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3463
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3464
1773
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3465
/*
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3466
 * 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
  3467
 * 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
  3468
 * 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
  3469
 * 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
  3470
 * 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
  3471
 * 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
  3472
 *
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3473
 * 	open()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3474
 * 	mmap()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3475
 * 	<modify memory>
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3476
 * 	munmap()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3477
 * 	close()
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3478
 * 	<time lapse>
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3479
 * 	putpage() via fsflush
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3480
 *
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3481
 * 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
  3482
 * 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
  3483
 * 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
  3484
 * torn down.
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3485
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3486
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3487
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3488
zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3489
    size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3490
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3491
	uint64_t pages = btopr(len);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3492
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3493
	ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1512
diff changeset
  3494
	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
  3495
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3496
	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
  3497
	    vn_has_cached_data(vp))
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3498
		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr);
d6e4f2855c14 6407791 bringover into ZFS results in s. files newer than extracted source
eschrock
parents: 1757
diff changeset
  3499
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3500
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3501
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3503
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3504
 * Free or allocate space in a file.  Currently, this function only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3505
 * supports the `F_FREESP' command.  However, this command is somewhat
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3506
 * misnamed, as its functionality includes the ability to allocate as
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3507
 * well as free space.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3508
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3509
 *	IN:	vp	- vnode of file to free data in.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3510
 *		cmd	- action to take (only F_FREESP supported).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3511
 *		bfp	- section of file to free/alloc.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3512
 *		flag	- current file open mode flags.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3513
 *		offset	- current file offset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3514
 *		cr	- credentials of caller [UNUSED].
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3515
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3516
 *	RETURN:	0 if success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3517
 *		error code if failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3518
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3519
 * Timestamps:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3520
 *	vp - ctime|mtime updated
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3521
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3522
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3523
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3524
zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3525
    offset_t offset, cred_t *cr, caller_context_t *ct)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3526
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3527
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3528
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3529
	uint64_t	off, len;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3530
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3531
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3532
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3533
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3534
top:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3535
	if (cmd != F_FREESP) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3536
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3537
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3538
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3539
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3540
	if (error = convoff(vp, bfp, 0, offset)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3541
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3542
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3543
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3544
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3545
	if (bfp->l_len < 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3546
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3547
		return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3548
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3549
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3550
	off = bfp->l_start;
1669
3521dbbcb2e8 6343608 ZFS file range locking
perrin
parents: 1576
diff changeset
  3551
	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
  3552
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  3553
	do {
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  3554
		error = zfs_freesp(zp, off, len, flag, TRUE);
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1941
diff changeset
  3555
		/* NB: we already did dmu_tx_wait() if necessary */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1819
diff changeset
  3556
	} while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3557
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3558
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3559
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3560
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3562
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3563
zfs_fid(vnode_t *vp, fid_t *fidp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3564
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3565
	znode_t		*zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3566
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3567
	uint32_t	gen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3568
	uint64_t	object = zp->z_id;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3569
	zfid_short_t	*zfid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3570
	int		size, i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3571
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3572
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3573
	gen = (uint32_t)zp->z_gen;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3574
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3575
	size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3576
	if (fidp->fid_len < size) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3577
		fidp->fid_len = size;
1512
dad2d74ca2cb 6391609 zfs_fid() missing ZFS_EXIT()
ek110237
parents: 1484
diff changeset
  3578
		ZFS_EXIT(zfsvfs);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3579
		return (ENOSPC);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3580
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3582
	zfid = (zfid_short_t *)fidp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3584
	zfid->zf_len = size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3585
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3586
	for (i = 0; i < sizeof (zfid->zf_object); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3587
		zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3588
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3589
	/* Must have a non-zero generation number to distinguish from .zfs */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3590
	if (gen == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3591
		gen = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3592
	for (i = 0; i < sizeof (zfid->zf_gen); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3593
		zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3594
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3595
	if (size == LONG_FID_LEN) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3596
		uint64_t	objsetid = dmu_objset_id(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3597
		zfid_long_t	*zlfid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3598
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3599
		zlfid = (zfid_long_t *)fidp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3600
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3601
		for (i = 0; i < sizeof (zlfid->zf_setid); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3602
			zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3603
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3604
		/* XXX - this should be the generation number for the objset */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3605
		for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3606
			zlfid->zf_setgen[i] = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3607
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3608
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3609
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3610
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3611
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3612
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3613
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3614
zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3615
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3616
	znode_t		*zp, *xzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3617
	zfsvfs_t	*zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3618
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3619
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3620
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3621
	switch (cmd) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3622
	case _PC_LINK_MAX:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3623
		*valp = ULONG_MAX;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3624
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3625
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3626
	case _PC_FILESIZEBITS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3627
		*valp = 64;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3628
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3629
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3630
	case _PC_XATTR_EXISTS:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3631
		zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3632
		zfsvfs = zp->z_zfsvfs;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3633
		ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3634
		*valp = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3635
		error = zfs_dirent_lock(&dl, zp, "", &xzp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3636
		    ZXATTR | ZEXISTS | ZSHARED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3637
		if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3638
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3639
			if (!zfs_dirempty(xzp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3640
				*valp = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3641
			VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3642
		} else if (error == ENOENT) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3643
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3644
			 * If there aren't extended attributes, it's the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3645
			 * same as having zero of them.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3646
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3647
			error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3648
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3649
		ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3650
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3651
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3652
	case _PC_ACL_ENABLED:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3653
		*valp = _ACL_ACE_ENABLED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3654
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3655
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3656
	case _PC_MIN_HOLE_SIZE:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3657
		*valp = (ulong_t)SPA_MINBLOCKSIZE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3658
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3659
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3660
	default:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3661
		return (fs_pathconf(vp, cmd, valp, cr));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3662
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3663
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3664
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3665
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3666
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3667
zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3668
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3669
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3670
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3671
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3672
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3673
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3674
	error = zfs_getacl(zp, vsecp, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3675
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3676
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3677
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3678
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3679
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3680
/*ARGSUSED*/
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3681
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3682
zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3683
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3684
	znode_t *zp = VTOZ(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3685
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3686
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3687
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 4863
diff changeset
  3688
	ZFS_ENTER_VERIFY_ZP(zfsvfs, zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3689
	error = zfs_setacl(zp, vsecp, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3690
	ZFS_EXIT(zfsvfs);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3691
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3692
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3693
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3694
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3695
 * Predeclare these here so that the compiler assumes that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3696
 * this is an "old style" function declaration that does
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3697
 * not include arguments => we won't get type mismatch errors
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3698
 * in the initializations that follow.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3699
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3700
static int zfs_inval();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3701
static int zfs_isdir();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3702
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3703
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3704
zfs_inval()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3705
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3706
	return (EINVAL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3707
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3708
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3709
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3710
zfs_isdir()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3711
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3712
	return (EISDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3713
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3714
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3715
 * Directory vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3716
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3717
vnodeops_t *zfs_dvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3718
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
  3719
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3720
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3721
	VOPNAME_READ,		{ .error = zfs_isdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3722
	VOPNAME_WRITE,		{ .error = zfs_isdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3723
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3724
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3725
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3726
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3727
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3728
	VOPNAME_CREATE,		{ .vop_create = zfs_create },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3729
	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3730
	VOPNAME_LINK,		{ .vop_link = zfs_link },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3731
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3732
	VOPNAME_MKDIR,		{ .vop_mkdir = zfs_mkdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3733
	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3734
	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3735
	VOPNAME_SYMLINK,	{ .vop_symlink = zfs_symlink },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3736
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3737
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3738
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3739
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3740
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3741
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3742
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
4863
7b14ad153d91 PSARC/2007/027 File Events Notification API
praks
parents: 4813
diff changeset
  3743
	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
  3744
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3745
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3746
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3747
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3748
 * Regular file vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3749
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3750
vnodeops_t *zfs_fvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3751
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
  3752
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3753
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3754
	VOPNAME_READ,		{ .vop_read = zfs_read },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3755
	VOPNAME_WRITE,		{ .vop_write = zfs_write },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3756
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3757
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3758
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3759
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3760
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3761
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3762
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3763
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3764
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3765
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3766
	VOPNAME_FRLOCK,		{ .vop_frlock = zfs_frlock },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3767
	VOPNAME_SPACE,		{ .vop_space = zfs_space },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3768
	VOPNAME_GETPAGE,	{ .vop_getpage = zfs_getpage },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3769
	VOPNAME_PUTPAGE,	{ .vop_putpage = zfs_putpage },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3770
	VOPNAME_MAP,		{ .vop_map = zfs_map },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3771
	VOPNAME_ADDMAP,		{ .vop_addmap = zfs_addmap },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3772
	VOPNAME_DELMAP,		{ .vop_delmap = zfs_delmap },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3773
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3774
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3775
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3776
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3777
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3778
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3779
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3780
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3781
 * Symbolic link vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3782
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3783
vnodeops_t *zfs_symvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3784
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
  3785
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3786
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3787
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3788
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3789
	VOPNAME_READLINK,	{ .vop_readlink = zfs_readlink },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3790
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3791
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3792
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3793
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3794
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3795
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3796
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3797
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3798
 * Extended attribute directory vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3799
 *	This template is identical to the directory vnodes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3800
 *	operation template except for restricted operations:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3801
 *		VOP_MKDIR()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3802
 *		VOP_SYMLINK()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3803
 * Note that there are other restrictions embedded in:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3804
 *	zfs_create()	- restrict type to VREG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3805
 *	zfs_link()	- no links into/out of attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3806
 *	zfs_rename()	- no moves into/out of attribute space
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3807
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3808
vnodeops_t *zfs_xdvnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3809
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
  3810
	VOPNAME_OPEN,		{ .vop_open = zfs_open },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3811
	VOPNAME_CLOSE,		{ .vop_close = zfs_close },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3812
	VOPNAME_IOCTL,		{ .vop_ioctl = zfs_ioctl },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3813
	VOPNAME_GETATTR,	{ .vop_getattr = zfs_getattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3814
	VOPNAME_SETATTR,	{ .vop_setattr = zfs_setattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3815
	VOPNAME_ACCESS,		{ .vop_access = zfs_access },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3816
	VOPNAME_LOOKUP,		{ .vop_lookup = zfs_lookup },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3817
	VOPNAME_CREATE,		{ .vop_create = zfs_create },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3818
	VOPNAME_REMOVE,		{ .vop_remove = zfs_remove },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3819
	VOPNAME_LINK,		{ .vop_link = zfs_link },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3820
	VOPNAME_RENAME,		{ .vop_rename = zfs_rename },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3821
	VOPNAME_MKDIR,		{ .error = zfs_inval },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3822
	VOPNAME_RMDIR,		{ .vop_rmdir = zfs_rmdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3823
	VOPNAME_READDIR,	{ .vop_readdir = zfs_readdir },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3824
	VOPNAME_SYMLINK,	{ .error = zfs_inval },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3825
	VOPNAME_FSYNC,		{ .vop_fsync = zfs_fsync },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3826
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3827
	VOPNAME_FID,		{ .vop_fid = zfs_fid },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3828
	VOPNAME_SEEK,		{ .vop_seek = zfs_seek },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3829
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3830
	VOPNAME_GETSECATTR,	{ .vop_getsecattr = zfs_getsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3831
	VOPNAME_SETSECATTR,	{ .vop_setsecattr = zfs_setsecattr },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3832
	VOPNAME_VNEVENT,	{ .vop_vnevent = fs_vnevent_support },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3833
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3834
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3835
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3836
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3837
 * Error vnode operations template
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3838
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3839
vnodeops_t *zfs_evnodeops;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3840
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
  3841
	VOPNAME_INACTIVE,	{ .vop_inactive = zfs_inactive },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3842
	VOPNAME_PATHCONF,	{ .vop_pathconf = zfs_pathconf },
c788126f2a20 PSARC/2007/124 Strong Type-Checking for VFS Operation Registration Mechanism
rsb
parents: 3897
diff changeset
  3843
	NULL,			NULL
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  3844
};