Fix O_APPEND for Linux 3.15 and older kernels
[zfs.git] / module / os / linux / zfs / zfs_dir.c
blobc5b3b5ce7fc0aee3c75cc3cc8759e3ec9a3204ca
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
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.
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/sysmacros.h>
32 #include <sys/vfs.h>
33 #include <sys/vnode.h>
34 #include <sys/file.h>
35 #include <sys/kmem.h>
36 #include <sys/uio.h>
37 #include <sys/pathname.h>
38 #include <sys/cmn_err.h>
39 #include <sys/errno.h>
40 #include <sys/stat.h>
41 #include <sys/sunddi.h>
42 #include <sys/random.h>
43 #include <sys/policy.h>
44 #include <sys/zfs_dir.h>
45 #include <sys/zfs_acl.h>
46 #include <sys/zfs_vnops.h>
47 #include <sys/fs/zfs.h>
48 #include <sys/zap.h>
49 #include <sys/dmu.h>
50 #include <sys/atomic.h>
51 #include <sys/zfs_ctldir.h>
52 #include <sys/zfs_fuid.h>
53 #include <sys/sa.h>
54 #include <sys/zfs_sa.h>
55 #include <sys/dmu_objset.h>
56 #include <sys/dsl_dir.h>
59 * zfs_match_find() is used by zfs_dirent_lock() to perform zap lookups
60 * of names after deciding which is the appropriate lookup interface.
62 static int
63 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
64 matchtype_t mt, boolean_t update, int *deflags, pathname_t *rpnp,
65 uint64_t *zoid)
67 boolean_t conflict = B_FALSE;
68 int error;
70 if (zfsvfs->z_norm) {
71 size_t bufsz = 0;
72 char *buf = NULL;
74 if (rpnp) {
75 buf = rpnp->pn_buf;
76 bufsz = rpnp->pn_bufsize;
80 * In the non-mixed case we only expect there would ever
81 * be one match, but we need to use the normalizing lookup.
83 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
84 zoid, mt, buf, bufsz, &conflict);
85 } else {
86 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
90 * Allow multiple entries provided the first entry is
91 * the object id. Non-zpl consumers may safely make
92 * use of the additional space.
94 * XXX: This should be a feature flag for compatibility
96 if (error == EOVERFLOW)
97 error = 0;
99 if (zfsvfs->z_norm && !error && deflags)
100 *deflags = conflict ? ED_CASE_CONFLICT : 0;
102 *zoid = ZFS_DIRENT_OBJ(*zoid);
104 return (error);
108 * Lock a directory entry. A dirlock on <dzp, name> protects that name
109 * in dzp's directory zap object. As long as you hold a dirlock, you can
110 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
111 * can change the zap entry for (i.e. link or unlink) this name.
113 * Input arguments:
114 * dzp - znode for directory
115 * name - name of entry to lock
116 * flag - ZNEW: if the entry already exists, fail with EEXIST.
117 * ZEXISTS: if the entry does not exist, fail with ENOENT.
118 * ZSHARED: allow concurrent access with other ZSHARED callers.
119 * ZXATTR: we want dzp's xattr directory
120 * ZCILOOK: On a mixed sensitivity file system,
121 * this lookup should be case-insensitive.
122 * ZCIEXACT: On a purely case-insensitive file system,
123 * this lookup should be case-sensitive.
124 * ZRENAMING: we are locking for renaming, force narrow locks
125 * ZHAVELOCK: Don't grab the z_name_lock for this call. The
126 * current thread already holds it.
128 * Output arguments:
129 * zpp - pointer to the znode for the entry (NULL if there isn't one)
130 * dlpp - pointer to the dirlock for this entry (NULL on error)
131 * direntflags - (case-insensitive lookup only)
132 * flags if multiple case-sensitive matches exist in directory
133 * realpnp - (case-insensitive lookup only)
134 * actual name matched within the directory
136 * Return value: 0 on success or errno on failure.
138 * NOTE: Always checks for, and rejects, '.' and '..'.
139 * NOTE: For case-insensitive file systems we take wide locks (see below),
140 * but return znode pointers to a single match.
143 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name,
144 znode_t **zpp, int flag, int *direntflags, pathname_t *realpnp)
146 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
147 zfs_dirlock_t *dl;
148 boolean_t update;
149 matchtype_t mt = 0;
150 uint64_t zoid;
151 int error = 0;
152 int cmpflags;
154 *zpp = NULL;
155 *dlpp = NULL;
158 * Verify that we are not trying to lock '.', '..', or '.zfs'
160 if ((name[0] == '.' &&
161 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) ||
162 (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0))
163 return (SET_ERROR(EEXIST));
166 * Case sensitivity and normalization preferences are set when
167 * the file system is created. These are stored in the
168 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
169 * affect what vnodes can be cached in the DNLC, how we
170 * perform zap lookups, and the "width" of our dirlocks.
172 * A normal dirlock locks a single name. Note that with
173 * normalization a name can be composed multiple ways, but
174 * when normalized, these names all compare equal. A wide
175 * dirlock locks multiple names. We need these when the file
176 * system is supporting mixed-mode access. It is sometimes
177 * necessary to lock all case permutations of file name at
178 * once so that simultaneous case-insensitive/case-sensitive
179 * behaves as rationally as possible.
183 * When matching we may need to normalize & change case according to
184 * FS settings.
186 * Note that a normalized match is necessary for a case insensitive
187 * filesystem when the lookup request is not exact because normalization
188 * can fold case independent of normalizing code point sequences.
190 * See the table above zfs_dropname().
192 if (zfsvfs->z_norm != 0) {
193 mt = MT_NORMALIZE;
196 * Determine if the match needs to honor the case specified in
197 * lookup, and if so keep track of that so that during
198 * normalization we don't fold case.
200 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE &&
201 (flag & ZCIEXACT)) ||
202 (zfsvfs->z_case == ZFS_CASE_MIXED && !(flag & ZCILOOK))) {
203 mt |= MT_MATCH_CASE;
208 * Only look in or update the DNLC if we are looking for the
209 * name on a file system that does not require normalization
210 * or case folding. We can also look there if we happen to be
211 * on a non-normalizing, mixed sensitivity file system IF we
212 * are looking for the exact name.
214 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
215 * case for performance improvement?
217 update = !zfsvfs->z_norm ||
218 (zfsvfs->z_case == ZFS_CASE_MIXED &&
219 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
222 * ZRENAMING indicates we are in a situation where we should
223 * take narrow locks regardless of the file system's
224 * preferences for normalizing and case folding. This will
225 * prevent us deadlocking trying to grab the same wide lock
226 * twice if the two names happen to be case-insensitive
227 * matches.
229 if (flag & ZRENAMING)
230 cmpflags = 0;
231 else
232 cmpflags = zfsvfs->z_norm;
235 * Wait until there are no locks on this name.
237 * Don't grab the lock if it is already held. However, cannot
238 * have both ZSHARED and ZHAVELOCK together.
240 ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
241 if (!(flag & ZHAVELOCK))
242 rw_enter(&dzp->z_name_lock, RW_READER);
244 mutex_enter(&dzp->z_lock);
245 for (;;) {
246 if (dzp->z_unlinked && !(flag & ZXATTR)) {
247 mutex_exit(&dzp->z_lock);
248 if (!(flag & ZHAVELOCK))
249 rw_exit(&dzp->z_name_lock);
250 return (SET_ERROR(ENOENT));
252 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
253 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
254 U8_UNICODE_LATEST, &error) == 0) || error != 0)
255 break;
257 if (error != 0) {
258 mutex_exit(&dzp->z_lock);
259 if (!(flag & ZHAVELOCK))
260 rw_exit(&dzp->z_name_lock);
261 return (SET_ERROR(ENOENT));
263 if (dl == NULL) {
265 * Allocate a new dirlock and add it to the list.
267 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
268 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
269 dl->dl_name = name;
270 dl->dl_sharecnt = 0;
271 dl->dl_namelock = 0;
272 dl->dl_namesize = 0;
273 dl->dl_dzp = dzp;
274 dl->dl_next = dzp->z_dirlocks;
275 dzp->z_dirlocks = dl;
276 break;
278 if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
279 break;
280 cv_wait(&dl->dl_cv, &dzp->z_lock);
284 * If the z_name_lock was NOT held for this dirlock record it.
286 if (flag & ZHAVELOCK)
287 dl->dl_namelock = 1;
289 if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
291 * We're the second shared reference to dl. Make a copy of
292 * dl_name in case the first thread goes away before we do.
293 * Note that we initialize the new name before storing its
294 * pointer into dl_name, because the first thread may load
295 * dl->dl_name at any time. It'll either see the old value,
296 * which belongs to it, or the new shared copy; either is OK.
298 dl->dl_namesize = strlen(dl->dl_name) + 1;
299 name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
300 memcpy(name, dl->dl_name, dl->dl_namesize);
301 dl->dl_name = name;
304 mutex_exit(&dzp->z_lock);
307 * We have a dirlock on the name. (Note that it is the dirlock,
308 * not the dzp's z_lock, that protects the name in the zap object.)
309 * See if there's an object by this name; if so, put a hold on it.
311 if (flag & ZXATTR) {
312 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
313 sizeof (zoid));
314 if (error == 0)
315 error = (zoid == 0 ? SET_ERROR(ENOENT) : 0);
316 } else {
317 error = zfs_match_find(zfsvfs, dzp, name, mt,
318 update, direntflags, realpnp, &zoid);
320 if (error) {
321 if (error != ENOENT || (flag & ZEXISTS)) {
322 zfs_dirent_unlock(dl);
323 return (error);
325 } else {
326 if (flag & ZNEW) {
327 zfs_dirent_unlock(dl);
328 return (SET_ERROR(EEXIST));
330 error = zfs_zget(zfsvfs, zoid, zpp);
331 if (error) {
332 zfs_dirent_unlock(dl);
333 return (error);
337 *dlpp = dl;
339 return (0);
343 * Unlock this directory entry and wake anyone who was waiting for it.
345 void
346 zfs_dirent_unlock(zfs_dirlock_t *dl)
348 znode_t *dzp = dl->dl_dzp;
349 zfs_dirlock_t **prev_dl, *cur_dl;
351 mutex_enter(&dzp->z_lock);
353 if (!dl->dl_namelock)
354 rw_exit(&dzp->z_name_lock);
356 if (dl->dl_sharecnt > 1) {
357 dl->dl_sharecnt--;
358 mutex_exit(&dzp->z_lock);
359 return;
361 prev_dl = &dzp->z_dirlocks;
362 while ((cur_dl = *prev_dl) != dl)
363 prev_dl = &cur_dl->dl_next;
364 *prev_dl = dl->dl_next;
365 cv_broadcast(&dl->dl_cv);
366 mutex_exit(&dzp->z_lock);
368 if (dl->dl_namesize != 0)
369 kmem_free(dl->dl_name, dl->dl_namesize);
370 cv_destroy(&dl->dl_cv);
371 kmem_free(dl, sizeof (*dl));
375 * Look up an entry in a directory.
377 * NOTE: '.' and '..' are handled as special cases because
378 * no directory entries are actually stored for them. If this is
379 * the root of a filesystem, then '.zfs' is also treated as a
380 * special pseudo-directory.
383 zfs_dirlook(znode_t *dzp, char *name, znode_t **zpp, int flags,
384 int *deflg, pathname_t *rpnp)
386 zfs_dirlock_t *dl;
387 znode_t *zp;
388 struct inode *ip;
389 int error = 0;
390 uint64_t parent;
392 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
393 *zpp = dzp;
394 zhold(*zpp);
395 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
396 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
399 * If we are a snapshot mounted under .zfs, return
400 * the inode pointer for the snapshot directory.
402 if ((error = sa_lookup(dzp->z_sa_hdl,
403 SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
404 return (error);
406 if (parent == dzp->z_id && zfsvfs->z_parent != zfsvfs) {
407 error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
408 "snapshot", &ip, 0, kcred, NULL, NULL);
409 *zpp = ITOZ(ip);
410 return (error);
412 rw_enter(&dzp->z_parent_lock, RW_READER);
413 error = zfs_zget(zfsvfs, parent, &zp);
414 if (error == 0)
415 *zpp = zp;
416 rw_exit(&dzp->z_parent_lock);
417 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
418 ip = zfsctl_root(dzp);
419 *zpp = ITOZ(ip);
420 } else {
421 int zf;
423 zf = ZEXISTS | ZSHARED;
424 if (flags & FIGNORECASE)
425 zf |= ZCILOOK;
427 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
428 if (error == 0) {
429 *zpp = zp;
430 zfs_dirent_unlock(dl);
431 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
433 rpnp = NULL;
436 if ((flags & FIGNORECASE) && rpnp && !error)
437 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
439 return (error);
443 * unlinked Set (formerly known as the "delete queue") Error Handling
445 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
446 * don't specify the name of the entry that we will be manipulating. We
447 * also fib and say that we won't be adding any new entries to the
448 * unlinked set, even though we might (this is to lower the minimum file
449 * size that can be deleted in a full filesystem). So on the small
450 * chance that the nlink list is using a fat zap (ie. has more than
451 * 2000 entries), we *may* not pre-read a block that's needed.
452 * Therefore it is remotely possible for some of the assertions
453 * regarding the unlinked set below to fail due to i/o error. On a
454 * nondebug system, this will result in the space being leaked.
456 void
457 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
459 zfsvfs_t *zfsvfs = ZTOZSB(zp);
461 ASSERT(zp->z_unlinked);
462 ASSERT(ZTOI(zp)->i_nlink == 0);
464 VERIFY3U(0, ==,
465 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
467 dataset_kstats_update_nunlinks_kstat(&zfsvfs->z_kstat, 1);
471 * Clean up any znodes that had no links when we either crashed or
472 * (force) umounted the file system.
474 static void
475 zfs_unlinked_drain_task(void *arg)
477 zfsvfs_t *zfsvfs = arg;
478 zap_cursor_t zc;
479 zap_attribute_t zap;
480 dmu_object_info_t doi;
481 znode_t *zp;
482 int error;
484 ASSERT3B(zfsvfs->z_draining, ==, B_TRUE);
487 * Iterate over the contents of the unlinked set.
489 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
490 zap_cursor_retrieve(&zc, &zap) == 0 && !zfsvfs->z_drain_cancel;
491 zap_cursor_advance(&zc)) {
494 * See what kind of object we have in list
497 error = dmu_object_info(zfsvfs->z_os,
498 zap.za_first_integer, &doi);
499 if (error != 0)
500 continue;
502 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
503 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
505 * We need to re-mark these list entries for deletion,
506 * so we pull them back into core and set zp->z_unlinked.
508 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
511 * We may pick up znodes that are already marked for deletion.
512 * This could happen during the purge of an extended attribute
513 * directory. All we need to do is skip over them, since they
514 * are already in the system marked z_unlinked.
516 if (error != 0)
517 continue;
519 zp->z_unlinked = B_TRUE;
522 * zrele() decrements the znode's ref count and may cause
523 * it to be synchronously freed. We interrupt freeing
524 * of this znode by checking the return value of
525 * dmu_objset_zfs_unmounting() in dmu_free_long_range()
526 * when an unmount is requested.
528 zrele(zp);
529 ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
531 zap_cursor_fini(&zc);
533 zfsvfs->z_draining = B_FALSE;
534 zfsvfs->z_drain_task = TASKQID_INVALID;
538 * Sets z_draining then tries to dispatch async unlinked drain.
539 * If that fails executes synchronous unlinked drain.
541 void
542 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
544 ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
545 ASSERT3B(zfsvfs->z_draining, ==, B_FALSE);
547 zfsvfs->z_draining = B_TRUE;
548 zfsvfs->z_drain_cancel = B_FALSE;
550 zfsvfs->z_drain_task = taskq_dispatch(
551 dsl_pool_unlinked_drain_taskq(dmu_objset_pool(zfsvfs->z_os)),
552 zfs_unlinked_drain_task, zfsvfs, TQ_SLEEP);
553 if (zfsvfs->z_drain_task == TASKQID_INVALID) {
554 zfs_dbgmsg("async zfs_unlinked_drain dispatch failed");
555 zfs_unlinked_drain_task(zfsvfs);
560 * Wait for the unlinked drain taskq task to stop. This will interrupt the
561 * unlinked set processing if it is in progress.
563 void
564 zfs_unlinked_drain_stop_wait(zfsvfs_t *zfsvfs)
566 ASSERT3B(zfsvfs->z_unmounted, ==, B_FALSE);
568 if (zfsvfs->z_draining) {
569 zfsvfs->z_drain_cancel = B_TRUE;
570 taskq_cancel_id(dsl_pool_unlinked_drain_taskq(
571 dmu_objset_pool(zfsvfs->z_os)), zfsvfs->z_drain_task);
572 zfsvfs->z_drain_task = TASKQID_INVALID;
573 zfsvfs->z_draining = B_FALSE;
578 * Delete the entire contents of a directory. Return a count
579 * of the number of entries that could not be deleted. If we encounter
580 * an error, return a count of at least one so that the directory stays
581 * in the unlinked set.
583 * NOTE: this function assumes that the directory is inactive,
584 * so there is no need to lock its entries before deletion.
585 * Also, it assumes the directory contents is *only* regular
586 * files.
588 static int
589 zfs_purgedir(znode_t *dzp)
591 zap_cursor_t zc;
592 zap_attribute_t zap;
593 znode_t *xzp;
594 dmu_tx_t *tx;
595 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
596 zfs_dirlock_t dl;
597 int skipped = 0;
598 int error;
600 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
601 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
602 zap_cursor_advance(&zc)) {
603 error = zfs_zget(zfsvfs,
604 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
605 if (error) {
606 skipped += 1;
607 continue;
610 ASSERT(S_ISREG(ZTOI(xzp)->i_mode) ||
611 S_ISLNK(ZTOI(xzp)->i_mode));
613 tx = dmu_tx_create(zfsvfs->z_os);
614 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
615 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
616 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
617 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
618 /* Is this really needed ? */
619 zfs_sa_upgrade_txholds(tx, xzp);
620 dmu_tx_mark_netfree(tx);
621 error = dmu_tx_assign(tx, TXG_WAIT);
622 if (error) {
623 dmu_tx_abort(tx);
624 zfs_zrele_async(xzp);
625 skipped += 1;
626 continue;
628 memset(&dl, 0, sizeof (dl));
629 dl.dl_dzp = dzp;
630 dl.dl_name = zap.za_name;
632 error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
633 if (error)
634 skipped += 1;
635 dmu_tx_commit(tx);
637 zfs_zrele_async(xzp);
639 zap_cursor_fini(&zc);
640 if (error != ENOENT)
641 skipped += 1;
642 return (skipped);
645 void
646 zfs_rmnode(znode_t *zp)
648 zfsvfs_t *zfsvfs = ZTOZSB(zp);
649 objset_t *os = zfsvfs->z_os;
650 znode_t *xzp = NULL;
651 dmu_tx_t *tx;
652 uint64_t acl_obj;
653 uint64_t xattr_obj;
654 uint64_t links;
655 int error;
657 ASSERT(ZTOI(zp)->i_nlink == 0);
658 ASSERT(atomic_read(&ZTOI(zp)->i_count) == 0);
661 * If this is an attribute directory, purge its contents.
663 if (S_ISDIR(ZTOI(zp)->i_mode) && (zp->z_pflags & ZFS_XATTR)) {
664 if (zfs_purgedir(zp) != 0) {
666 * Not enough space to delete some xattrs.
667 * Leave it in the unlinked set.
669 zfs_znode_dmu_fini(zp);
671 return;
676 * Free up all the data in the file. We don't do this for directories
677 * because we need truncate and remove to be in the same tx, like in
678 * zfs_znode_delete(). Otherwise, if we crash here we'll end up with
679 * an inconsistent truncated zap object in the delete queue. Note a
680 * truncated file is harmless since it only contains user data.
682 if (S_ISREG(ZTOI(zp)->i_mode)) {
683 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
684 if (error) {
686 * Not enough space or we were interrupted by unmount.
687 * Leave the file in the unlinked set.
689 zfs_znode_dmu_fini(zp);
690 return;
695 * If the file has extended attributes, we're going to unlink
696 * the xattr dir.
698 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
699 &xattr_obj, sizeof (xattr_obj));
700 if (error == 0 && xattr_obj) {
701 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
702 ASSERT(error == 0);
705 acl_obj = zfs_external_acl(zp);
708 * Set up the final transaction.
710 tx = dmu_tx_create(os);
711 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
712 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
713 if (xzp) {
714 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
715 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
717 if (acl_obj)
718 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
720 zfs_sa_upgrade_txholds(tx, zp);
721 error = dmu_tx_assign(tx, TXG_WAIT);
722 if (error) {
724 * Not enough space to delete the file. Leave it in the
725 * unlinked set, leaking it until the fs is remounted (at
726 * which point we'll call zfs_unlinked_drain() to process it).
728 dmu_tx_abort(tx);
729 zfs_znode_dmu_fini(zp);
730 goto out;
733 if (xzp) {
734 ASSERT(error == 0);
735 mutex_enter(&xzp->z_lock);
736 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
737 clear_nlink(ZTOI(xzp)); /* no more links to it */
738 links = 0;
739 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
740 &links, sizeof (links), tx));
741 mutex_exit(&xzp->z_lock);
742 zfs_unlinked_add(xzp, tx);
745 mutex_enter(&os->os_dsl_dataset->ds_dir->dd_activity_lock);
748 * Remove this znode from the unlinked set. If a has rollback has
749 * occurred while a file is open and unlinked. Then when the file
750 * is closed post rollback it will not exist in the rolled back
751 * version of the unlinked object.
753 error = zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj,
754 zp->z_id, tx);
755 VERIFY(error == 0 || error == ENOENT);
757 uint64_t count;
758 if (zap_count(os, zfsvfs->z_unlinkedobj, &count) == 0 && count == 0) {
759 cv_broadcast(&os->os_dsl_dataset->ds_dir->dd_activity_cv);
762 mutex_exit(&os->os_dsl_dataset->ds_dir->dd_activity_lock);
764 dataset_kstats_update_nunlinked_kstat(&zfsvfs->z_kstat, 1);
766 zfs_znode_delete(zp, tx);
768 dmu_tx_commit(tx);
769 out:
770 if (xzp)
771 zfs_zrele_async(xzp);
774 static uint64_t
775 zfs_dirent(znode_t *zp, uint64_t mode)
777 uint64_t de = zp->z_id;
779 if (ZTOZSB(zp)->z_version >= ZPL_VERSION_DIRENT_TYPE)
780 de |= IFTODT(mode) << 60;
781 return (de);
785 * Link zp into dl. Can fail in the following cases :
786 * - if zp has been unlinked.
787 * - if the number of entries with the same hash (aka. colliding entries)
788 * exceed the capacity of a leaf-block of fatzap and splitting of the
789 * leaf-block does not help.
792 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
794 znode_t *dzp = dl->dl_dzp;
795 zfsvfs_t *zfsvfs = ZTOZSB(zp);
796 uint64_t value;
797 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
798 sa_bulk_attr_t bulk[5];
799 uint64_t mtime[2], ctime[2];
800 uint64_t links;
801 int count = 0;
802 int error;
804 mutex_enter(&zp->z_lock);
806 if (!(flag & ZRENAMING)) {
807 if (zp->z_unlinked) { /* no new links to unlinked zp */
808 ASSERT(!(flag & (ZNEW | ZEXISTS)));
809 mutex_exit(&zp->z_lock);
810 return (SET_ERROR(ENOENT));
812 if (!(flag & ZNEW)) {
814 * ZNEW nodes come from zfs_mknode() where the link
815 * count has already been initialised
817 inc_nlink(ZTOI(zp));
818 links = ZTOI(zp)->i_nlink;
819 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
820 NULL, &links, sizeof (links));
824 value = zfs_dirent(zp, zp->z_mode);
825 error = zap_add(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name, 8, 1,
826 &value, tx);
829 * zap_add could fail to add the entry if it exceeds the capacity of the
830 * leaf-block and zap_leaf_split() failed to help.
831 * The caller of this routine is responsible for failing the transaction
832 * which will rollback the SA updates done above.
834 if (error != 0) {
835 if (!(flag & ZRENAMING) && !(flag & ZNEW))
836 drop_nlink(ZTOI(zp));
837 mutex_exit(&zp->z_lock);
838 return (error);
841 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
842 &dzp->z_id, sizeof (dzp->z_id));
843 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
844 &zp->z_pflags, sizeof (zp->z_pflags));
846 if (!(flag & ZNEW)) {
847 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
848 ctime, sizeof (ctime));
849 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
850 ctime);
852 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
853 ASSERT(error == 0);
855 mutex_exit(&zp->z_lock);
857 mutex_enter(&dzp->z_lock);
858 dzp->z_size++;
859 if (zp_is_dir)
860 inc_nlink(ZTOI(dzp));
861 links = ZTOI(dzp)->i_nlink;
862 count = 0;
863 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
864 &dzp->z_size, sizeof (dzp->z_size));
865 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
866 &links, sizeof (links));
867 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
868 mtime, sizeof (mtime));
869 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
870 ctime, sizeof (ctime));
871 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
872 &dzp->z_pflags, sizeof (dzp->z_pflags));
873 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
874 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
875 ASSERT(error == 0);
876 mutex_exit(&dzp->z_lock);
878 return (0);
882 * The match type in the code for this function should conform to:
884 * ------------------------------------------------------------------------
885 * fs type | z_norm | lookup type | match type
886 * ---------|-------------|-------------|----------------------------------
887 * CS !norm | 0 | 0 | 0 (exact)
888 * CS norm | formX | 0 | MT_NORMALIZE
889 * CI !norm | upper | !ZCIEXACT | MT_NORMALIZE
890 * CI !norm | upper | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
891 * CI norm | upper|formX | !ZCIEXACT | MT_NORMALIZE
892 * CI norm | upper|formX | ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
893 * CM !norm | upper | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
894 * CM !norm | upper | ZCILOOK | MT_NORMALIZE
895 * CM norm | upper|formX | !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
896 * CM norm | upper|formX | ZCILOOK | MT_NORMALIZE
898 * Abbreviations:
899 * CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
900 * upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
901 * formX = unicode normalization form set on fs creation
903 static int
904 zfs_dropname(zfs_dirlock_t *dl, znode_t *zp, znode_t *dzp, dmu_tx_t *tx,
905 int flag)
907 int error;
909 if (ZTOZSB(zp)->z_norm) {
910 matchtype_t mt = MT_NORMALIZE;
912 if ((ZTOZSB(zp)->z_case == ZFS_CASE_INSENSITIVE &&
913 (flag & ZCIEXACT)) ||
914 (ZTOZSB(zp)->z_case == ZFS_CASE_MIXED &&
915 !(flag & ZCILOOK))) {
916 mt |= MT_MATCH_CASE;
919 error = zap_remove_norm(ZTOZSB(zp)->z_os, dzp->z_id,
920 dl->dl_name, mt, tx);
921 } else {
922 error = zap_remove(ZTOZSB(zp)->z_os, dzp->z_id, dl->dl_name,
923 tx);
926 return (error);
930 * Unlink zp from dl, and mark zp for deletion if this was the last link. Can
931 * fail if zp is a mount point (EBUSY) or a non-empty directory (ENOTEMPTY).
932 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
933 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
934 * and it's the caller's job to do it.
937 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
938 boolean_t *unlinkedp)
940 znode_t *dzp = dl->dl_dzp;
941 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
942 int zp_is_dir = S_ISDIR(ZTOI(zp)->i_mode);
943 boolean_t unlinked = B_FALSE;
944 sa_bulk_attr_t bulk[5];
945 uint64_t mtime[2], ctime[2];
946 uint64_t links;
947 int count = 0;
948 int error;
950 if (!(flag & ZRENAMING)) {
951 mutex_enter(&zp->z_lock);
953 if (zp_is_dir && !zfs_dirempty(zp)) {
954 mutex_exit(&zp->z_lock);
955 return (SET_ERROR(ENOTEMPTY));
959 * If we get here, we are going to try to remove the object.
960 * First try removing the name from the directory; if that
961 * fails, return the error.
963 error = zfs_dropname(dl, zp, dzp, tx, flag);
964 if (error != 0) {
965 mutex_exit(&zp->z_lock);
966 return (error);
969 if (ZTOI(zp)->i_nlink <= zp_is_dir) {
970 zfs_panic_recover("zfs: link count on %lu is %u, "
971 "should be at least %u", zp->z_id,
972 (int)ZTOI(zp)->i_nlink, zp_is_dir + 1);
973 set_nlink(ZTOI(zp), zp_is_dir + 1);
975 drop_nlink(ZTOI(zp));
976 if (ZTOI(zp)->i_nlink == zp_is_dir) {
977 zp->z_unlinked = B_TRUE;
978 clear_nlink(ZTOI(zp));
979 unlinked = B_TRUE;
980 } else {
981 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
982 NULL, &ctime, sizeof (ctime));
983 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
984 NULL, &zp->z_pflags, sizeof (zp->z_pflags));
985 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
986 ctime);
988 links = ZTOI(zp)->i_nlink;
989 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
990 NULL, &links, sizeof (links));
991 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
992 count = 0;
993 ASSERT(error == 0);
994 mutex_exit(&zp->z_lock);
995 } else {
996 error = zfs_dropname(dl, zp, dzp, tx, flag);
997 if (error != 0)
998 return (error);
1001 mutex_enter(&dzp->z_lock);
1002 dzp->z_size--; /* one dirent removed */
1003 if (zp_is_dir)
1004 drop_nlink(ZTOI(dzp)); /* ".." link from zp */
1005 links = ZTOI(dzp)->i_nlink;
1006 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
1007 NULL, &links, sizeof (links));
1008 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
1009 NULL, &dzp->z_size, sizeof (dzp->z_size));
1010 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
1011 NULL, ctime, sizeof (ctime));
1012 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
1013 NULL, mtime, sizeof (mtime));
1014 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
1015 NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
1016 zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime);
1017 error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
1018 ASSERT(error == 0);
1019 mutex_exit(&dzp->z_lock);
1021 if (unlinkedp != NULL)
1022 *unlinkedp = unlinked;
1023 else if (unlinked)
1024 zfs_unlinked_add(zp, tx);
1026 return (0);
1030 * Indicate whether the directory is empty. Works with or without z_lock
1031 * held, but can only be consider a hint in the latter case. Returns true
1032 * if only "." and ".." remain and there's no work in progress.
1034 * The internal ZAP size, rather than zp->z_size, needs to be checked since
1035 * some consumers (Lustre) do not strictly maintain an accurate SA_ZPL_SIZE.
1037 boolean_t
1038 zfs_dirempty(znode_t *dzp)
1040 zfsvfs_t *zfsvfs = ZTOZSB(dzp);
1041 uint64_t count;
1042 int error;
1044 if (dzp->z_dirlocks != NULL)
1045 return (B_FALSE);
1047 error = zap_count(zfsvfs->z_os, dzp->z_id, &count);
1048 if (error != 0 || count != 0)
1049 return (B_FALSE);
1051 return (B_TRUE);
1055 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, znode_t **xzpp, cred_t *cr)
1057 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1058 znode_t *xzp;
1059 dmu_tx_t *tx;
1060 int error;
1061 zfs_acl_ids_t acl_ids;
1062 boolean_t fuid_dirtied;
1063 #ifdef ZFS_DEBUG
1064 uint64_t parent;
1065 #endif
1067 *xzpp = NULL;
1069 if ((error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr)))
1070 return (error);
1072 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
1073 &acl_ids)) != 0)
1074 return (error);
1075 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zp->z_projid)) {
1076 zfs_acl_ids_free(&acl_ids);
1077 return (SET_ERROR(EDQUOT));
1080 tx = dmu_tx_create(zfsvfs->z_os);
1081 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1082 ZFS_SA_BASE_ATTR_SIZE);
1083 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1084 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1085 fuid_dirtied = zfsvfs->z_fuid_dirty;
1086 if (fuid_dirtied)
1087 zfs_fuid_txhold(zfsvfs, tx);
1088 error = dmu_tx_assign(tx, TXG_WAIT);
1089 if (error) {
1090 zfs_acl_ids_free(&acl_ids);
1091 dmu_tx_abort(tx);
1092 return (error);
1094 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
1096 if (fuid_dirtied)
1097 zfs_fuid_sync(zfsvfs, tx);
1099 #ifdef ZFS_DEBUG
1100 error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
1101 &parent, sizeof (parent));
1102 ASSERT(error == 0 && parent == zp->z_id);
1103 #endif
1105 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
1106 sizeof (xzp->z_id), tx));
1108 if (!zp->z_unlinked)
1109 zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp, xzp, "", NULL,
1110 acl_ids.z_fuidp, vap);
1112 zfs_acl_ids_free(&acl_ids);
1113 dmu_tx_commit(tx);
1115 *xzpp = xzp;
1117 return (0);
1121 * Return a znode for the extended attribute directory for zp.
1122 * ** If the directory does not already exist, it is created **
1124 * IN: zp - znode to obtain attribute directory from
1125 * cr - credentials of caller
1126 * flags - flags from the VOP_LOOKUP call
1128 * OUT: xipp - pointer to extended attribute znode
1130 * RETURN: 0 on success
1131 * error number on failure
1134 zfs_get_xattrdir(znode_t *zp, znode_t **xzpp, cred_t *cr, int flags)
1136 zfsvfs_t *zfsvfs = ZTOZSB(zp);
1137 znode_t *xzp;
1138 zfs_dirlock_t *dl;
1139 vattr_t va;
1140 int error;
1141 top:
1142 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
1143 if (error)
1144 return (error);
1146 if (xzp != NULL) {
1147 *xzpp = xzp;
1148 zfs_dirent_unlock(dl);
1149 return (0);
1152 if (!(flags & CREATE_XATTR_DIR)) {
1153 zfs_dirent_unlock(dl);
1154 return (SET_ERROR(ENOENT));
1157 if (zfs_is_readonly(zfsvfs)) {
1158 zfs_dirent_unlock(dl);
1159 return (SET_ERROR(EROFS));
1163 * The ability to 'create' files in an attribute
1164 * directory comes from the write_xattr permission on the base file.
1166 * The ability to 'search' an attribute directory requires
1167 * read_xattr permission on the base file.
1169 * Once in a directory the ability to read/write attributes
1170 * is controlled by the permissions on the attribute file.
1172 va.va_mask = ATTR_MODE | ATTR_UID | ATTR_GID;
1173 va.va_mode = S_IFDIR | S_ISVTX | 0777;
1174 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
1176 va.va_dentry = NULL;
1177 error = zfs_make_xattrdir(zp, &va, xzpp, cr);
1178 zfs_dirent_unlock(dl);
1180 if (error == ERESTART) {
1181 /* NB: we already did dmu_tx_wait() if necessary */
1182 goto top;
1185 return (error);
1189 * Decide whether it is okay to remove within a sticky directory.
1191 * In sticky directories, write access is not sufficient;
1192 * you can remove entries from a directory only if:
1194 * you own the directory,
1195 * you own the entry,
1196 * you have write access to the entry,
1197 * or you are privileged (checked in secpolicy...).
1199 * The function returns 0 if remove access is granted.
1202 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1204 uid_t uid;
1205 uid_t downer;
1206 uid_t fowner;
1207 zfsvfs_t *zfsvfs = ZTOZSB(zdp);
1209 if (zfsvfs->z_replay)
1210 return (0);
1212 if ((zdp->z_mode & S_ISVTX) == 0)
1213 return (0);
1215 downer = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zdp)->i_uid),
1216 cr, ZFS_OWNER);
1217 fowner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(ZTOI(zp)->i_uid),
1218 cr, ZFS_OWNER);
1220 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1221 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0)
1222 return (0);
1223 else
1224 return (secpolicy_vnode_remove(cr));