usr/src/uts/common/fs/zfs/zfs_dir.c
author amw
Thu, 25 Oct 2007 16:34:29 -0700
changeset 5331 3047ad28a67b
parent 4577 ed36b0e652bc
child 5446 51fbc14b301d
permissions -rw-r--r--
PSARC/2007/218 caller_context_t in all VOPs PSARC/2007/227 VFS Feature Registration and ACL on Create PSARC/2007/244 ZFS Case-insensitive support PSARC/2007/315 Extensible Attribute Interfaces PSARC/2007/394 ls(1) new command line options '-/' and '-%': CIFS system attributes support PSARC/2007/403 Modified Access Checks for CIFS PSARC/2007/410 Add system attribute support to chmod(1) PSARC/2007/432 CIFS system attributes support for cp(1), pack(1), unpack(1), compress(1) and uncompress(1) PSARC/2007/444 Rescind SETTABLE Attribute PSARC/2007/459 CIFS system attributes support for cpio(1), pax(1), tar(1) PSARC/2007/546 Update utilities to match CIFS system attributes changes. PSARC/2007/560 ZFS sharesmb property 4890717 want append-only files 6417428 Case-insensitive file system name lookup to support CIFS 6417435 DOS attributes and additional timestamps to support for CIFS 6417442 File system quarantined and modified attributes to support an integrated Anti-Virus service 6417453 FS boolean property for rejecting/allowing invalid UTF-8 sequences in file names 6473733 RFE: Need support for open-deny modes 6473755 RFE: Need ability to reconcile oplock and delegation conflicts 6494624 sharemgr needs to support CIFS shares better 6546705 All vnode operations need to pass caller_context_t 6546706 Need VOP_SETATTR/VOP_GETATTR to support new, optional attributes 6546893 Solaris system attribute support 6550962 ZFS ACL inheritance needs to be enhanced to support Automatic Inheritance 6553589 RFE: VFS Feature Registration facility 6553770 RFE: ZFS support for ACL-on-CREATE (PSARC 2007/227) 6565581 ls(1) should support file system attributes proposed in PSARC/2007/315 6566784 NTFS streams are not copied along with the files. 6576205 cp(1), pack(1) and compress(1) should support file system attributes proposed in PSARC/2007/315 6578875 RFE: kernel interfaces for nbmand need improvement 6578883 RFE: VOP_SHRLOCK needs additional access types 6578885 chmod(1) should support file system attributes proposed in PSARC/2007/315 6578886 RFE: disallow nbmand state to change on remount 6583349 ACL parser needs to support audit/alarm ACE types 6590347 tar(1) should support filesystem attributes proposed in PSARC/2007/315 6597357 *tar* xv@ doesn't show the hidden directory even though it is restored 6597360 *tar* should re-init xattr info if openat() fails during extraction of and extended attribute 6597368 *tar* cannot restore hard linked extended attributes 6597374 *tar* doesn't display "x " when hard linked attributes are restored 6597375 *tar* extended attribute header off by one 6614861 *cpio* incorrectly archives extended system attributes with -@ 6614896 *pax* incorrectly archives extended system attributes with -@ 6615225 *tar* incorrectly archives extended system attributes with -@ 6617183 CIFS Service - PSARC 2006/715
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
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
     5
 * Common Development and Distribution License (the "License").
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
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
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
#include <sys/types.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/param.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/time.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/systm.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/sysmacros.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/resource.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/vfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/vnode.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/file.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/mode.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/kmem.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/uio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <sys/pathname.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
#include <sys/cmn_err.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
#include <sys/errno.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
#include <sys/stat.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
#include <sys/unistd.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
#include <sys/random.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
#include <sys/policy.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
#include <sys/zfs_dir.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
#include <sys/zfs_acl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
#include <sys/fs/zfs.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
#include "fs/fs_subr.h"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
#include <sys/zap.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
#include <sys/atomic.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
#include <sys/zfs_ctldir.h>
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    55
#include <sys/zfs_fuid.h>
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
    56
#include <sys/dnlc.h>
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    57
#include <sys/extdirent.h>
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    58
#include <sys/zfs_i18n.h>
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    59
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    60
/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    61
 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    62
 * of names after deciding which is the appropriate lookup interface.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    63
 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    64
