]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zfs_dir.c
New upstream version 0.7.6
[mirror_zfs-debian.git] / module / zfs / zfs_dir.c
CommitLineData
34dc7c2f
BB
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
cae5b340 21
34dc7c2f 22/*
428870ff 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
cae5b340
AX
24 * Copyright (c) 2013, 2016 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
28#include <sys/types.h>
29#include <sys/param.h>
30#include <sys/time.h>
31#include <sys/systm.h>
32#include <sys/sysmacros.h>
33#include <sys/resource.h>
34#include <sys/vfs.h>
35#include <sys/vnode.h>
36#include <sys/file.h>
37#include <sys/mode.h>
38#include <sys/kmem.h>
39#include <sys/uio.h>
40#include <sys/pathname.h>
41#include <sys/cmn_err.h>
42#include <sys/errno.h>
43#include <sys/stat.h>
44#include <sys/unistd.h>
45#include <sys/sunddi.h>
46#include <sys/random.h>
47#include <sys/policy.h>
48#include <sys/zfs_dir.h>
49#include <sys/zfs_acl.h>
ea04106b 50#include <sys/zfs_vnops.h>
34dc7c2f
BB
51#include <sys/fs/zfs.h>
52#include "fs/fs_subr.h"
53#include <sys/zap.h>
54#include <sys/dmu.h>
55#include <sys/atomic.h>
ebe7e575 56#include <sys/zfs_ctldir.h>
34dc7c2f 57#include <sys/zfs_fuid.h>
428870ff
BB
58#include <sys/sa.h>
59#include <sys/zfs_sa.h>
34dc7c2f
BB
60#include <sys/dnlc.h>
61#include <sys/extdirent.h>
62
63/*
64 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
65 * of names after deciding which is the appropriate lookup interface.
66 */
67static int
cae5b340 68zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, matchtype_t mt,
34dc7c2f
BB
69 boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
70{
0c5dde49 71 boolean_t conflict = B_FALSE;
34dc7c2f
BB
72 int error;
73
cae5b340 74 if (zfsvfs->z_norm) {
34dc7c2f
BB
75 size_t bufsz = 0;
76 char *buf = NULL;
77
78 if (rpnp) {
79 buf = rpnp->pn_buf;
80 bufsz = rpnp->pn_bufsize;
81 }
cae5b340 82
34dc7c2f
BB
83 /*
84 * In the non-mixed case we only expect there would ever
85 * be one match, but we need to use the normalizing lookup.
86 */
cae5b340 87 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
34dc7c2f 88 zoid, mt, buf, bufsz, &conflict);
34dc7c2f 89 } else {
cae5b340 90 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
34dc7c2f 91 }
0c5dde49
BB
92
93 /*
94 * Allow multiple entries provided the first entry is
95 * the object id. Non-zpl consumers may safely make
96 * use of the additional space.
97 *
98 * XXX: This should be a feature flag for compatibility
99 */
100 if (error == EOVERFLOW)
101 error = 0;
102
cae5b340 103 if (zfsvfs->z_norm && !error && deflags)
0c5dde49
BB
104 *deflags = conflict ? ED_CASE_CONFLICT : 0;
105
34dc7c2f
BB
106 *zoid = ZFS_DIRENT_OBJ(*zoid);
107
3558fd73 108#ifdef HAVE_DNLC
34dc7c2f 109 if (error == ENOENT && update)
3558fd73
BB
110 dnlc_update(ZTOI(dzp), name, DNLC_NO_VNODE);
111#endif /* HAVE_DNLC */
34dc7c2f
BB
112
113 return (error);
114}
115
116/*
117 * Lock a directory entry. A dirlock on <dzp, name> protects that name
118 * in dzp's directory zap object. As long as you hold a dirlock, you can
119 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
120 * can change the zap entry for (i.e. link or unlink) this name.
121 *
122 * Input arguments:
123 * dzp - znode for directory
124 * name - name of entry to lock
125 * flag - ZNEW: if the entry already exists, fail with EEXIST.
126 * ZEXISTS: if the entry does not exist, fail with ENOENT.
127 * ZSHARED: allow concurrent access with other ZSHARED callers.
128 * ZXATTR: we want dzp's xattr directory
129 * ZCILOOK: On a mixed sensitivity file system,
130 * this lookup should be case-insensitive.
131 * ZCIEXACT: On a purely case-insensitive file system,
132 * this lookup should be case-sensitive.
133 * ZRENAMING: we are locking for renaming, force narrow locks
428870ff
BB
134 * ZHAVELOCK: Don't grab the z_name_lock for this call. The
135 * current thread already holds it.
34dc7c2f
BB
136 *
137 * Output arguments:
138 * zpp - pointer to the znode for the entry (NULL if there isn't one)
139 * dlpp - pointer to the dirlock for this entry (NULL on error)
140 * direntflags - (case-insensitive lookup only)
141 * flags if multiple case-sensitive matches exist in directory
142 * realpnp - (case-insensitive lookup only)
143 * actual name matched within the directory
144 *
145 * Return value: 0 on success or errno on failure.
146 *
147 * NOTE: Always checks for, and rejects, '.' and '..'.
148 * NOTE: For case-insensitive file systems we take wide locks (see below),
149 * but return znode pointers to a single match.
150 */
151int
152zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
153 int flag, int *direntflags, pathname_t *realpnp)
154{
cae5b340 155 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f
BB
156 zfs_dirlock_t *dl;
157 boolean_t update;
cae5b340 158 matchtype_t mt = 0;
34dc7c2f 159 uint64_t zoid;
3558fd73 160#ifdef HAVE_DNLC
34dc7c2f 161 vnode_t *vp = NULL;
3558fd73 162#endif /* HAVE_DNLC */
34dc7c2f
BB
163 int error = 0;
164 int cmpflags;
165
166 *zpp = NULL;
167 *dlpp = NULL;
168
169 /*
170 * Verify that we are not trying to lock '.', '..', or '.zfs'
171 */
149e873a
BB
172 if ((name[0] == '.' &&
173 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) ||
174 (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0))
a08ee875 175 return (SET_ERROR(EEXIST));
34dc7c2f
BB
176
177 /*
178 * Case sensitivity and normalization preferences are set when
179 * the file system is created. These are stored in the
cae5b340 180 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
34dc7c2f
BB
181 * affect what vnodes can be cached in the DNLC, how we
182 * perform zap lookups, and the "width" of our dirlocks.
183 *
184 * A normal dirlock locks a single name. Note that with
185 * normalization a name can be composed multiple ways, but
186 * when normalized, these names all compare equal. A wide
187 * dirlock locks multiple names. We need these when the file
188 * system is supporting mixed-mode access. It is sometimes
189 * necessary to lock all case permutations of file name at
190 * once so that simultaneous case-insensitive/case-sensitive
191 * behaves as rationally as possible.
192 */
193
194 /*
cae5b340
AX
195 * When matching we may need to normalize & change case according to
196 * FS settings.
197 *
198 * Note that a normalized match is necessary for a case insensitive
199 * filesystem when the lookup request is not exact because normalization
200 * can fold case independent of normalizing code point sequences.
201 *
202 * See the table above zfs_dropname().
34dc7c2f 203 */
cae5b340
AX
204 if (zfsvfs->z_norm != 0) {
205 mt = MT_NORMALIZE;
206
207 /*
208 * Determine if the match needs to honor the case specified in
209 * lookup, and if so keep track of that so that during
210 * normalization we don't fold case.
211 */
212 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
213 (flag & ZCIEXACT)) ||
214 (zfsvfs->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
215 mt |= MT_MATCH_CASE;
216 }
217 }
34dc7c2f
BB
218
219 /*
220 * Only look in or update the DNLC if we are looking for the
221 * name on a file system that does not require normalization
222 * or case folding. We can also look there if we happen to be
223 * on a non-normalizing, mixed sensitivity file system IF we
224 * are looking for the exact name.
225 *
226 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
227 * case for performance improvement?
228 */
cae5b340
AX
229 update = !zfsvfs->z_norm ||
230 (zfsvfs->z_case == ZFS_CASE_MIXED &&
231 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
34dc7c2f
BB
232
233 /*
234 * ZRENAMING indicates we are in a situation where we should
235 * take narrow locks regardless of the file system's
236 * preferences for normalizing and case folding. This will
237 * prevent us deadlocking trying to grab the same wide lock
238 * twice if the two names happen to be case-insensitive
239 * matches.
240 */
241 if (flag & ZRENAMING)
242 cmpflags = 0;
243 else
cae5b340 244 cmpflags = zfsvfs->z_norm;
34dc7c2f
BB
245
246 /*
247 * Wait until there are no locks on this name.
428870ff
BB
248 *
249 * Don't grab the the lock if it is already held. However, cannot
250 * have both ZSHARED and ZHAVELOCK together.
34dc7c2f 251 */
428870ff
BB
252 ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
253 if (!(flag & ZHAVELOCK))
254 rw_enter(&dzp->z_name_lock, RW_READER);
255
34dc7c2f
BB
256 mutex_enter(&dzp->z_lock);
257 for (;;) {
cae5b340 258 if (dzp->z_unlinked && !(flag & ZXATTR)) {
34dc7c2f 259 mutex_exit(&dzp->z_lock);
428870ff
BB
260 if (!(flag & ZHAVELOCK))
261 rw_exit(&dzp->z_name_lock);
a08ee875 262 return (SET_ERROR(ENOENT));
34dc7c2f
BB
263 }
264 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
265 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
266 U8_UNICODE_LATEST, &error) == 0) || error != 0)
267 break;
268 }
269 if (error != 0) {
270 mutex_exit(&dzp->z_lock);
428870ff
BB
271 if (!(flag & ZHAVELOCK))
272 rw_exit(&dzp->z_name_lock);
a08ee875 273 return (SET_ERROR(ENOENT));
34dc7c2f
BB
274 }
275 if (dl == NULL) {
276 /*
277 * Allocate a new dirlock and add it to the list.
278 */
279 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
280 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
281 dl->dl_name = name;
282 dl->dl_sharecnt = 0;
428870ff 283 dl->dl_namelock = 0;
34dc7c2f
BB
284 dl->dl_namesize = 0;
285 dl->dl_dzp = dzp;
286 dl->dl_next = dzp->z_dirlocks;
287 dzp->z_dirlocks = dl;
288 break;
289 }
290 if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
291 break;
292 cv_wait(&dl->dl_cv, &dzp->z_lock);
293 }
294
428870ff
BB
295 /*
296 * If the z_name_lock was NOT held for this dirlock record it.
297 */
298 if (flag & ZHAVELOCK)
299 dl->dl_namelock = 1;
300
34dc7c2f
BB
301 if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
302 /*
303 * We're the second shared reference to dl. Make a copy of
304 * dl_name in case the first thread goes away before we do.
305 * Note that we initialize the new name before storing its
306 * pointer into dl_name, because the first thread may load
cae5b340
AX
307 * dl->dl_name at any time. It'll either see the old value,
308 * which belongs to it, or the new shared copy; either is OK.
34dc7c2f
BB
309 */
310 dl->dl_namesize = strlen(dl->dl_name) + 1;
311 name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
312 bcopy(dl->dl_name, name, dl->dl_namesize);
313 dl->dl_name = name;
314 }
315
316 mutex_exit(&dzp->z_lock);
317
318 /*
319 * We have a dirlock on the name. (Note that it is the dirlock,
320 * not the dzp's z_lock, that protects the name in the zap object.)
321 * See if there's an object by this name; if so, put a hold on it.
322 */
323 if (flag & ZXATTR) {
cae5b340 324 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
428870ff
BB
325 sizeof (zoid));
326 if (error == 0)
a08ee875 327 error = (zoid == 0 ? SET_ERROR(ENOENT) : 0);
34dc7c2f 328 } else {
3558fd73 329#ifdef HAVE_DNLC
34dc7c2f 330 if (update)
3558fd73 331 vp = dnlc_lookup(ZTOI(dzp), name);
34dc7c2f 332 if (vp == DNLC_NO_VNODE) {
3558fd73 333 iput(vp);
a08ee875 334 error = SET_ERROR(ENOENT);
34dc7c2f
BB
335 } else if (vp) {
336 if (flag & ZNEW) {
337 zfs_dirent_unlock(dl);
3558fd73 338 iput(vp);
a08ee875 339 return (SET_ERROR(EEXIST));
34dc7c2f
BB
340 }
341 *dlpp = dl;
342 *zpp = VTOZ(vp);
343 return (0);
344 } else {
cae5b340 345 error = zfs_match_find(zfsvfs, dzp, name, mt,
34dc7c2f
BB
346 update, direntflags, realpnp, &zoid);
347 }
3558fd73 348#else
cae5b340 349 error = zfs_match_find(zfsvfs, dzp, name, mt,
3558fd73
BB
350 update, direntflags, realpnp, &zoid);
351#endif /* HAVE_DNLC */
34dc7c2f
BB
352 }
353 if (error) {
354 if (error != ENOENT || (flag & ZEXISTS)) {
355 zfs_dirent_unlock(dl);
356 return (error);
357 }
358 } else {
359 if (flag & ZNEW) {
360 zfs_dirent_unlock(dl);
a08ee875 361 return (SET_ERROR(EEXIST));
34dc7c2f 362 }
cae5b340 363 error = zfs_zget(zfsvfs, zoid, zpp);
34dc7c2f
BB
364 if (error) {
365 zfs_dirent_unlock(dl);
366 return (error);
367 }
3558fd73 368#ifdef HAVE_DNLC
34dc7c2f 369 if (!(flag & ZXATTR) && update)
3558fd73
BB
370 dnlc_update(ZTOI(dzp), name, ZTOI(*zpp));
371#endif /* HAVE_DNLC */
34dc7c2f
BB
372 }
373
374 *dlpp = dl;
375
376 return (0);
377}
378
379/*
380 * Unlock this directory entry and wake anyone who was waiting for it.
381 */
382void
383zfs_dirent_unlock(zfs_dirlock_t *dl)
384{
385 znode_t *dzp = dl->dl_dzp;
386 zfs_dirlock_t **prev_dl, *cur_dl;
387
388 mutex_enter(&dzp->z_lock);
428870ff
BB
389
390 if (!dl->dl_namelock)
391 rw_exit(&dzp->z_name_lock);
392
34dc7c2f
BB
393 if (dl->dl_sharecnt > 1) {
394 dl->dl_sharecnt--;
395 mutex_exit(&dzp->z_lock);
396 return;
397 }
398 prev_dl = &dzp->z_dirlocks;
399 while ((cur_dl = *prev_dl) != dl)
400 prev_dl = &cur_dl->dl_next;
401 *prev_dl = dl->dl_next;
402 cv_broadcast(&dl->dl_cv);
403 mutex_exit(&dzp->z_lock);
404
405 if (dl->dl_namesize != 0)
406 kmem_free(dl->dl_name, dl->dl_namesize);
407 cv_destroy(&dl->dl_cv);
408 kmem_free(dl, sizeof (*dl));
409}
410
411/*
412 * Look up an entry in a directory.
413 *
414 * NOTE: '.' and '..' are handled as special cases because
415 * no directory entries are actually stored for them. If this is
416 * the root of a filesystem, then '.zfs' is also treated as a
417 * special pseudo-directory.
418 */
419int
3558fd73 420zfs_dirlook(znode_t *dzp, char *name, struct inode **ipp, int flags,
34dc7c2f
BB
421 int *deflg, pathname_t *rpnp)
422{
423 zfs_dirlock_t *dl;
424 znode_t *zp;
425 int error = 0;
428870ff 426 uint64_t parent;
34dc7c2f
BB
427
428 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
3558fd73
BB
429 *ipp = ZTOI(dzp);
430 igrab(*ipp);
34dc7c2f 431 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
cae5b340 432 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
428870ff 433
34dc7c2f
BB
434 /*
435 * If we are a snapshot mounted under .zfs, return
ebe7e575 436 * the inode pointer for the snapshot directory.
34dc7c2f 437 */
428870ff 438 if ((error = sa_lookup(dzp->z_sa_hdl,
cae5b340 439 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
428870ff 440 return (error);
ebe7e575 441
cae5b340
AX
442 if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) {
443 error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
ebe7e575 444 "snapshot", ipp, 0, kcred, NULL, NULL);
34dc7c2f
BB
445 return (error);
446 }
447 rw_enter(&dzp->z_parent_lock, RW_READER);
cae5b340 448 error = zfs_zget(zfsvfs, parent, &zp);
34dc7c2f 449 if (error == 0)
3558fd73 450 *ipp = ZTOI(zp);
34dc7c2f
BB
451 rw_exit(&dzp->z_parent_lock);
452 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
3558fd73 453 *ipp = zfsctl_root(dzp);
34dc7c2f
BB
454 } else {
455 int zf;
456
457 zf = ZEXISTS | ZSHARED;
458 if (flags & FIGNORECASE)
459 zf |= ZCILOOK;
460
461 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
462 if (error == 0) {
3558fd73 463 *ipp = ZTOI(zp);
34dc7c2f
BB
464 zfs_dirent_unlock(dl);
465 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
466 }
467 rpnp = NULL;
468 }
469
470 if ((flags & FIGNORECASE) && rpnp && !error)
471 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
472
473 return (error);
474}
475
34dc7c2f
BB
476/*
477 * unlinked Set (formerly known as the "delete queue") Error Handling
478 *
479 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
480 * don't specify the name of the entry that we will be manipulating. We
481 * also fib and say that we won't be adding any new entries to the
482 * unlinked set, even though we might (this is to lower the minimum file
483 * size that can be deleted in a full filesystem). So on the small
484 * chance that the nlink list is using a fat zap (ie. has more than
485 * 2000 entries), we *may* not pre-read a block that's needed.
486 * Therefore it is remotely possible for some of the assertions
487 * regarding the unlinked set below to fail due to i/o error. On a
488 * nondebug system, this will result in the space being leaked.
489 */
490void
491zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
492{
cae5b340 493 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f
BB
494
495 ASSERT(zp->z_unlinked);
cae5b340 496 ASSERT(ZTOI(zp)->i_nlink == 0);
34dc7c2f 497
b128c09f 498 VERIFY3U(0, ==,
cae5b340 499 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
34dc7c2f
BB
500}
501
ea04106b
AX
502/*
503 * Clean up any znodes that had no links when we either crashed or
504 * (force) umounted the file system.
505 */
506void
cae5b340 507zfs_unlinked_drain(zfsvfs_t *zfsvfs)
ea04106b
AX
508{
509 zap_cursor_t zc;
510 zap_attribute_t zap;
511 dmu_object_info_t doi;
512 znode_t *zp;
513 int error;
514
515 /*
516 * Iterate over the contents of the unlinked set.
517 */
cae5b340 518 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
ea04106b
AX
519 zap_cursor_retrieve(&zc, &zap) == 0;
520 zap_cursor_advance(&zc)) {
521
522 /*
523 * See what kind of object we have in list
524 */
525
cae5b340
AX
526 error = dmu_object_info(zfsvfs->z_os,
527 zap.za_first_integer, &doi);
ea04106b
AX
528 if (error != 0)
529 continue;
530
531 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
532 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
533 /*
534 * We need to re-mark these list entries for deletion,
535 * so we pull them back into core and set zp->z_unlinked.
536 */
cae5b340 537 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
ea04106b
AX
538
539 /*
540 * We may pick up znodes that are already marked for deletion.
541 * This could happen during the purge of an extended attribute
542 * directory. All we need to do is skip over them, since they
543 * are already in the system marked z_unlinked.
544 */
545 if (error != 0)
546 continue;
547
548 zp->z_unlinked = B_TRUE;
549 iput(ZTOI(zp));
550 }
551 zap_cursor_fini(&zc);
552}
553
34dc7c2f
BB
554/*
555 * Delete the entire contents of a directory. Return a count
556 * of the number of entries that could not be deleted. If we encounter
557 * an error, return a count of at least one so that the directory stays
558 * in the unlinked set.
559 *
560 * NOTE: this function assumes that the directory is inactive,
561 * so there is no need to lock its entries before deletion.
562 * Also, it assumes the directory contents is *only* regular
563 * files.
564 */
565static int
566zfs_purgedir(znode_t *dzp)
567{
568 zap_cursor_t zc;
569 zap_attribute_t zap;
570 znode_t *xzp;
571 dmu_tx_t *tx;
cae5b340 572 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
34dc7c2f
BB
573 zfs_dirlock_t dl;
574 int skipped = 0;
575 int error;
576
cae5b340 577 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
34dc7c2f
BB
578 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
579 zap_cursor_advance(&zc)) {
cae5b340 580 error = zfs_zget(zfsvfs,
34dc7c2f
BB
581 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
582 if (error) {
583 skipped += 1;
584 continue;
585 }
586
ea04106b
AX
587 ASSERT(S_ISREG(ZTOI(xzp)->i_mode) ||
588 S_ISLNK(ZTOI(xzp)->i_mode));
34dc7c2f 589
cae5b340 590 tx = dmu_tx_create(zfsvfs->z_os);
428870ff 591 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
34dc7c2f 592 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
428870ff 593 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
cae5b340 594 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
428870ff
BB
595 /* Is this really needed ? */
596 zfs_sa_upgrade_txholds(tx, xzp);
cae5b340 597 dmu_tx_mark_netfree(tx);
34dc7c2f
BB
598 error = dmu_tx_assign(tx, TXG_WAIT);
599 if (error) {
600 dmu_tx_abort(tx);
ea04106b 601 zfs_iput_async(ZTOI(xzp));
34dc7c2f
BB
602 skipped += 1;
603 continue;
604 }
605 bzero(&dl, sizeof (dl));
606 dl.dl_dzp = dzp;
607 dl.dl_name = zap.za_name;
608
609 error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
610 if (error)
611 skipped += 1;
612 dmu_tx_commit(tx);
cae5b340 613
ea04106b 614 zfs_iput_async(ZTOI(xzp));
34dc7c2f
BB
615 }
616 zap_cursor_fini(&zc);
617 if (error != ENOENT)
618 skipped += 1;
619 return (skipped);
620}
621
622void
623zfs_rmnode(znode_t *zp)
624{
cae5b340
AX
625 zfsvfs_t *zfsvfs = ZTOZSB(zp);
626 objset_t *os = zfsvfs->z_os;
34dc7c2f 627 znode_t *xzp = NULL;
34dc7c2f
BB
628 dmu_tx_t *tx;
629 uint64_t acl_obj;
428870ff 630 uint64_t xattr_obj;
cae5b340 631 uint64_t links;
34dc7c2f
BB
632 int error;
633
cae5b340 634 ASSERT(ZTOI(zp)->i_nlink == 0);
3558fd73 635 ASSERT(atomic_read(&ZTOI(zp)->i_count) == 0);
34dc7c2f
BB
636
637 /*
638 * If this is an attribute directory, purge its contents.
639 */
3558fd73 640 if (S_ISDIR(ZTOI(zp)->i_mode) && (zp->z_pflags & ZFS_XATTR)) {
ea04106b 641 if (zfs_purgedir(zp) != 0) {
34dc7c2f 642 /*
ea04106b
AX
643 * Not enough space to delete some xattrs.
644 * Leave it in the unlinked set.
34dc7c2f 645 */
7973e464 646 zfs_znode_dmu_fini(zp);
ea04106b 647
34dc7c2f
BB
648 return;
649 }
650 }
651
b128c09f 652 /*
94a40997
AX
653 * Free up all the data in the file. We don't do this for directories
654 * because we need truncate and remove to be in the same tx, like in
655 * zfs_znode_delete(). Otherwise, if we crash here we'll end up with
656 * an inconsistent truncated zap object in the delete queue. Note a
657 * truncated file is harmless since it only contains user data.
b128c09f 658 */
94a40997
AX
659 if (S_ISREG(ZTOI(zp)->i_mode)) {
660 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
661 if (error) {
662 /*
cae5b340
AX
663 * Not enough space or we were interrupted by unmount.
664 * Leave the file in the unlinked set.
94a40997
AX
665 */
666 zfs_znode_dmu_fini(zp);
667 return;
668 }
b128c09f
BB
669 }
670
34dc7c2f
BB
671 /*
672 * If the file has extended attributes, we're going to unlink
673 * the xattr dir.
674 */
cae5b340 675 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
428870ff
BB
676 &xattr_obj, sizeof (xattr_obj));
677 if (error == 0 && xattr_obj) {
cae5b340 678 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
34dc7c2f
BB
679 ASSERT(error == 0);
680 }
681
572e2857 682 acl_obj = zfs_external_acl(zp);
34dc7c2f
BB
683
684 /*
b128c09f 685 * Set up the final transaction.
34dc7c2f
BB
686 */
687 tx = dmu_tx_create(os);
688 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
cae5b340 689 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
34dc7c2f 690 if (xzp) {
cae5b340 691 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
428870ff 692 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
34dc7c2f
BB
693 }
694 if (acl_obj)
695 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
428870ff
BB
696
697 zfs_sa_upgrade_txholds(tx, zp);
34dc7c2f
BB
698 error = dmu_tx_assign(tx, TXG_WAIT);
699 if (error) {
700 /*
701 * Not enough space to delete the file. Leave it in the
702 * unlinked set, leaking it until the fs is remounted (at
703 * which point we'll call zfs_unlinked_drain() to process it).
704 */
705 dmu_tx_abort(tx);
706 zfs_znode_dmu_fini(zp);
34dc7c2f
BB
707 goto out;
708 }
709
710 if (xzp) {
428870ff 711 ASSERT(error == 0);
34dc7c2f
BB
712 mutex_enter(&xzp->z_lock);
713 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
cae5b340
AX
714 clear_nlink(ZTOI(xzp)); /* no more links to it */
715 links = 0;
716 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
717 &links, sizeof (links), tx));
34dc7c2f
BB
718 mutex_exit(&xzp->z_lock);
719 zfs_unlinked_add(xzp, tx);
720 }
721
722 /* Remove this znode from the unlinked set */
b128c09f 723 VERIFY3U(0, ==,
cae5b340 724 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
34dc7c2f
BB
725
726 zfs_znode_delete(zp, tx);
727
728 dmu_tx_commit(tx);
729out:
730 if (xzp)
ea04106b 731 zfs_iput_async(ZTOI(xzp));
34dc7c2f
BB
732}
733
734static uint64_t
428870ff 735zfs_dirent(znode_t *zp, uint64_t mode)
34dc7c2f
BB
736{
737 uint64_t de = zp->z_id;
428870ff 738
3558fd73 739 if (ZTOZSB(zp)->z_version >= ZPL_VERSION_DIRENT_TYPE)
428870ff 740 de |= IFTODT(mode) << 60;
34dc7c2f
BB
741 return (de);
742}
743
744/*
745 * Link zp into dl. Can only fail if zp has been unlinked.
746 */
747int
748zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
749{
750 znode_t *dzp = dl->dl_dzp;
cae5b340 751 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f 752 uint64_t value;
3558fd73 753 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
428870ff
BB
754 sa_bulk_attr_t bulk[5];
755 uint64_t mtime[2], ctime[2];
cae5b340 756 uint64_t links;
428870ff 757 int count = 0;
34dc7c2f
BB
758 int error;
759
34dc7c2f
BB
760 mutex_enter(&zp->z_lock);
761
762 if (!(flag & ZRENAMING)) {
763 if (zp->z_unlinked) { /* no new links to unlinked zp */
764 ASSERT(!(flag & (ZNEW | ZEXISTS)));
765 mutex_exit(&zp->z_lock);
a08ee875 766 return (SET_ERROR(ENOENT));
34dc7c2f 767 }
cae5b340
AX
768 if (!(flag & ZNEW)) {
769 /*
770 * ZNEW nodes come from zfs_mknode() where the link
771 * count has already been initialised
772 */
773 inc_nlink(ZTOI(zp));
774 links = ZTOI(zp)->i_nlink;
775 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
776 NULL, &links, sizeof (links));
777 }
34dc7c2f 778 }
cae5b340 779 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
428870ff 780 &dzp->z_id, sizeof (dzp->z_id));
cae5b340 781 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff
BB
782 &zp->z_pflags, sizeof (zp->z_pflags));
783
784 if (!(flag & ZNEW)) {
cae5b340 785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
428870ff
BB
786 ctime, sizeof (ctime));
787 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
68d83c55 788 ctime);
428870ff
BB
789 }
790 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
791 ASSERT(error == 0);
34dc7c2f 792
34dc7c2f
BB
793 mutex_exit(&zp->z_lock);
794
34dc7c2f 795 mutex_enter(&dzp->z_lock);
428870ff 796 dzp->z_size++;
cae5b340
AX
797 if (zp_is_dir)
798 inc_nlink(ZTOI(dzp));
799 links = ZTOI(dzp)->i_nlink;
428870ff 800 count = 0;
cae5b340 801 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
428870ff 802 &dzp->z_size, sizeof (dzp->z_size));
cae5b340
AX
803 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
804 &links, sizeof (links));
805 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
428870ff 806 mtime, sizeof (mtime));
cae5b340 807 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
428870ff 808 ctime, sizeof (ctime));
cae5b340 809 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
428870ff 810 &dzp->z_pflags, sizeof (dzp->z_pflags));
68d83c55 811 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
428870ff
BB
812 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
813 ASSERT(error == 0);
34dc7c2f
BB
814 mutex_exit(&dzp->z_lock);
815
428870ff 816 value = zfs_dirent(zp, zp->z_mode);
3558fd73 817 error = zap_add(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
34dc7c2f
BB
818 8, 1, &value, tx);
819 ASSERT(error == 0);
820
34dc7c2f
BB
821 return (0);
822}
823
cae5b340
AX
824/*
825 * The match type in the code for this function should conform to:
826 *
827 * ------------------------------------------------------------------------
828 * fs type | z_norm | lookup type | match type
829 * ---------|-------------|-------------|----------------------------------
830 * CS !norm | 0 | 0 | 0 (exact)
831 * CS norm | formX | 0 | MT_NORMALIZE
832 * CI !norm | upper | !ZCIEXACT | MT_NORMALIZE
833 * CI !norm | upper | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
834 * CI norm | upper|formX | !ZCIEXACT | MT_NORMALIZE
835 * CI norm | upper|formX | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
836 * CM !norm | upper | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
837 * CM !norm | upper | ZCILOOK | MT_NORMALIZE
838 * CM norm | upper|formX | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
839 * CM norm | upper|formX | ZCILOOK | MT_NORMALIZE
840 *
841 * Abbreviations:
842 * CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
843 * upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
844 * formX = unicode normalization form set on fs creation
845 */
428870ff
BB
846static int
847zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
848 int flag)
849{
850 int error;
851
3558fd73 852 if (ZTOZSB(zp)->z_norm) {
cae5b340
AX
853 matchtype_t mt = MT_NORMALIZE;
854
855 if ((ZTOZSB(zp)->z_case == ZFS_CASE_INSENSITIVE &&
428870ff 856 (flag & ZCIEXACT)) ||
cae5b340
AX
857 (ZTOZSB(zp)->z_case == ZFS_CASE_MIXED &&
858 !(flag & ZCILOOK))) {
859 mt |= MT_MATCH_CASE;
860 }
861
862 error = zap_remove_norm(ZTOZSB(zp)->z_os, dzp->z_id,
863 dl->dl_name, mt, tx);
428870ff 864 } else {
cae5b340
AX
865 error = zap_remove(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
866 tx);
428870ff
BB
867 }
868
869 return (error);
870}
871
34dc7c2f 872/*
cd38ac58
BB
873 * Unlink zp from dl, and mark zp for deletion if this was the last link. Can
874 * fail if zp is a mount point (EBUSY) or a non-empty directory (ENOTEMPTY).
34dc7c2f
BB
875 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
876 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
877 * and it's the caller's job to do it.
878 */
879int
880zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
cae5b340 881 boolean_t *unlinkedp)
34dc7c2f
BB
882{
883 znode_t *dzp = dl->dl_dzp;
cae5b340 884 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
3558fd73 885 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
34dc7c2f 886 boolean_t unlinked = B_FALSE;
428870ff
BB
887 sa_bulk_attr_t bulk[5];
888 uint64_t mtime[2], ctime[2];
cae5b340 889 uint64_t links;
428870ff 890 int count = 0;
34dc7c2f
BB
891 int error;
892
3558fd73
BB
893#ifdef HAVE_DNLC
894 dnlc_remove(ZTOI(dzp), dl->dl_name);
895#endif /* HAVE_DNLC */
34dc7c2f
BB
896
897 if (!(flag & ZRENAMING)) {
34dc7c2f 898 mutex_enter(&zp->z_lock);
428870ff
BB
899
900 if (zp_is_dir && !zfs_dirempty(zp)) {
34dc7c2f 901 mutex_exit(&zp->z_lock);
a08ee875 902 return (SET_ERROR(ENOTEMPTY));
34dc7c2f 903 }
428870ff
BB
904
905 /*
906 * If we get here, we are going to try to remove the object.
907 * First try removing the name from the directory; if that
908 * fails, return the error.
909 */
910 error = zfs_dropname(dl, zp, dzp, tx, flag);
911 if (error != 0) {
912 mutex_exit(&zp->z_lock);
428870ff
BB
913 return (error);
914 }
915
cae5b340 916 if (ZTOI(zp)->i_nlink <= zp_is_dir) {
3558fd73
BB
917 zfs_panic_recover("zfs: link count on %lu is %u, "
918 "should be at least %u", zp->z_id,
cae5b340
AX
919 (int)ZTOI(zp)->i_nlink, zp_is_dir + 1);
920 set_nlink(ZTOI(zp), zp_is_dir + 1);
34dc7c2f 921 }
cae5b340
AX
922 drop_nlink(ZTOI(zp));
923 if (ZTOI(zp)->i_nlink == zp_is_dir) {
34dc7c2f 924 zp->z_unlinked = B_TRUE;
cae5b340 925 clear_nlink(ZTOI(zp));
34dc7c2f
BB
926 unlinked = B_TRUE;
927 } else {
cae5b340 928 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
428870ff 929 NULL, &ctime, sizeof (ctime));
cae5b340 930 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
428870ff 931 NULL, &zp->z_pflags, sizeof (zp->z_pflags));
68d83c55
AX
932 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
933 ctime);
34dc7c2f 934 }
cae5b340
AX
935 links = ZTOI(zp)->i_nlink;
936 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
937 NULL, &links, sizeof (links));
428870ff
BB
938 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
939 count = 0;
940 ASSERT(error == 0);
34dc7c2f 941 mutex_exit(&zp->z_lock);
428870ff
BB
942 } else {
943 error = zfs_dropname(dl, zp, dzp, tx, flag);
944 if (error != 0)
945 return (error);
34dc7c2f
BB
946 }
947
34dc7c2f 948 mutex_enter(&dzp->z_lock);
428870ff 949 dzp->z_size--; /* one dirent removed */
cae5b340
AX
950 if (zp_is_dir)
951 drop_nlink(ZTOI(dzp)); /* ".." link from zp */
952 links = ZTOI(dzp)->i_nlink;
953 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
954 NULL, &links, sizeof (links));
955 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
428870ff 956 NULL, &dzp->z_size, sizeof (dzp->z_size));
cae5b340 957 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
428870ff 958 NULL, ctime, sizeof (ctime));
cae5b340 959 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
428870ff 960 NULL, mtime, sizeof (mtime));
cae5b340 961 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
428870ff 962 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
68d83c55 963 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
428870ff 964 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
34dc7c2f 965 ASSERT(error == 0);
428870ff 966 mutex_exit(&dzp->z_lock);
34dc7c2f
BB
967
968 if (unlinkedp != NULL)
969 *unlinkedp = unlinked;
970 else if (unlinked)
971 zfs_unlinked_add(zp, tx);
972
973 return (0);
974}
975
976/*
977 * Indicate whether the directory is empty. Works with or without z_lock
978 * held, but can only be consider a hint in the latter case. Returns true
979 * if only "." and ".." remain and there's no work in progress.
047218e2
AX
980 *
981 * The internal ZAP size, rather than zp->z_size, needs to be checked since
982 * some consumers (Lustre) do not strictly maintain an accurate SA_ZPL_SIZE.
34dc7c2f
BB
983 */
984boolean_t
985zfs_dirempty(znode_t *dzp)
986{
047218e2
AX
987 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
988 uint64_t count;
989 int error;
990
991 if (dzp->z_dirlocks != NULL)
992 return (B_FALSE);
993
994 error = zap_count(zfsvfs->z_os, dzp->z_id, &count);
995 if (error != 0 || count != 0)
996 return (B_FALSE);
997
998 return (B_TRUE);
34dc7c2f
BB
999}
1000
1001int
3558fd73 1002zfs_make_xattrdir(znode_t *zp, vattr_t *vap, struct inode **xipp, cred_t *cr)
34dc7c2f 1003{
cae5b340 1004 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f
BB
1005 znode_t *xzp;
1006 dmu_tx_t *tx;
1007 int error;
9babb374
BB
1008 zfs_acl_ids_t acl_ids;
1009 boolean_t fuid_dirtied;
3558fd73 1010#ifdef DEBUG
428870ff 1011 uint64_t parent;
3558fd73 1012#endif
34dc7c2f 1013
3558fd73 1014 *xipp = NULL;
34dc7c2f 1015
3558fd73 1016 if ((error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)))
34dc7c2f
BB
1017 return (error);
1018
9babb374
BB
1019 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
1020 &acl_ids)) != 0)
1021 return (error);
cae5b340 1022 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
9babb374 1023 zfs_acl_ids_free(&acl_ids);
a08ee875 1024 return (SET_ERROR(EDQUOT));
9babb374
BB
1025 }
1026
cae5b340 1027 tx = dmu_tx_create(zfsvfs->z_os);
428870ff
BB
1028 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1029 ZFS_SA_BASE_ATTR_SIZE);
1030 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
34dc7c2f 1031 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
cae5b340 1032 fuid_dirtied = zfsvfs->z_fuid_dirty;
9babb374 1033 if (fuid_dirtied)
cae5b340 1034 zfs_fuid_txhold(zfsvfs, tx);
a08ee875 1035 error = dmu_tx_assign(tx, TXG_WAIT);
34dc7c2f 1036 if (error) {
428870ff 1037 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
1038 dmu_tx_abort(tx);
1039 return (error);
1040 }
428870ff 1041 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
9babb374
BB
1042
1043 if (fuid_dirtied)
cae5b340 1044 zfs_fuid_sync(zfsvfs, tx);
9babb374 1045
428870ff 1046#ifdef DEBUG
cae5b340 1047 error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
428870ff
BB
1048 &parent, sizeof (parent));
1049 ASSERT(error == 0 && parent == zp->z_id);
1050#endif
1051
cae5b340 1052 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
428870ff 1053 sizeof (xzp->z_id), tx));
34dc7c2f 1054
cae5b340
AX
1055 if (!zp->z_unlinked)
1056 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
1057 xzp, "", NULL, acl_ids.z_fuidp, vap);
9babb374
BB
1058
1059 zfs_acl_ids_free(&acl_ids);
34dc7c2f
BB
1060 dmu_tx_commit(tx);
1061
3558fd73 1062 *xipp = ZTOI(xzp);
34dc7c2f
BB
1063
1064 return (0);
1065}
1066
1067/*
1068 * Return a znode for the extended attribute directory for zp.
1069 * ** If the directory does not already exist, it is created **
1070 *
1071 * IN: zp - znode to obtain attribute directory from
1072 * cr - credentials of caller
1073 * flags - flags from the VOP_LOOKUP call
1074 *
3558fd73 1075 * OUT: xipp - pointer to extended attribute znode
34dc7c2f
BB
1076 *
1077 * RETURN: 0 on success
1078 * error number on failure
1079 */
1080int
3558fd73 1081zfs_get_xattrdir(znode_t *zp, struct inode **xipp, cred_t *cr, int flags)
34dc7c2f 1082{
cae5b340 1083 zfsvfs_t *zfsvfs = ZTOZSB(zp);
34dc7c2f
BB
1084 znode_t *xzp;
1085 zfs_dirlock_t *dl;
1086 vattr_t va;
1087 int error;
1088top:
1089 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1090 if (error)
1091 return (error);
1092
1093 if (xzp != NULL) {
3558fd73 1094 *xipp = ZTOI(xzp);
34dc7c2f
BB
1095 zfs_dirent_unlock(dl);
1096 return (0);
1097 }
1098
34dc7c2f
BB
1099 if (!(flags & CREATE_XATTR_DIR)) {
1100 zfs_dirent_unlock(dl);
a08ee875 1101 return (SET_ERROR(ENOENT));
34dc7c2f
BB
1102 }
1103
cae5b340 1104 if (zfs_is_readonly(zfsvfs)) {
34dc7c2f 1105 zfs_dirent_unlock(dl);
a08ee875 1106 return (SET_ERROR(EROFS));
34dc7c2f
BB
1107 }
1108
1109 /*
1110 * The ability to 'create' files in an attribute
1111 * directory comes from the write_xattr permission on the base file.
1112 *
1113 * The ability to 'search' an attribute directory requires
1114 * read_xattr permission on the base file.
1115 *
1116 * Once in a directory the ability to read/write attributes
1117 * is controlled by the permissions on the attribute file.
1118 */
3558fd73 1119 va.va_mask = ATTR_MODE | ATTR_UID | ATTR_GID;
34dc7c2f
BB
1120 va.va_mode = S_IFDIR | S_ISVTX | 0777;
1121 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1122
274b7e79 1123 va.va_dentry = NULL;
3558fd73 1124 error = zfs_make_xattrdir(zp, &va, xipp, cr);
34dc7c2f
BB
1125 zfs_dirent_unlock(dl);
1126
fb5f0bc8 1127 if (error == ERESTART) {
34dc7c2f
BB
1128 /* NB: we already did dmu_tx_wait() if necessary */
1129 goto top;
1130 }
1131
1132 return (error);
1133}
1134
1135/*
1136 * Decide whether it is okay to remove within a sticky directory.
1137 *
1138 * In sticky directories, write access is not sufficient;
1139 * you can remove entries from a directory only if:
1140 *
1141 * you own the directory,
1142 * you own the entry,
41d74433 1143 * you have write access to the entry,
34dc7c2f
BB
1144 * or you are privileged (checked in secpolicy...).
1145 *
1146 * The function returns 0 if remove access is granted.
1147 */
1148int
1149zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1150{
3558fd73 1151 uid_t uid;
572e2857
BB
1152 uid_t downer;
1153 uid_t fowner;
cae5b340 1154 zfsvfs_t *zfsvfs = ZTOZSB(zdp);
34dc7c2f 1155
cae5b340 1156 if (zfsvfs->z_replay)
34dc7c2f
BB
1157 return (0);
1158
428870ff 1159 if ((zdp->z_mode & S_ISVTX) == 0)
34dc7c2f
BB
1160 return (0);
1161
cae5b340
AX
1162 downer = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zdp)->i_uid),
1163 cr, ZFS_OWNER);
1164 fowner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zp)->i_uid),
1165 cr, ZFS_OWNER);
572e2857
BB
1166
1167 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
41d74433 1168 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)
34dc7c2f
BB
1169 return (0);
1170 else
1171 return (secpolicy_vnode_remove(cr));
1172}