zfs_dir.c revision 7f6e3e7d4ebf3d6d45073854bef004ca26f8e918
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/sysmacros.h>
#include <sys/resource.h>
#include <sys/pathname.h>
#include <sys/zfs_ctldir.h>
/*
* Lock a directory entry. A dirlock on <dzp, name> protects that name
* in dzp's directory zap object. As long as you hold a dirlock, you can
* assume two things: (1) dzp cannot be reaped, and (2) no other thread
* can change the zap entry for (i.e. link or unlink) this name.
*
* Input arguments:
* dzp - znode for directory
* name - name of entry to lock
* flag - ZNEW: if the entry already exists, fail with EEXIST.
* ZEXISTS: if the entry does not exist, fail with ENOENT.
* ZSHARED: allow concurrent access with other ZSHARED callers.
* ZXATTR: we want dzp's xattr directory
*
* Output arguments:
* zpp - pointer to the znode for the entry (NULL if there isn't one)
* dlpp - pointer to the dirlock for this entry (NULL on error)
*
* Return value: 0 on success or errno on failure.
*
* NOTE: Always checks for, and rejects, '.' and '..'.
*/
int
int flag)
{
int error;
/*
* Verify that we are not trying to lock '.', '..', or '.zfs'
*/
if (name[0] == '.' &&
return (EEXIST);
/*
* Wait until there are no locks on this name.
*/
for (;;) {
return (ENOENT);
}
break;
/*
* Allocate a new dirlock and add it to the list.
*/
dl->dl_sharecnt = 0;
dl->dl_namesize = 0;
break;
}
break;
}
/*
* We're the second shared reference to dl. Make a copy of
* dl_name in case the first thread goes away before we do.
* Note that we initialize the new name before storing its
* pointer into dl_name, because the first thread may load
* dl->dl_name at any time. He'll either see the old value,
* which is his, or the new shared copy; either is OK.
*/
}
/*
* We have a dirlock on the name. (Note that it is the dirlock,
* not the dzp's z_lock, that protects the name in the zap object.)
* See if there's an object by this name; if so, put a hold on it.
*/
} else {
}
if (error) {
return (error);
}
} else {
return (EEXIST);
}
if (error) {
return (error);
}
}
return (0);
}
/*
* Unlock this directory entry and wake anyone who was waiting for it.
*/
void
{
dl->dl_sharecnt--;
return;
}
if (dl->dl_namesize != 0)
}
/*
* Look up an entry in a directory.
*
* NOTE: '.' and '..' are handled as special cases because
* no directory entries are actually stored for them. If this is
* the root of a filesystem, then '.zfs' is also treated as a
* special pseudo-directory.
*/
int
{
int error = 0;
/*
* If we are a snapshot mounted under .zfs, return
* the vp for the snapshot directory.
*/
return (error);
}
if (error == 0)
} else {
if (error == 0) {
}
}
return (error);
}
static char *
{
*name = '\0';
do {
x >>= 4;
} while (x != 0);
return (name);
}
void
{
char obj_name[17];
int error;
}
/*
* Delete the entire contents of a directory. Return a count
* of the number of entries that could not be deleted.
*
* NOTE: this function assumes that the directory is inactive,
* so there is no need to lock its entries before deletion.
* Also, it assumes the directory contents is *only* regular
* files.
*/
static int
{
int skipped = 0;
int error;
zap_cursor_advance(&zc)) {
if (error) {
skipped += 1;
continue;
}
}
return (skipped);
}
/*
* Special function to requeue the znodes for deletion that were
* in progress when we either crashed or umounted the file system.
*/
static void
{
int error;
/*
* Interate over the contents of the delete queue.
*/
zap_cursor_advance(&zc)) {
/*
* Need some helpers?
*/
return;
/*
* See what kind of object we have in queue
*/
if (error != 0)
continue;
/*
* We need to re-mark these queue entries for reaping,
* so we pull them back into core and set zp->z_reap.
*/
/*
* We may pick up znodes that are already marked for reaping.
* This could happen during the purge of an extended attribute
* directory. All we need to do is skip over them, since they
* are already in the system to be processed by the taskq.
*/
if (error != 0) {
continue;
}
break;
}
}
void
zfs_delete_thread(void *arg)
{
}
continue;
}
if (--zd->z_znode_count == 0)
zfs_rmnode(zp);
}
if (--zd->z_thread_count == 0)
thread_exit();
}
/*
* Set the target number of delete threads to 'nthreads'.
* If nthreads == -1, choose a number based on current workload.
* If nthreads == 0, don't return until the threads have exited.
*/
int
{
if (nthreads == -1) {
if (zd->z_thread_target == 0) {
return (EBUSY);
}
}
zd->z_thread_count++;
}
}
return (0);
}
/*
* Wait until everything that's been queued has been deleted.
*/
void
{
}
}
void
{
char obj_name[17];
int error;
/*
* If this is an attribute directory, purge its contents.
*/
if (zfs_purgedir(zp) != 0) {
/*
* Add this back to the delete list to be retried later.
*
* XXX - this could just busy loop on us...
*/
delq->z_znode_count++;
return;
}
/*
* If the file has extended attributes, unlink the xattr dir.
*/
}
/*
* Set up the transaction.
*/
if (xzp) {
}
if (acl_obj)
if (error) {
/*
* Add this back to the delete list to be retried later.
*
* XXX - this could just busy loop on us...
*/
delq->z_znode_count++;
return;
}
if (xzp) {
}
/*
* Remove this znode from delete queue
*/
if (xzp)
}
/*
* Link zp into dl. Can only fail if zp has been reaped.
*/
int
{
int error;
return (ENOENT);
}
}
return (0);
}
/*
* Unlink zp from dl, and mark zp for reaping if this was the last link.
* Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
* If 'reaped_ptr' is NULL, we put reaped znodes on the delete queue.
* If it's non-NULL, we use it to indicate whether the znode needs reaping,
* and it's the caller's job to do it.
*/
int
int *reaped_ptr)
{
int reaped = 0;
int error;
return (EBUSY);
return (EBUSY);
}
return (EEXIST);
}
reaped = 1;
} else {
}
}
if (reaped_ptr != NULL)
*reaped_ptr = reaped;
else if (reaped)
return (0);
}
/*
* Indicate whether the directory is empty. Works with or without z_lock
* held, but can only be consider a hint in the latter case. Returns true
* if only "." and ".." remain and there's no work in progress.
*/
{
}
int
{
int error;
return (error);
if (error) {
return (error);
}
return (0);
}
/*
* Return a znode for the extended attribute directory for zp.
* ** If the directory does not already exist, it is created **
*
* IN: zp - znode to obtain attribute directory from
* cr - credentials of caller
*
* OUT: xzpp - pointer to extended attribute znode
*
* RETURN: 0 on success
* error number on failure
*/
int
{
int error;
top:
if (error)
return (error);
return (0);
}
return (EROFS);
}
/*
* The ability to 'create' files in an attribute
* directory comes from the write_xattr permission on the base file.
*
* The ability to 'search' an attribute directory requires
* read_xattr permission on the base file.
*
* is controlled by the permissions on the attribute file.
*/
goto top;
}
return (error);
}
/*
* Decide whether it is okay to remove within a sticky directory.
*
* In sticky directories, write access is not sufficient;
* you can remove entries from a directory only if:
*
* you own the directory,
* you own the entry,
* the entry is a plain file and you have write access,
* or you are privileged (checked in secpolicy...).
*
* The function returns 0 if remove access is granted.
*/
int
{
return (0);
return (0);
else
return (secpolicy_vnode_remove(cr));
}