static int
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    65
zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    66
    boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    67
{
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    68
	int error;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    69
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    70
	if (zfsvfs->z_norm) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    71
		matchtype_t mt = MT_FIRST;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    72
		boolean_t conflict = B_FALSE;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    73
		size_t bufsz = 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    74
		char *buf = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    75
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    76
		if (rpnp) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    77
			buf = rpnp->pn_path;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    78
			bufsz = rpnp->pn_bufsize;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    79
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    80
		if (exact)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    81
			mt = MT_EXACT;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    82
		/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    83
		 * In the non-mixed case we only expect there would ever
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    84
		 * be one match, but we need to use the normalizing lookup.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    85
		 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    86
		error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    87
		    zoid, mt, buf, bufsz, &conflict);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    88
		if (deflags)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    89
			*deflags = conflict ? ED_CASE_CONFLICT : 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    90
	} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    91
		error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    92
	}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    93
	*zoid = ZFS_DIRENT_OBJ(*zoid);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    94
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    95
	if (error == ENOENT && update)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    96
		dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    97
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    98
	return (error);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
    99
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
 * Lock a directory entry.  A dirlock on <dzp, name> protects that name
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
 * in dzp's directory zap object.  As long as you hold a dirlock, you can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
 * can change the zap entry for (i.e. link or unlink) this name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
 * Input arguments:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
 *	dzp	- znode for directory
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
 *	name	- name of entry to lock
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
 *	flag	- ZNEW: if the entry already exists, fail with EEXIST.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
 *		  ZEXISTS: if the entry does not exist, fail with ENOENT.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
 *		  ZSHARED: allow concurrent access with other ZSHARED callers.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
 *		  ZXATTR: we want dzp's xattr directory
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   114
 *		  ZCILOOK: On a mixed sensitivity file system,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   115
 *			   this lookup should be case-insensitive.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   116
 *		  ZCIEXACT: On a purely case-insensitive file system,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   117
 *			    this lookup should be case-sensitive.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   118
 *		  ZRENAMING: we are locking for renaming, force narrow locks
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
 * Output arguments:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
 *	zpp	- pointer to the znode for the entry (NULL if there isn't one)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
 *	dlpp	- pointer to the dirlock for this entry (NULL on error)
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   123
 *      direntflags - (case-insensitive lookup only)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   124
 *		flags if multiple case-sensitive matches exist in directory
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   125
 *      realpnp     - (case-insensitive lookup only)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   126
 *		actual name matched within the directory
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
 * Return value: 0 on success or errno on failure.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
 * NOTE: Always checks for, and rejects, '.' and '..'.
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   131
 * NOTE: For case-insensitive file systems we take wide locks (see below),
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   132
 *	 but return znode pointers to a single match.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   136
    int flag, int *direntflags, pathname_t *realpnp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
	zfs_dirlock_t	*dl;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   140
	boolean_t	update;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   141
	boolean_t	exact;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	uint64_t	zoid;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   143
	vnode_t		*vp = NULL;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   144
	int		error = 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   145
	int		cmpflags;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
	*zpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
	*dlpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
	 * Verify that we are not trying to lock '.', '..', or '.zfs'
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
	if (name[0] == '.' &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
	    (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
	    zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
		return (EEXIST);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
	/*
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   159
	 * Case sensitivity and normalization preferences are set when
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   160
	 * the file system is created.  These are stored in the
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   161
	 * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   162
	 * affect what vnodes can be cached in the DNLC, how we
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   163
	 * perform zap lookups, and the "width" of our dirlocks.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   164
	 *
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   165
	 * A normal dirlock locks a single name.  Note that with
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   166
	 * normalization a name can be composed multiple ways, but
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   167
	 * when normalized, these names all compare equal.  A wide
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   168
	 * dirlock locks multiple names.  We need these when the file
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   169
	 * system is supporting mixed-mode access.  It is sometimes
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   170
	 * necessary to lock all case permutations of file name at
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   171
	 * once so that simultaneous case-insensitive/case-sensitive
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   172
	 * behaves as rationally as possible.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   173
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   174
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   175
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   176
	 * Decide if exact matches should be requested when performing
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   177
	 * a zap lookup on file systems supporting case-insensitive
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   178
	 * access.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   179
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   180
	exact = ((zfsvfs->z_case & ZFS_CI_ONLY) && (flag & ZCIEXACT)) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   181
	    ((zfsvfs->z_case & ZFS_CI_MIXD) && !(flag & ZCILOOK));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   182
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   183
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   184
	 * Only look in or update the DNLC if we are looking for the
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   185
	 * name on a file system that does not require normalization
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   186
	 * or case folding.  We can also look there if we happen to be
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   187
	 * on a non-normalizing, mixed sensitivity file system IF we
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   188
	 * are looking for the exact name.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   189
	 *
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   190
	 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   191
	 * case for performance improvement?
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   192
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   193
	update = !zfsvfs->z_norm ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   194
	    ((zfsvfs->z_case & ZFS_CI_MIXD) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   195
	    !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   196
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   197
	/*
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   198
	 * ZRENAMING indicates we are in a situation where we should
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   199
	 * take narrow locks regardless of the file system's
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   200
	 * preferences for normalizing and case folding.  This will
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   201
	 * prevent us deadlocking trying to grab the same wide lock
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   202
	 * twice if the two names happen to be case-insensitive
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   203
	 * matches.
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   204
	 */
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   205
	if (flag & ZRENAMING)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   206
		cmpflags = 0;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   207
	else
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   208
		cmpflags = zfsvfs->z_norm;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   209
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   210
	/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
	 * Wait until there are no locks on this name.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
	 */
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3713
diff changeset
   213
	rw_enter(&dzp->z_name_lock, RW_READER);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
	mutex_enter(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
	for (;;) {
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   216
		if (dzp->z_unlinked) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
			mutex_exit(&dzp->z_lock);
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3713
diff changeset
   218
			rw_exit(&dzp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
			return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   221
		for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   222
			if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   223
			    U8_UNICODE_LATEST, &error) == 0) || error != 0)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
				break;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   225
		}
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   226
		if (error != 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   227
			mutex_exit(&dzp->z_lock);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   228
			rw_exit(&dzp->z_name_lock);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   229
			return (ENOENT);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   230
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
		if (dl == NULL)	{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
			 * Allocate a new dirlock and add it to the list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
			dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
			cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
			dl->dl_name = name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
			dl->dl_sharecnt = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
			dl->dl_namesize = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
			dl->dl_dzp = dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
			dl->dl_next = dzp->z_dirlocks;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
			dzp->z_dirlocks = dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
		if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
			break;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
		cv_wait(&dl->dl_cv, &dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
	if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
		 * We're the second shared reference to dl.  Make a copy of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
		 * dl_name in case the first thread goes away before we do.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
		 * Note that we initialize the new name before storing its
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
		 * pointer into dl_name, because the first thread may load
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
		 * dl->dl_name at any time.  He'll either see the old value,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
		 * which is his, or the new shared copy; either is OK.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
		dl->dl_namesize = strlen(dl->dl_name) + 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
		name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
		bcopy(dl->dl_name, name, dl->dl_namesize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
		dl->dl_name = name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
	mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
	 * We have a dirlock on the name.  (Note that it is the dirlock,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
	 * not the dzp's z_lock, that protects the name in the zap object.)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
	 * See if there's an object by this name; if so, put a hold on it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	if (flag & ZXATTR) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
		zoid = dzp->z_phys->zp_xattr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
		error = (zoid == 0 ? ENOENT : 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
	} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   276
		if (update)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   277
			vp = dnlc_lookup(ZTOV(dzp), name);
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   278
		if (vp == DNLC_NO_VNODE) {
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   279
			VN_RELE(vp);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   280
			error = ENOENT;
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   281
		} else if (vp) {
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   282
			if (flag & ZNEW) {
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   283
				zfs_dirent_unlock(dl);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   284
				VN_RELE(vp);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   285
				return (EEXIST);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   286
			}
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   287
			*dlpp = dl;
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   288
			*zpp = VTOZ(vp);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   289
			return (0);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   290
		} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   291
			error = zfs_match_find(zfsvfs, dzp, name, exact,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   292
			    update, direntflags, realpnp, &zoid);
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   293
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
	if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
		if (error != ENOENT || (flag & ZEXISTS)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
		if (flag & ZNEW) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
			return (EEXIST);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
		error = zfs_zget(zfsvfs, zoid, zpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
			zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   310
		if (!(flag & ZXATTR) && update)
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   311
			dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
	*dlpp = dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
 * Unlock this directory entry and wake anyone who was waiting for it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
zfs_dirent_unlock(zfs_dirlock_t *dl)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
	znode_t *dzp = dl->dl_dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
	zfs_dirlock_t **prev_dl, *cur_dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
	mutex_enter(&dzp->z_lock);
3897
278bade789ba 6437750 panic: db->db_buf==0||arc_referenced(db->db_buf), file: dbuf.c,line:1539
maybee
parents: 3713
diff changeset
   329
	rw_exit(&dzp->z_name_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
	if (dl->dl_sharecnt > 1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
		dl->dl_sharecnt--;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
		mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
	prev_dl = &dzp->z_dirlocks;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
	while ((cur_dl = *prev_dl) != dl)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
		prev_dl = &cur_dl->dl_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
	*prev_dl = dl->dl_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
	cv_broadcast(&dl->dl_cv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
	mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
	if (dl->dl_namesize != 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
		kmem_free(dl->dl_name, dl->dl_namesize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
	cv_destroy(&dl->dl_cv);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
	kmem_free(dl, sizeof (*dl));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
 * Look up an entry in a directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
 * NOTE: '.' and '..' are handled as special cases because
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
 *	no directory entries are actually stored for them.  If this is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
 *	the root of a filesystem, then '.zfs' is also treated as a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
 *	special pseudo-directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
int
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   357
zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   358
    int *deflg, pathname_t *rpnp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
	zfs_dirlock_t *dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
	znode_t *zp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
	int error = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
	if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
		*vpp = ZTOV(dzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
		VN_HOLD(*vpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
	} else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
		zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
		 * If we are a snapshot mounted under .zfs, return
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
		 * the vp for the snapshot directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
		 */
1878
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1544
diff changeset
   373
		if (dzp->z_phys->zp_parent == dzp->z_id &&
c22df0f5603f 6413573 deadlock between fsflush() and zfs_create()
maybee
parents: 1544
diff changeset
   374
		    zfsvfs->z_parent != zfsvfs) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
			error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   376
			    "snapshot", vpp, NULL, 0, NULL, kcred,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   377
			    NULL, NULL, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
			return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
		rw_enter(&dzp->z_parent_lock, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
		error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
		if (error == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
			*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
		rw_exit(&dzp->z_parent_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
	} else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   386
		*vpp = zfsctl_root(dzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
	} else {
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   388
		int zf;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   389
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   390
		zf = ZEXISTS | ZSHARED;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   391
		if (flags & FIGNORECASE)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   392
			zf |= ZCILOOK;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   393
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   394
		error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
		if (error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
			*vpp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
			zfs_dirent_unlock(dl);
869
dc133b87dfb3 6297285 znode prefetching in zfs_readdir causes 5x performance degradation for 'ls'
perrin
parents: 789
diff changeset
   398
			dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
		}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   400
		rpnp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   403
	if ((flags & FIGNORECASE) && rpnp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   404
		(void) strlcpy(rpnp->pn_path, name, rpnp->pn_bufsize);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   405
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
static char *
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   410
zfs_unlinked_hexname(char namebuf[17], uint64_t x)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	char *name = &namebuf[16];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
	const char digits[16] = "0123456789abcdef";
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
	*name = '\0';
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
	do {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
		*--name = digits[x & 0xf];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
		x >>= 4;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
	} while (x != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
	return (name);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   424
/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   425
 * unlinked Set (formerly known as the "delete queue") Error Handling
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   426
 *
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   427
 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   428
 * don't specify the name of the entry that we will be manipulating.  We
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   429
 * also fib and say that we won't be adding any new entries to the
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   430
 * unlinked set, even though we might (this is to lower the minimum file
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   431
 * size that can be deleted in a full filesystem).  So on the small
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   432
 * chance that the nlink list is using a fat zap (ie. has more than
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   433
 * 2000 entries), we *may* not pre-read a block that's needed.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   434
 * Therefore it is remotely possible for some of the assertions
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   435
 * regarding the unlinked set below to fail due to i/o error.  On a
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   436
 * nondebug system, this will result in the space being leaked.
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   437
 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
void
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   439
zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
	char obj_name[17];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   445
	ASSERT(zp->z_unlinked);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
	ASSERT3U(zp->z_phys->zp_links, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   448
	error = zap_add(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   449
	    zfs_unlinked_hexname(obj_name, zp->z_id), 8, 1, &zp->z_id, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
	ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   454
 * Clean up any znodes that had no links when we either crashed or
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   455
 * (force) umounted the file system.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   456
 */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   457
void
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   458
zfs_unlinked_drain(zfsvfs_t *zfsvfs)
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   459
{
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   460
	zap_cursor_t	zc;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   461
	zap_attribute_t zap;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   462
	dmu_object_info_t doi;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   463
	znode_t		*zp;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   464
	int		error;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   465
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   466
	/*
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   467
	 * Interate over the contents of the unlinked set.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   468
	 */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   469
	for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   470
	    zap_cursor_retrieve(&zc, &zap) == 0;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   471
	    zap_cursor_advance(&zc)) {
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   472
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   473
		/*
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   474
		 * See what kind of object we have in list
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   475
		 */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   476
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   477
		error = dmu_object_info(zfsvfs->z_os,
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   478
		    zap.za_first_integer, &doi);
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   479
		if (error != 0)
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   480
			continue;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   481
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   482
		ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   483
		    (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   484
		/*
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   485
		 * We need to re-mark these list entries for deletion,
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   486
		 * so we pull them back into core and set zp->z_unlinked.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   487
		 */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   488
		error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   489
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   490
		/*
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   491
		 * We may pick up znodes that are already marked for deletion.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   492
		 * This could happen during the purge of an extended attribute
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   493
		 * directory.  All we need to do is skip over them, since they
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   494
		 * are already in the system marked z_unlinked.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   495
		 */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   496
		if (error != 0)
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   497
			continue;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   498
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   499
		zp->z_unlinked = B_TRUE;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   500
		VN_RELE(ZTOV(zp));
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   501
	}
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   502
	zap_cursor_fini(&zc);
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   503
}
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   504
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   505
/*
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
 * Delete the entire contents of a directory.  Return a count
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
 * of the number of entries that could not be deleted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
 * NOTE: this function assumes that the directory is inactive,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
 *	so there is no need to lock its entries before deletion.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
 *	Also, it assumes the directory contents is *only* regular
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
 *	files.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
zfs_purgedir(znode_t *dzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
	zap_cursor_t	zc;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
	zap_attribute_t	zap;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
	znode_t		*xzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
	zfsvfs_t	*zfsvfs = dzp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
	zfs_dirlock_t	dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
	int skipped = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
	    (error = zap_cursor_retrieve(&zc, &zap)) == 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
	    zap_cursor_advance(&zc)) {
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3897
diff changeset
   529
		error = zfs_zget(zfsvfs,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3897
diff changeset
   530
		    ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
		ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
		ASSERT((ZTOV(xzp)->v_type == VREG) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
		    (ZTOV(xzp)->v_type == VLNK));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
		tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
		dmu_tx_hold_bonus(tx, dzp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   538
		dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
		dmu_tx_hold_bonus(tx, xzp->z_id);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   540
		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
		error = dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
		if (error) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
			dmu_tx_abort(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
			VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
			skipped += 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
		bzero(&dl, sizeof (dl));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
		dl.dl_dzp = dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
		dl.dl_name = zap.za_name;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
		error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
		ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
		dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
		VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
	}
885
d925b21dba78 6347493 tar of 25K empty directory entries in ZFS takes 30+ seconds ...
ahrens
parents: 869
diff changeset
   558
	zap_cursor_fini(&zc);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
	ASSERT(error == ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
	return (skipped);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
zfs_rmnode(znode_t *zp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
	objset_t	*os = zfsvfs->z_os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
	znode_t		*xzp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
	char		obj_name[17];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
	dmu_tx_t	*tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
	uint64_t	acl_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
	ASSERT(ZTOV(zp)->v_count == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
	ASSERT(zp->z_phys->zp_links == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
	 * If this is an attribute directory, purge its contents.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
	 */
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   580
	if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
		if (zfs_purgedir(zp) != 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
			/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   583
			 * Not enough space to delete some xattrs.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   584
			 * Leave it on the unlinked set.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
			return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
		}
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   588
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
	/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   591
	 * If the file has extended attributes, we're going to unlink
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   592
	 * the xattr dir.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
	if (zp->z_phys->zp_xattr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
		error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
		ASSERT(error == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
	acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
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
	 * Set up the transaction.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
	tx = dmu_tx_create(os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
	dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   606
	dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
	if (xzp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
		dmu_tx_hold_bonus(tx, xzp->z_id);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   609
		dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
	if (acl_obj)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
		dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
	error = dmu_tx_assign(tx, TXG_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
	if (error) {
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   615
		/*
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   616
		 * Not enough space to delete the file.  Leave it in the
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   617
		 * unlinked set, leaking it until the fs is remounted (at
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   618
		 * which point we'll call zfs_unlinked_drain() to process it).
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   619
		 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
		dmu_tx_abort(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
	if (xzp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
		dmu_buf_will_dirty(xzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
		mutex_enter(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   627
		xzp->z_unlinked = B_TRUE;	/* mark xzp for deletion */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
		xzp->z_phys->zp_links = 0;	/* no more links to it */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
		mutex_exit(&xzp->z_lock);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   630
		zfs_unlinked_add(xzp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   633
	/* Remove this znode from the unlinked set */
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   634
	error = zap_remove(os, zfsvfs->z_unlinkedobj,
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   635
	    zfs_unlinked_hexname(obj_name, zp->z_id), tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
	ASSERT3U(error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	zfs_znode_delete(zp, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
	if (xzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
		VN_RELE(ZTOV(xzp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   646
static uint64_t
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   647
zfs_dirent(znode_t *zp)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   648
{
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   649
	uint64_t de = zp->z_id;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   650
	if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   651
		de |= IFTODT((zp)->z_phys->zp_mode) << 60;
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   652
	return (de);
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   653
}
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   654
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   656
 * Link zp into dl.  Can only fail if zp has been unlinked.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	znode_t *dzp = dl->dl_dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
	vnode_t *vp = ZTOV(zp);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3897
diff changeset
   663
	uint64_t value;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
	int zp_is_dir = (vp->v_type == VDIR);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
	dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
	mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	if (!(flag & ZRENAMING)) {
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   671
		if (zp->z_unlinked) {	/* no new links to unlinked zp */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
			ASSERT(!(flag & (ZNEW | ZEXISTS)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
			mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
			return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
		zp->z_phys->zp_links++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
	zp->z_phys->zp_parent = dzp->z_id;	/* dzp is now zp's parent */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
	if (!(flag & ZNEW))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
		zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
	dmu_buf_will_dirty(dzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
	mutex_enter(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
	dzp->z_phys->zp_size++;			/* one dirent added */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
	dzp->z_phys->zp_links += zp_is_dir;	/* ".." link from zp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
	mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 3912
diff changeset
   691
	value = zfs_dirent(zp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
	error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3897
diff changeset
   693
	    8, 1, &value, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
	ASSERT(error == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   696
	dnlc_update(ZTOV(dzp), dl->dl_name, vp);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   697
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
/*
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   702
 * Unlink zp from dl, and mark zp for deletion if this was the last link.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   704
 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   705
 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
 * and it's the caller's job to do it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   710
	boolean_t *unlinkedp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
	znode_t *dzp = dl->dl_dzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
	vnode_t *vp = ZTOV(zp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
	int zp_is_dir = (vp->v_type == VDIR);
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   715
	boolean_t unlinked = B_FALSE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
	int error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
1484
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   718
	dnlc_remove(ZTOV(dzp), dl->dl_name);
d330e98f8ed7 6350001 ZFS lookup performance still much slower than UFS : help tar : help spec SFS
ek110237
parents: 1231
diff changeset
   719
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
	if (!(flag & ZRENAMING)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
		dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
		if (vn_vfswlock(vp))		/* prevent new mounts on zp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
			return (EBUSY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
		if (vn_ismntpt(vp)) {		/* don't remove mount point */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
			vn_vfsunlock(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
			return (EBUSY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
		mutex_enter(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
		if (zp_is_dir && !zfs_dirempty(zp)) {	/* dir not empty */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
			mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
			vn_vfsunlock(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
			return (EEXIST);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
		}
3713
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   737
		if (zp->z_phys->zp_links <= zp_is_dir) {
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   738
			zfs_panic_recover("zfs: link count on %s is %u, "
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   739
			    "should be at least %u",
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   740
			    zp->z_vnode->v_path ? zp->z_vnode->v_path :
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   741
			    "<unknown>", (int)zp->z_phys->zp_links,
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   742
			    zp_is_dir + 1);
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   743
			zp->z_phys->zp_links = zp_is_dir + 1;
00e75dc8b749 6527325 want more assertions in space map code
ahrens
parents: 3461
diff changeset
   744
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
		if (--zp->z_phys->zp_links == zp_is_dir) {
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   746
			zp->z_unlinked = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
			zp->z_phys->zp_links = 0;
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   748
			unlinked = B_TRUE;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
			zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
		mutex_exit(&zp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
		vn_vfsunlock(vp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
	dmu_buf_will_dirty(dzp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
	mutex_enter(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
	dzp->z_phys->zp_size--;			/* one dirent removed */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
	dzp->z_phys->zp_links -= zp_is_dir;	/* ".." link from zp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
	zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
	mutex_exit(&dzp->z_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   763
	if (zp->z_zfsvfs->z_norm) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   764
		if (((zp->z_zfsvfs->z_case & ZFS_CI_ONLY) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   765
		    (flag & ZCIEXACT)) ||
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   766
		    ((zp->z_zfsvfs->z_case & ZFS_CI_MIXD) &&
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   767
		    !(flag & ZCILOOK)))
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   768
			error = zap_remove_norm(zp->z_zfsvfs->z_os,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   769
			    dzp->z_id, dl->dl_name, MT_EXACT, tx);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   770
		else
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   771
			error = zap_remove_norm(zp->z_zfsvfs->z_os,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   772
			    dzp->z_id, dl->dl_name, MT_FIRST, tx);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   773
	} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   774
		error = zap_remove(zp->z_zfsvfs->z_os,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   775
		    dzp->z_id, dl->dl_name, tx);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   776
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	ASSERT(error == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
3461
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   779
	if (unlinkedp != NULL)
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   780
		*unlinkedp = unlinked;
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   781
	else if (unlinked)
c19b22f347d6 6514331 in-memory delete queue is not needed
ahrens
parents: 3280
diff changeset
   782
		zfs_unlinked_add(zp, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
 * Indicate whether the directory is empty.  Works with or without z_lock
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
 * held, but can only be consider a hint in the latter case.  Returns true
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
 * if only "." and ".." remain and there's no work in progress.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
boolean_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
zfs_dirempty(znode_t *dzp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
	return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
	zfsvfs_t *zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
	znode_t *xzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
	dmu_tx_t *tx;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
	uint64_t xoid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	int error;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   806
	zfs_fuid_info_t *fuidp = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
	*xvpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   810
	if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
	tx = dmu_tx_create(zfsvfs->z_os);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	dmu_tx_hold_bonus(tx, zp->z_id);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 1484
diff changeset
   815
	dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   816
	if (zfsvfs->z_fuid_obj == 0) {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   817
		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   818
		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   819
		dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   820
	} else {
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   821
		dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   822
		dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, SPA_MAXBLOCKSIZE);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   823
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
	error = dmu_tx_assign(tx, zfsvfs->z_assign);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
	if (error) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1878
diff changeset
   826
		if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT)
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1878
diff changeset
   827
			dmu_tx_wait(tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
		dmu_tx_abort(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
	}
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   831
	zfs_mknode(zp, vap, &xoid, tx, cr, IS_XATTR, &xzp, 0, NULL, &fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
	ASSERT(xzp->z_id == xoid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
	ASSERT(xzp->z_phys->zp_parent == zp->z_id);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
	dmu_buf_will_dirty(zp->z_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
	zp->z_phys->zp_xattr = xoid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   837
	(void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   838
	    xzp, "", NULL, fuidp, vap);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   839
	if (fuidp)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   840
		zfs_fuid_info_free(fuidp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
	dmu_tx_commit(tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
	*xvpp = ZTOV(xzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
 * Return a znode for the extended attribute directory for zp.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
 * ** If the directory does not already exist, it is created **
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
 *	IN:	zp	- znode to obtain attribute directory from
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
 *		cr	- credentials of caller
3280
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   854
 *		flags	- flags from the VOP_LOOKUP call
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
 *	OUT:	xzpp	- pointer to extended attribute znode
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
 *	RETURN:	0 on success
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
 *		error number on failure
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
int
3280
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   862
zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	zfsvfs_t	*zfsvfs = zp->z_zfsvfs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
	znode_t		*xzp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	zfs_dirlock_t	*dl;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
	vattr_t		va;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
	int		error;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
top:
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   870
	error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
	if (error)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
		return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
	if (xzp != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
		*xvpp = ZTOV(xzp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   876
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
	ASSERT(zp->z_phys->zp_xattr == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
3280
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   882
	if (!(flags & CREATE_XATTR_DIR)) {
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   883
		zfs_dirent_unlock(dl);
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   884
		return (ENOENT);
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   885
	}
e93ccc27c51d 6492686 NFSv4 client got EACCES over ZFS when trying to OPENATTR without createdir
ck153898
parents: 2597
diff changeset
   886
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
	if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
		zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
		return (EROFS);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   893
	 * The ability to 'create' files in an attribute
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
	 * directory comes from the write_xattr permission on the base file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
	 * The ability to 'search' an attribute directory requires
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
	 * read_xattr permission on the base file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
	 * Once in a directory the ability to read/write attributes
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
	 * is controlled by the permissions on the attribute file.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
	va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   903
	va.va_type = VDIR;
1231
64215f768e86 6354804 The file's ACL was changed when cp it from one ZFS file system to another one.
marks
parents: 885
diff changeset
   904
	va.va_mode = S_IFDIR | S_ISVTX | 0777;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   905
	zfs_fuid_map_ids(zp, &va.va_uid, &va.va_gid);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   906
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   907
	error = zfs_make_xattrdir(zp, &va, xvpp, cr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   908
	zfs_dirent_unlock(dl);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   909
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   910
	if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
2113
0510bb40c993 6430121 3-way deadlock involving tc_lock within zfs
ahrens
parents: 1878
diff changeset
   911
		/* NB: we already did dmu_tx_wait() if necessary */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   912
		goto top;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   914
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
	return (error);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   916
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   917
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   918
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   919
 * Decide whether it is okay to remove within a sticky directory.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   920
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
 * In sticky directories, write access is not sufficient;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   922
 * you can remove entries from a directory only if:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   923
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
 *	you own the directory,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   925
 *	you own the entry,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
 *	the entry is a plain file and you have write access,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
 *	or you are privileged (checked in secpolicy...).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
 * The function returns 0 if remove access is granted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
	uid_t  		uid;
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   935
	uid_t		downer;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   936
	uid_t		fowner;
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   937
	zfsvfs_t	*zfsvfs = zdp->z_zfsvfs;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
	if (zdp->z_zfsvfs->z_assign >= TXG_INITIAL)	/* ZIL replay */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   942
	if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   943
		return (0);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   944
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   945
	zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, ZFS_OWNER, &downer);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   946
	zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, ZFS_OWNER, &fowner);
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   947
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   948
	if ((uid = crgetuid(cr)) == downer || uid == fowner ||
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
	    (ZTOV(zp)->v_type == VREG &&
5331
3047ad28a67b PSARC/2007/218 caller_context_t in all VOPs
amw
parents: 4577
diff changeset
   950
	    zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
		return (secpolicy_vnode_remove(cr));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
}