]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zfs_dir.c
c5643c43a917e33f8afbc509b42c2a06905bfd97
[mirror_zfs.git] / module / zfs / zfs_dir.c
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, 2015 by Delphix. All rights reserved.
25 * Copyright 2017 Nexenta Systems, Inc.
26 */
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/zfs_vnops.h>
52 #include <sys/fs/zfs.h>
53 #include "fs/fs_subr.h"
54 #include <sys/zap.h>
55 #include <sys/dmu.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(zfs_sb_t *zsb, znode_t *dzp, char *name, matchtype_t mt,
70 boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
71 {
72 boolean_t conflict = B_FALSE;
73 int error;
74
75 if (zsb->z_norm) {
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(zsb->z_os, dzp->z_id, name, 8, 1,
89 zoid, mt, buf, bufsz, &conflict);
90 } else {
91 error = zap_lookup(zsb->z_os, dzp->z_id, name, 8, 1, zoid);
92 }
93
94 /*
95 * Allow multiple entries provided the first entry is
96 * the object id. Non-zpl consumers may safely make
97 * use of the additional space.
98 *
99 * XXX: This should be a feature flag for compatibility
100 */
101 if (error == EOVERFLOW)
102 error = 0;
103
104 if (zsb->z_norm && !error && deflags)
105 *deflags = conflict ? ED_CASE_CONFLICT : 0;
106
107 *zoid = ZFS_DIRENT_OBJ(*zoid);
108
109 #ifdef HAVE_DNLC
110 if (error == ENOENT && update)
111 dnlc_update(ZTOI(dzp), name, DNLC_NO_VNODE);
112 #endif /* HAVE_DNLC */
113
114 return (error);
115 }
116
117 /*
118 * Lock a directory entry. A dirlock on <dzp, name> protects that name
119 * in dzp's directory zap object. As long as you hold a dirlock, you can
120 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
121 * can change the zap entry for (i.e. link or unlink) this name.
122 *
123 * Input arguments:
124 * dzp - znode for directory
125 * name - name of entry to lock
126 * flag - ZNEW: if the entry already exists, fail with EEXIST.
127 * ZEXISTS: if the entry does not exist, fail with ENOENT.
128 * ZSHARED: allow concurrent access with other ZSHARED callers.
129 * ZXATTR: we want dzp's xattr directory
130 * ZCILOOK: On a mixed sensitivity file system,
131 * this lookup should be case-insensitive.
132 * ZCIEXACT: On a purely case-insensitive file system,
133 * this lookup should be case-sensitive.
134 * ZRENAMING: we are locking for renaming, force narrow locks
135 * ZHAVELOCK: Don't grab the z_name_lock for this call. The
136 * current thread already holds it.
137 *
138 * Output arguments:
139 * zpp - pointer to the znode for the entry (NULL if there isn't one)
140 * dlpp - pointer to the dirlock for this entry (NULL on error)
141 * direntflags - (case-insensitive lookup only)
142 * flags if multiple case-sensitive matches exist in directory
143 * realpnp - (case-insensitive lookup only)
144 * actual name matched within the directory
145 *
146 * Return value: 0 on success or errno on failure.
147 *
148 * NOTE: Always checks for, and rejects, '.' and '..'.
149 * NOTE: For case-insensitive file systems we take wide locks (see below),
150 * but return znode pointers to a single match.
151 */
152 int
153 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
154 int flag, int *direntflags, pathname_t *realpnp)
155 {
156 zfs_sb_t *zsb = ZTOZSB(dzp);
157 zfs_dirlock_t *dl;
158 boolean_t update;
159 matchtype_t mt = 0;
160 uint64_t zoid;
161 #ifdef HAVE_DNLC
162 vnode_t *vp = NULL;
163 #endif /* HAVE_DNLC */
164 int error = 0;
165 int cmpflags;
166
167 *zpp = NULL;
168 *dlpp = NULL;
169
170 /*
171 * Verify that we are not trying to lock '.', '..', or '.zfs'
172 */
173 if ((name[0] == '.' &&
174 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) ||
175 (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0))
176 return (SET_ERROR(EEXIST));
177
178 /*
179 * Case sensitivity and normalization preferences are set when
180 * the file system is created. These are stored in the
181 * zsb->z_case and zsb->z_norm fields. These choices
182 * affect what vnodes can be cached in the DNLC, how we
183 * perform zap lookups, and the "width" of our dirlocks.
184 *
185 * A normal dirlock locks a single name. Note that with
186 * normalization a name can be composed multiple ways, but
187 * when normalized, these names all compare equal. A wide
188 * dirlock locks multiple names. We need these when the file
189 * system is supporting mixed-mode access. It is sometimes
190 * necessary to lock all case permutations of file name at
191 * once so that simultaneous case-insensitive/case-sensitive
192 * behaves as rationally as possible.
193 */
194
195 /*
196 * When matching we may need to normalize & change case according to
197 * FS settings.
198 *
199 * Note that a normalized match is necessary for a case insensitive
200 * filesystem when the lookup request is not exact because normalization
201 * can fold case independent of normalizing code point sequences.
202 *
203 * See the table above zfs_dropname().
204 */
205 if (zsb->z_norm != 0) {
206 mt = MT_NORMALIZE;
207
208 /*
209 * Determine if the match needs to honor the case specified in
210 * lookup, and if so keep track of that so that during
211 * normalization we don't fold case.
212 */
213 if ((zsb->z_case == ZFS_CASE_INSENSITIVE &&
214 (flag & ZCIEXACT)) ||
215 (zsb->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
216 mt |= MT_MATCH_CASE;
217 }
218 }
219
220 /*
221 * Only look in or update the DNLC if we are looking for the
222 * name on a file system that does not require normalization
223 * or case folding. We can also look there if we happen to be
224 * on a non-normalizing, mixed sensitivity file system IF we
225 * are looking for the exact name.
226 *
227 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
228 * case for performance improvement?
229 */
230 update = !zsb->z_norm ||
231 (zsb->z_case == ZFS_CASE_MIXED &&
232 !(zsb->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
233
234 /*
235 * ZRENAMING indicates we are in a situation where we should
236 * take narrow locks regardless of the file system's
237 * preferences for normalizing and case folding. This will
238 * prevent us deadlocking trying to grab the same wide lock
239 * twice if the two names happen to be case-insensitive
240 * matches.
241 */
242 if (flag & ZRENAMING)
243 cmpflags = 0;
244 else
245 cmpflags = zsb->z_norm;
246
247 /*
248 * Wait until there are no locks on this name.
249 *
250 * Don't grab the the lock if it is already held. However, cannot
251 * have both ZSHARED and ZHAVELOCK together.
252 */
253 ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
254 if (!(flag & ZHAVELOCK))
255 rw_enter(&dzp->z_name_lock, RW_READER);
256
257 mutex_enter(&dzp->z_lock);
258 for (;;) {
259 if (dzp->z_unlinked && !(flag & ZXATTR)) {
260 mutex_exit(&dzp->z_lock);
261 if (!(flag & ZHAVELOCK))
262 rw_exit(&dzp->z_name_lock);
263 return (SET_ERROR(ENOENT));
264 }
265 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
266 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
267 U8_UNICODE_LATEST, &error) == 0) || error != 0)
268 break;
269 }
270 if (error != 0) {
271 mutex_exit(&dzp->z_lock);
272 if (!(flag & ZHAVELOCK))
273 rw_exit(&dzp->z_name_lock);
274 return (SET_ERROR(ENOENT));
275 }
276 if (dl == NULL) {
277 /*
278 * Allocate a new dirlock and add it to the list.
279 */
280 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
281 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
282 dl->dl_name = name;
283 dl->dl_sharecnt = 0;
284 dl->dl_namelock = 0;
285 dl->dl_namesize = 0;
286 dl->dl_dzp = dzp;
287 dl->dl_next = dzp->z_dirlocks;
288 dzp->z_dirlocks = dl;
289 break;
290 }
291 if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
292 break;
293 cv_wait(&dl->dl_cv, &dzp->z_lock);
294 }
295
296 /*
297 * If the z_name_lock was NOT held for this dirlock record it.
298 */
299 if (flag & ZHAVELOCK)
300 dl->dl_namelock = 1;
301
302 if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
303 /*
304 * We're the second shared reference to dl. Make a copy of
305 * dl_name in case the first thread goes away before we do.
306 * Note that we initialize the new name before storing its
307 * pointer into dl_name, because the first thread may load
308 * dl->dl_name at any time. He'll either see the old value,
309 * which is his, or the new shared copy; either is OK.
310 */
311 dl->dl_namesize = strlen(dl->dl_name) + 1;
312 name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
313 bcopy(dl->dl_name, name, dl->dl_namesize);
314 dl->dl_name = name;
315 }
316
317 mutex_exit(&dzp->z_lock);
318
319 /*
320 * We have a dirlock on the name. (Note that it is the dirlock,
321 * not the dzp's z_lock, that protects the name in the zap object.)
322 * See if there's an object by this name; if so, put a hold on it.
323 */
324 if (flag & ZXATTR) {
325 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zsb), &zoid,
326 sizeof (zoid));
327 if (error == 0)
328 error = (zoid == 0 ? SET_ERROR(ENOENT) : 0);
329 } else {
330 #ifdef HAVE_DNLC
331 if (update)
332 vp = dnlc_lookup(ZTOI(dzp), name);
333 if (vp == DNLC_NO_VNODE) {
334 iput(vp);
335 error = SET_ERROR(ENOENT);
336 } else if (vp) {
337 if (flag & ZNEW) {
338 zfs_dirent_unlock(dl);
339 iput(vp);
340 return (SET_ERROR(EEXIST));
341 }
342 *dlpp = dl;
343 *zpp = VTOZ(vp);
344 return (0);
345 } else {
346 error = zfs_match_find(zsb, dzp, name, mt,
347 update, direntflags, realpnp, &zoid);
348 }
349 #else
350 error = zfs_match_find(zsb, dzp, name, mt,
351 update, direntflags, realpnp, &zoid);
352 #endif /* HAVE_DNLC */
353 }
354 if (error) {
355 if (error != ENOENT || (flag & ZEXISTS)) {
356 zfs_dirent_unlock(dl);
357 return (error);
358 }
359 } else {
360 if (flag & ZNEW) {
361 zfs_dirent_unlock(dl);
362 return (SET_ERROR(EEXIST));
363 }
364 error = zfs_zget(zsb, zoid, zpp);
365 if (error) {
366 zfs_dirent_unlock(dl);
367 return (error);
368 }
369 #ifdef HAVE_DNLC
370 if (!(flag & ZXATTR) && update)
371 dnlc_update(ZTOI(dzp), name, ZTOI(*zpp));
372 #endif /* HAVE_DNLC */
373 }
374
375 *dlpp = dl;
376
377 return (0);
378 }
379
380 /*
381 * Unlock this directory entry and wake anyone who was waiting for it.
382 */
383 void
384 zfs_dirent_unlock(zfs_dirlock_t *dl)
385 {
386 znode_t *dzp = dl->dl_dzp;
387 zfs_dirlock_t **prev_dl, *cur_dl;
388
389 mutex_enter(&dzp->z_lock);
390
391 if (!dl->dl_namelock)
392 rw_exit(&dzp->z_name_lock);
393
394 if (dl->dl_sharecnt > 1) {
395 dl->dl_sharecnt--;
396 mutex_exit(&dzp->z_lock);
397 return;
398 }
399 prev_dl = &dzp->z_dirlocks;
400 while ((cur_dl = *prev_dl) != dl)
401 prev_dl = &cur_dl->dl_next;
402 *prev_dl = dl->dl_next;
403 cv_broadcast(&dl->dl_cv);
404 mutex_exit(&dzp->z_lock);
405
406 if (dl->dl_namesize != 0)
407 kmem_free(dl->dl_name, dl->dl_namesize);
408 cv_destroy(&dl->dl_cv);
409 kmem_free(dl, sizeof (*dl));
410 }
411
412 /*
413 * Look up an entry in a directory.
414 *
415 * NOTE: '.' and '..' are handled as special cases because
416 * no directory entries are actually stored for them. If this is
417 * the root of a filesystem, then '.zfs' is also treated as a
418 * special pseudo-directory.
419 */
420 int
421 zfs_dirlook(znode_t *dzp, char *name, struct inode **ipp, int flags,
422 int *deflg, pathname_t *rpnp)
423 {
424 zfs_dirlock_t *dl;
425 znode_t *zp;
426 int error = 0;
427 uint64_t parent;
428
429 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
430 *ipp = ZTOI(dzp);
431 igrab(*ipp);
432 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
433 zfs_sb_t *zsb = ZTOZSB(dzp);
434
435 /*
436 * If we are a snapshot mounted under .zfs, return
437 * the inode pointer for the snapshot directory.
438 */
439 if ((error = sa_lookup(dzp->z_sa_hdl,
440 SA_ZPL_PARENT(zsb), &parent, sizeof (parent))) != 0)
441 return (error);
442
443 if (parent == dzp->z_id && zsb->z_parent != zsb) {
444 error = zfsctl_root_lookup(zsb->z_parent->z_ctldir,
445 "snapshot", ipp, 0, kcred, NULL, NULL);
446 return (error);
447 }
448 rw_enter(&dzp->z_parent_lock, RW_READER);
449 error = zfs_zget(zsb, parent, &zp);
450 if (error == 0)
451 *ipp = ZTOI(zp);
452 rw_exit(&dzp->z_parent_lock);
453 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
454 *ipp = zfsctl_root(dzp);
455 } else {
456 int zf;
457
458 zf = ZEXISTS | ZSHARED;
459 if (flags & FIGNORECASE)
460 zf |= ZCILOOK;
461
462 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
463 if (error == 0) {
464 *ipp = ZTOI(zp);
465 zfs_dirent_unlock(dl);
466 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
467 }
468 rpnp = NULL;
469 }
470
471 if ((flags & FIGNORECASE) && rpnp && !error)
472 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
473
474 return (error);
475 }
476
477 /*
478 * unlinked Set (formerly known as the "delete queue") Error Handling
479 *
480 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
481 * don't specify the name of the entry that we will be manipulating. We
482 * also fib and say that we won't be adding any new entries to the
483 * unlinked set, even though we might (this is to lower the minimum file
484 * size that can be deleted in a full filesystem). So on the small
485 * chance that the nlink list is using a fat zap (ie. has more than
486 * 2000 entries), we *may* not pre-read a block that's needed.
487 * Therefore it is remotely possible for some of the assertions
488 * regarding the unlinked set below to fail due to i/o error. On a
489 * nondebug system, this will result in the space being leaked.
490 */
491 void
492 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
493 {
494 zfs_sb_t *zsb = ZTOZSB(zp);
495
496 ASSERT(zp->z_unlinked);
497 ASSERT(ZTOI(zp)->i_nlink == 0);
498
499 VERIFY3U(0, ==,
500 zap_add_int(zsb->z_os, zsb->z_unlinkedobj, zp->z_id, tx));
501 }
502
503 /*
504 * Clean up any znodes that had no links when we either crashed or
505 * (force) umounted the file system.
506 */
507 void
508 zfs_unlinked_drain(zfs_sb_t *zsb)
509 {
510 zap_cursor_t zc;
511 zap_attribute_t zap;
512 dmu_object_info_t doi;
513 znode_t *zp;
514 int error;
515
516 /*
517 * Iterate over the contents of the unlinked set.
518 */
519 for (zap_cursor_init(&zc, zsb->z_os, zsb->z_unlinkedobj);
520 zap_cursor_retrieve(&zc, &zap) == 0;
521 zap_cursor_advance(&zc)) {
522
523 /*
524 * See what kind of object we have in list
525 */
526
527 error = dmu_object_info(zsb->z_os, zap.za_first_integer, &doi);
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 */
537 error = zfs_zget(zsb, zap.za_first_integer, &zp);
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
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 */
565 static int
566 zfs_purgedir(znode_t *dzp)
567 {
568 zap_cursor_t zc;
569 zap_attribute_t zap;
570 znode_t *xzp;
571 dmu_tx_t *tx;
572 zfs_sb_t *zsb = ZTOZSB(dzp);
573 zfs_dirlock_t dl;
574 int skipped = 0;
575 int error;
576
577 for (zap_cursor_init(&zc, zsb->z_os, dzp->z_id);
578 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
579 zap_cursor_advance(&zc)) {
580 error = zfs_zget(zsb,
581 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
582 if (error) {
583 skipped += 1;
584 continue;
585 }
586
587 ASSERT(S_ISREG(ZTOI(xzp)->i_mode) ||
588 S_ISLNK(ZTOI(xzp)->i_mode));
589
590 tx = dmu_tx_create(zsb->z_os);
591 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
592 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
593 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
594 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
595 /* Is this really needed ? */
596 zfs_sa_upgrade_txholds(tx, xzp);
597 dmu_tx_mark_netfree(tx);
598 error = dmu_tx_assign(tx, TXG_WAIT);
599 if (error) {
600 dmu_tx_abort(tx);
601 zfs_iput_async(ZTOI(xzp));
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);
613
614 zfs_iput_async(ZTOI(xzp));
615 }
616 zap_cursor_fini(&zc);
617 if (error != ENOENT)
618 skipped += 1;
619 return (skipped);
620 }
621
622 void
623 zfs_rmnode(znode_t *zp)
624 {
625 zfs_sb_t *zsb = ZTOZSB(zp);
626 objset_t *os = zsb->z_os;
627 znode_t *xzp = NULL;
628 dmu_tx_t *tx;
629 uint64_t acl_obj;
630 uint64_t xattr_obj;
631 uint64_t links;
632 int error;
633
634 ASSERT(ZTOI(zp)->i_nlink == 0);
635 ASSERT(atomic_read(&ZTOI(zp)->i_count) == 0);
636
637 /*
638 * If this is an attribute directory, purge its contents.
639 */
640 if (S_ISDIR(ZTOI(zp)->i_mode) && (zp->z_pflags & ZFS_XATTR)) {
641 if (zfs_purgedir(zp) != 0) {
642 /*
643 * Not enough space to delete some xattrs.
644 * Leave it in the unlinked set.
645 */
646 zfs_znode_dmu_fini(zp);
647
648 return;
649 }
650 }
651
652 /*
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.
658 */
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 /*
663 * Not enough space or we were interrupted by unmount.
664 * Leave the file in the unlinked set.
665 */
666 zfs_znode_dmu_fini(zp);
667 return;
668 }
669 }
670
671 /*
672 * If the file has extended attributes, we're going to unlink
673 * the xattr dir.
674 */
675 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb),
676 &xattr_obj, sizeof (xattr_obj));
677 if (error == 0 && xattr_obj) {
678 error = zfs_zget(zsb, xattr_obj, &xzp);
679 ASSERT(error == 0);
680 }
681
682 acl_obj = zfs_external_acl(zp);
683
684 /*
685 * Set up the final transaction.
686 */
687 tx = dmu_tx_create(os);
688 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
689 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL);
690 if (xzp) {
691 dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, TRUE, NULL);
692 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
693 }
694 if (acl_obj)
695 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
696
697 zfs_sa_upgrade_txholds(tx, zp);
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);
707 goto out;
708 }
709
710 if (xzp) {
711 ASSERT(error == 0);
712 mutex_enter(&xzp->z_lock);
713 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
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(zsb),
717 &links, sizeof (links), tx));
718 mutex_exit(&xzp->z_lock);
719 zfs_unlinked_add(xzp, tx);
720 }
721
722 /* Remove this znode from the unlinked set */
723 VERIFY3U(0, ==,
724 zap_remove_int(zsb->z_os, zsb->z_unlinkedobj, zp->z_id, tx));
725
726 zfs_znode_delete(zp, tx);
727
728 dmu_tx_commit(tx);
729 out:
730 if (xzp)
731 zfs_iput_async(ZTOI(xzp));
732 }
733
734 static uint64_t
735 zfs_dirent(znode_t *zp, uint64_t mode)
736 {
737 uint64_t de = zp->z_id;
738
739 if (ZTOZSB(zp)->z_version >= ZPL_VERSION_DIRENT_TYPE)
740 de |= IFTODT(mode) << 60;
741 return (de);
742 }
743
744 /*
745 * Link zp into dl. Can only fail if zp has been unlinked.
746 */
747 int
748 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
749 {
750 znode_t *dzp = dl->dl_dzp;
751 zfs_sb_t *zsb = ZTOZSB(zp);
752 uint64_t value;
753 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
754 sa_bulk_attr_t bulk[5];
755 uint64_t mtime[2], ctime[2];
756 uint64_t links;
757 int count = 0;
758 int error;
759
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);
766 return (SET_ERROR(ENOENT));
767 }
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(zsb), NULL,
776 &links, sizeof (links));
777 }
778 }
779 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zsb), NULL,
780 &dzp->z_id, sizeof (dzp->z_id));
781 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
782 &zp->z_pflags, sizeof (zp->z_pflags));
783
784 if (!(flag & ZNEW)) {
785 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
786 ctime, sizeof (ctime));
787 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
788 ctime);
789 }
790 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
791 ASSERT(error == 0);
792
793 mutex_exit(&zp->z_lock);
794
795 mutex_enter(&dzp->z_lock);
796 dzp->z_size++;
797 if (zp_is_dir)
798 inc_nlink(ZTOI(dzp));
799 links = ZTOI(dzp)->i_nlink;
800 count = 0;
801 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb), NULL,
802 &dzp->z_size, sizeof (dzp->z_size));
803 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb), NULL,
804 &links, sizeof (links));
805 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL,
806 mtime, sizeof (mtime));
807 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL,
808 ctime, sizeof (ctime));
809 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL,
810 &dzp->z_pflags, sizeof (dzp->z_pflags));
811 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
812 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
813 ASSERT(error == 0);
814 mutex_exit(&dzp->z_lock);
815
816 value = zfs_dirent(zp, zp->z_mode);
817 error = zap_add(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
818 8, 1, &value, tx);
819 ASSERT(error == 0);
820
821 return (0);
822 }
823
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 */
846 static int
847 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
848 int flag)
849 {
850 int error;
851
852 if (ZTOZSB(zp)->z_norm) {
853 matchtype_t mt = MT_NORMALIZE;
854
855 if ((ZTOZSB(zp)->z_case == ZFS_CASE_INSENSITIVE &&
856 (flag & ZCIEXACT)) ||
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);
864 } else {
865 error = zap_remove(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
866 tx);
867 }
868
869 return (error);
870 }
871
872 /*
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).
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 */
879 int
880 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
881 boolean_t *unlinkedp)
882 {
883 znode_t *dzp = dl->dl_dzp;
884 zfs_sb_t *zsb = ZTOZSB(dzp);
885 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
886 boolean_t unlinked = B_FALSE;
887 sa_bulk_attr_t bulk[5];
888 uint64_t mtime[2], ctime[2];
889 uint64_t links;
890 int count = 0;
891 int error;
892
893 #ifdef HAVE_DNLC
894 dnlc_remove(ZTOI(dzp), dl->dl_name);
895 #endif /* HAVE_DNLC */
896
897 if (!(flag & ZRENAMING)) {
898 mutex_enter(&zp->z_lock);
899
900 if (zp_is_dir && !zfs_dirempty(zp)) {
901 mutex_exit(&zp->z_lock);
902 return (SET_ERROR(ENOTEMPTY));
903 }
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);
913 return (error);
914 }
915
916 if (ZTOI(zp)->i_nlink <= zp_is_dir) {
917 zfs_panic_recover("zfs: link count on %lu is %u, "
918 "should be at least %u", zp->z_id,
919 (int)ZTOI(zp)->i_nlink, zp_is_dir + 1);
920 set_nlink(ZTOI(zp), zp_is_dir + 1);
921 }
922 drop_nlink(ZTOI(zp));
923 if (ZTOI(zp)->i_nlink == zp_is_dir) {
924 zp->z_unlinked = B_TRUE;
925 clear_nlink(ZTOI(zp));
926 unlinked = B_TRUE;
927 } else {
928 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb),
929 NULL, &ctime, sizeof (ctime));
930 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
931 NULL, &zp->z_pflags, sizeof (zp->z_pflags));
932 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
933 ctime);
934 }
935 links = ZTOI(zp)->i_nlink;
936 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zsb),
937 NULL, &links, sizeof (links));
938 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
939 count = 0;
940 ASSERT(error == 0);
941 mutex_exit(&zp->z_lock);
942 } else {
943 error = zfs_dropname(dl, zp, dzp, tx, flag);
944 if (error != 0)
945 return (error);
946 }
947
948 mutex_enter(&dzp->z_lock);
949 dzp->z_size--; /* one dirent removed */
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(zsb),
954 NULL, &links, sizeof (links));
955 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zsb),
956 NULL, &dzp->z_size, sizeof (dzp->z_size));
957 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb),
958 NULL, ctime, sizeof (ctime));
959 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb),
960 NULL, mtime, sizeof (mtime));
961 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb),
962 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
963 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
964 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
965 ASSERT(error == 0);
966 mutex_exit(&dzp->z_lock);
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.
980 */
981 boolean_t
982 zfs_dirempty(znode_t *dzp)
983 {
984 return (dzp->z_size == 2 && dzp->z_dirlocks == 0);
985 }
986
987 int
988 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, struct inode **xipp, cred_t *cr)
989 {
990 zfs_sb_t *zsb = ZTOZSB(zp);
991 znode_t *xzp;
992 dmu_tx_t *tx;
993 int error;
994 zfs_acl_ids_t acl_ids;
995 boolean_t fuid_dirtied;
996 #ifdef DEBUG
997 uint64_t parent;
998 #endif
999
1000 *xipp = NULL;
1001
1002 if ((error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)))
1003 return (error);
1004
1005 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
1006 &acl_ids)) != 0)
1007 return (error);
1008 if (zfs_acl_ids_overquota(zsb, &acl_ids)) {
1009 zfs_acl_ids_free(&acl_ids);
1010 return (SET_ERROR(EDQUOT));
1011 }
1012
1013 tx = dmu_tx_create(zsb->z_os);
1014 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1015 ZFS_SA_BASE_ATTR_SIZE);
1016 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1017 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1018 fuid_dirtied = zsb->z_fuid_dirty;
1019 if (fuid_dirtied)
1020 zfs_fuid_txhold(zsb, tx);
1021 error = dmu_tx_assign(tx, TXG_WAIT);
1022 if (error) {
1023 zfs_acl_ids_free(&acl_ids);
1024 dmu_tx_abort(tx);
1025 return (error);
1026 }
1027 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1028
1029 if (fuid_dirtied)
1030 zfs_fuid_sync(zsb, tx);
1031
1032 #ifdef DEBUG
1033 error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zsb),
1034 &parent, sizeof (parent));
1035 ASSERT(error == 0 && parent == zp->z_id);
1036 #endif
1037
1038 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zsb), &xzp->z_id,
1039 sizeof (xzp->z_id), tx));
1040
1041 if (!zp->z_unlinked)
1042 (void) zfs_log_create(zsb->z_log, tx, TX_MKXATTR, zp,
1043 xzp, "", NULL, acl_ids.z_fuidp, vap);
1044
1045 zfs_acl_ids_free(&acl_ids);
1046 dmu_tx_commit(tx);
1047
1048 *xipp = ZTOI(xzp);
1049
1050 return (0);
1051 }
1052
1053 /*
1054 * Return a znode for the extended attribute directory for zp.
1055 * ** If the directory does not already exist, it is created **
1056 *
1057 * IN: zp - znode to obtain attribute directory from
1058 * cr - credentials of caller
1059 * flags - flags from the VOP_LOOKUP call
1060 *
1061 * OUT: xipp - pointer to extended attribute znode
1062 *
1063 * RETURN: 0 on success
1064 * error number on failure
1065 */
1066 int
1067 zfs_get_xattrdir(znode_t *zp, struct inode **xipp, cred_t *cr, int flags)
1068 {
1069 zfs_sb_t *zsb = ZTOZSB(zp);
1070 znode_t *xzp;
1071 zfs_dirlock_t *dl;
1072 vattr_t va;
1073 int error;
1074 top:
1075 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1076 if (error)
1077 return (error);
1078
1079 if (xzp != NULL) {
1080 *xipp = ZTOI(xzp);
1081 zfs_dirent_unlock(dl);
1082 return (0);
1083 }
1084
1085 if (!(flags & CREATE_XATTR_DIR)) {
1086 zfs_dirent_unlock(dl);
1087 return (SET_ERROR(ENOENT));
1088 }
1089
1090 if (zfs_is_readonly(zsb)) {
1091 zfs_dirent_unlock(dl);
1092 return (SET_ERROR(EROFS));
1093 }
1094
1095 /*
1096 * The ability to 'create' files in an attribute
1097 * directory comes from the write_xattr permission on the base file.
1098 *
1099 * The ability to 'search' an attribute directory requires
1100 * read_xattr permission on the base file.
1101 *
1102 * Once in a directory the ability to read/write attributes
1103 * is controlled by the permissions on the attribute file.
1104 */
1105 va.va_mask = ATTR_MODE | ATTR_UID | ATTR_GID;
1106 va.va_mode = S_IFDIR | S_ISVTX | 0777;
1107 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1108
1109 va.va_dentry = NULL;
1110 error = zfs_make_xattrdir(zp, &va, xipp, cr);
1111 zfs_dirent_unlock(dl);
1112
1113 if (error == ERESTART) {
1114 /* NB: we already did dmu_tx_wait() if necessary */
1115 goto top;
1116 }
1117
1118 return (error);
1119 }
1120
1121 /*
1122 * Decide whether it is okay to remove within a sticky directory.
1123 *
1124 * In sticky directories, write access is not sufficient;
1125 * you can remove entries from a directory only if:
1126 *
1127 * you own the directory,
1128 * you own the entry,
1129 * the entry is a plain file and you have write access,
1130 * or you are privileged (checked in secpolicy...).
1131 *
1132 * The function returns 0 if remove access is granted.
1133 */
1134 int
1135 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1136 {
1137 uid_t uid;
1138 uid_t downer;
1139 uid_t fowner;
1140 zfs_sb_t *zsb = ZTOZSB(zdp);
1141
1142 if (zsb->z_replay)
1143 return (0);
1144
1145 if ((zdp->z_mode & S_ISVTX) == 0)
1146 return (0);
1147
1148 downer = zfs_fuid_map_id(zsb, KUID_TO_SUID(ZTOI(zdp)->i_uid),
1149 cr, ZFS_OWNER);
1150 fowner = zfs_fuid_map_id(zsb, KUID_TO_SUID(ZTOI(zp)->i_uid),
1151 cr, ZFS_OWNER);
1152
1153 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1154 (S_ISDIR(ZTOI(zp)->i_mode) &&
1155 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1156 return (0);
1157 else
1158 return (secpolicy_vnode_remove(cr));
1159 }