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