]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_dir.c
Put jail(2) under COMPAT_FREEBSD11. It has been the "old" way of creating
[FreeBSD/FreeBSD.git] / sys / cddl / contrib / opensolaris / uts / common / fs / 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, 2016 by Delphix. All rights reserved.
25  * Copyright 2017 Nexenta Systems, Inc.
26  */
27
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/time.h>
31 #include <sys/systm.h>
32 #include <sys/sysmacros.h>
33 #include <sys/resource.h>
34 #include <sys/vfs.h>
35 #include <sys/vnode.h>
36 #include <sys/file.h>
37 #include <sys/kmem.h>
38 #include <sys/uio.h>
39 #include <sys/cmn_err.h>
40 #include <sys/errno.h>
41 #include <sys/stat.h>
42 #include <sys/unistd.h>
43 #include <sys/sunddi.h>
44 #include <sys/random.h>
45 #include <sys/policy.h>
46 #include <sys/kcondvar.h>
47 #include <sys/callb.h>
48 #include <sys/smp.h>
49 #include <sys/zfs_dir.h>
50 #include <sys/zfs_acl.h>
51 #include <sys/fs/zfs.h>
52 #include <sys/zap.h>
53 #include <sys/dmu.h>
54 #include <sys/atomic.h>
55 #include <sys/zfs_ctldir.h>
56 #include <sys/zfs_fuid.h>
57 #include <sys/sa.h>
58 #include <sys/zfs_sa.h>
59 #include <sys/dnlc.h>
60 #include <sys/extdirent.h>
61
62 /*
63  * zfs_match_find() is used by zfs_dirent_lookup() to peform zap lookups
64  * of names after deciding which is the appropriate lookup interface.
65  */
66 static int
67 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, const char *name,
68     matchtype_t mt, uint64_t *zoid)
69 {
70         int error;
71
72         if (zfsvfs->z_norm) {
73
74                 /*
75                  * In the non-mixed case we only expect there would ever
76                  * be one match, but we need to use the normalizing lookup.
77                  */
78                 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
79                     zoid, mt, NULL, 0, NULL);
80         } else {
81                 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
82         }
83         *zoid = ZFS_DIRENT_OBJ(*zoid);
84
85         return (error);
86 }
87
88 /*
89  * Look up a directory entry under a locked vnode.
90  * dvp being locked gives us a guarantee that there are no concurrent
91  * modification of the directory and, thus, if a node can be found in
92  * the directory, then it must not be unlinked.
93  *
94  * Input arguments:
95  *      dzp     - znode for directory
96  *      name    - name of entry to lock
97  *      flag    - ZNEW: if the entry already exists, fail with EEXIST.
98  *                ZEXISTS: if the entry does not exist, fail with ENOENT.
99  *                ZXATTR: we want dzp's xattr directory
100  *
101  * Output arguments:
102  *      zpp     - pointer to the znode for the entry (NULL if there isn't one)
103  *
104  * Return value: 0 on success or errno on failure.
105  *
106  * NOTE: Always checks for, and rejects, '.' and '..'.
107  */
108 int
109 zfs_dirent_lookup(znode_t *dzp, const char *name, znode_t **zpp, int flag)
110 {
111         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
112         matchtype_t     mt = 0;
113         uint64_t        zoid;
114         vnode_t         *vp = NULL;
115         int             error = 0;
116
117         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
118
119         *zpp = NULL;
120
121         /*
122          * Verify that we are not trying to lock '.', '..', or '.zfs'
123          */
124         if (name[0] == '.' &&
125             (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
126             zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
127                 return (SET_ERROR(EEXIST));
128
129         /*
130          * Case sensitivity and normalization preferences are set when
131          * the file system is created.  These are stored in the
132          * zfsvfs->z_case and zfsvfs->z_norm fields.  These choices
133          * affect how we perform zap lookups.
134          *
135          * When matching we may need to normalize & change case according to
136          * FS settings.
137          *
138          * Note that a normalized match is necessary for a case insensitive
139          * filesystem when the lookup request is not exact because normalization
140          * can fold case independent of normalizing code point sequences.
141          *
142          * See the table above zfs_dropname().
143          */
144         if (zfsvfs->z_norm != 0) {
145                 mt = MT_NORMALIZE;
146
147                 /*
148                  * Determine if the match needs to honor the case specified in
149                  * lookup, and if so keep track of that so that during
150                  * normalization we don't fold case.
151                  */
152                 if (zfsvfs->z_case == ZFS_CASE_MIXED) {
153                         mt |= MT_MATCH_CASE;
154                 }
155         }
156
157         /*
158          * Only look in or update the DNLC if we are looking for the
159          * name on a file system that does not require normalization
160          * or case folding.  We can also look there if we happen to be
161          * on a non-normalizing, mixed sensitivity file system IF we
162          * are looking for the exact name.
163          *
164          * NB: we do not need to worry about this flag for ZFS_CASE_SENSITIVE
165          * because in that case MT_EXACT and MT_FIRST should produce exactly
166          * the same result.
167          */
168
169         if (dzp->z_unlinked && !(flag & ZXATTR))
170                 return (ENOENT);
171         if (flag & ZXATTR) {
172                 error = sa_lookup(dzp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &zoid,
173                     sizeof (zoid));
174                 if (error == 0)
175                         error = (zoid == 0 ? ENOENT : 0);
176         } else {
177                 error = zfs_match_find(zfsvfs, dzp, name, mt, &zoid);
178         }
179         if (error) {
180                 if (error != ENOENT || (flag & ZEXISTS)) {
181                         return (error);
182                 }
183         } else {
184                 if (flag & ZNEW) {
185                         return (SET_ERROR(EEXIST));
186                 }
187                 error = zfs_zget(zfsvfs, zoid, zpp);
188                 if (error)
189                         return (error);
190                 ASSERT(!(*zpp)->z_unlinked);
191         }
192
193         return (0);
194 }
195
196 static int
197 zfs_dd_lookup(znode_t *dzp, znode_t **zpp)
198 {
199         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
200         znode_t *zp;
201         uint64_t parent;
202         int error;
203
204         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
205         ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
206
207         if (dzp->z_unlinked)
208                 return (ENOENT);
209
210         if ((error = sa_lookup(dzp->z_sa_hdl,
211             SA_ZPL_PARENT(zfsvfs), &parent, sizeof (parent))) != 0)
212                 return (error);
213
214         error = zfs_zget(zfsvfs, parent, &zp);
215         if (error == 0)
216                 *zpp = zp;
217         return (error);
218 }
219
220 int
221 zfs_dirlook(znode_t *dzp, const char *name, znode_t **zpp)
222 {
223         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
224         znode_t *zp;
225         int error = 0;
226
227         ASSERT_VOP_LOCKED(ZTOV(dzp), __func__);
228         ASSERT(RRM_READ_HELD(&zfsvfs->z_teardown_lock));
229
230         if (dzp->z_unlinked)
231                 return (SET_ERROR(ENOENT));
232
233         if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
234                 *zpp = dzp;
235         } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
236                 error = zfs_dd_lookup(dzp, zpp);
237         } else {
238                 error = zfs_dirent_lookup(dzp, name, &zp, ZEXISTS);
239                 if (error == 0) {
240                         dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
241                         *zpp = zp;
242                 }
243         }
244         return (error);
245 }
246
247 /*
248  * unlinked Set (formerly known as the "delete queue") Error Handling
249  *
250  * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
251  * don't specify the name of the entry that we will be manipulating.  We
252  * also fib and say that we won't be adding any new entries to the
253  * unlinked set, even though we might (this is to lower the minimum file
254  * size that can be deleted in a full filesystem).  So on the small
255  * chance that the nlink list is using a fat zap (ie. has more than
256  * 2000 entries), we *may* not pre-read a block that's needed.
257  * Therefore it is remotely possible for some of the assertions
258  * regarding the unlinked set below to fail due to i/o error.  On a
259  * nondebug system, this will result in the space being leaked.
260  */
261 void
262 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
263 {
264         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
265
266         ASSERT(zp->z_unlinked);
267         ASSERT(zp->z_links == 0);
268
269         VERIFY3U(0, ==,
270             zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
271 }
272
273 /*
274  * Clean up any znodes that had no links when we either crashed or
275  * (force) umounted the file system.
276  */
277 void
278 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
279 {
280         zap_cursor_t    zc;
281         zap_attribute_t zap;
282         dmu_object_info_t doi;
283         znode_t         *zp;
284         dmu_tx_t        *tx;
285         int             error;
286
287         /*
288          * Interate over the contents of the unlinked set.
289          */
290         for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
291             zap_cursor_retrieve(&zc, &zap) == 0;
292             zap_cursor_advance(&zc)) {
293
294                 /*
295                  * See what kind of object we have in list
296                  */
297
298                 error = dmu_object_info(zfsvfs->z_os,
299                     zap.za_first_integer, &doi);
300                 if (error != 0)
301                         continue;
302
303                 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
304                     (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
305                 /*
306                  * We need to re-mark these list entries for deletion,
307                  * so we pull them back into core and set zp->z_unlinked.
308                  */
309                 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
310
311                 /*
312                  * We may pick up znodes that are already marked for deletion.
313                  * This could happen during the purge of an extended attribute
314                  * directory.  All we need to do is skip over them, since they
315                  * are already in the system marked z_unlinked.
316                  */
317                 if (error != 0)
318                         continue;
319
320                 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
321                 zp->z_unlinked = B_TRUE;
322 #if defined(__FreeBSD__)
323                 /*
324                  * Due to changes in zfs_rmnode we need to make sure the
325                  * link count is set to zero here.
326                  */
327                 zp->z_links = 0;
328                 tx = dmu_tx_create(zfsvfs->z_os);
329                 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
330                 VERIFY(0 == dmu_tx_assign(tx, TXG_WAIT));
331                 VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
332                     &zp->z_links, sizeof (zp->z_links), tx));
333                 dmu_tx_commit(tx);
334 #endif
335                 vput(ZTOV(zp));
336         }
337         zap_cursor_fini(&zc);
338 }
339
340 /*
341  * Delete the entire contents of a directory.  Return a count
342  * of the number of entries that could not be deleted. If we encounter
343  * an error, return a count of at least one so that the directory stays
344  * in the unlinked set.
345  *
346  * NOTE: this function assumes that the directory is inactive,
347  *      so there is no need to lock its entries before deletion.
348  *      Also, it assumes the directory contents is *only* regular
349  *      files.
350  */
351 static int
352 zfs_purgedir(znode_t *dzp)
353 {
354         zap_cursor_t    zc;
355         zap_attribute_t zap;
356         znode_t         *xzp;
357         dmu_tx_t        *tx;
358         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
359         int skipped = 0;
360         int error;
361
362         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
363             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
364             zap_cursor_advance(&zc)) {
365                 error = zfs_zget(zfsvfs,
366                     ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
367                 if (error) {
368                         skipped += 1;
369                         continue;
370                 }
371
372                 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
373                 ASSERT((ZTOV(xzp)->v_type == VREG) ||
374                     (ZTOV(xzp)->v_type == VLNK));
375
376                 tx = dmu_tx_create(zfsvfs->z_os);
377                 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
378                 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
379                 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
380                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
381                 /* Is this really needed ? */
382                 zfs_sa_upgrade_txholds(tx, xzp);
383                 dmu_tx_mark_netfree(tx);
384                 error = dmu_tx_assign(tx, TXG_WAIT);
385                 if (error) {
386                         dmu_tx_abort(tx);
387                         vput(ZTOV(xzp));
388                         skipped += 1;
389                         continue;
390                 }
391
392                 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
393                 if (error)
394                         skipped += 1;
395                 dmu_tx_commit(tx);
396
397                 vput(ZTOV(xzp));
398         }
399         zap_cursor_fini(&zc);
400         if (error != ENOENT)
401                 skipped += 1;
402         return (skipped);
403 }
404
405 void
406 zfs_rmnode(znode_t *zp)
407 {
408         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
409         objset_t        *os = zfsvfs->z_os;
410         dmu_tx_t        *tx;
411         uint64_t        acl_obj;
412         uint64_t        xattr_obj;
413         int             error;
414
415         ASSERT(zp->z_links == 0);
416         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
417
418         /*
419          * If this is an attribute directory, purge its contents.
420          */
421         if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
422             (zp->z_pflags & ZFS_XATTR)) {
423                 if (zfs_purgedir(zp) != 0) {
424                         /*
425                          * Not enough space to delete some xattrs.
426                          * Leave it in the unlinked set.
427                          */
428                         zfs_znode_dmu_fini(zp);
429                         zfs_znode_free(zp);
430                         return;
431                 }
432         } else {
433                 /*
434                  * Free up all the data in the file.  We don't do this for
435                  * XATTR directories because we need truncate and remove to be
436                  * in the same tx, like in zfs_znode_delete(). Otherwise, if
437                  * we crash here we'll end up with an inconsistent truncated
438                  * zap object in the delete queue.  Note a truncated file is
439                  * harmless since it only contains user data.
440                  */
441                 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
442                 if (error) {
443                         /*
444                          * Not enough space or we were interrupted by unmount.
445                          * Leave the file in the unlinked set.
446                          */
447                         zfs_znode_dmu_fini(zp);
448                         zfs_znode_free(zp);
449                         return;
450                 }
451         }
452
453         /*
454          * If the file has extended attributes, we're going to unlink
455          * the xattr dir.
456          */
457         error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
458             &xattr_obj, sizeof (xattr_obj));
459         if (error)
460                 xattr_obj = 0;
461
462         acl_obj = zfs_external_acl(zp);
463
464         /*
465          * Set up the final transaction.
466          */
467         tx = dmu_tx_create(os);
468         dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
469         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
470         if (xattr_obj)
471                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
472         if (acl_obj)
473                 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
474
475         zfs_sa_upgrade_txholds(tx, zp);
476         error = dmu_tx_assign(tx, TXG_WAIT);
477         if (error) {
478                 /*
479                  * Not enough space to delete the file.  Leave it in the
480                  * unlinked set, leaking it until the fs is remounted (at
481                  * which point we'll call zfs_unlinked_drain() to process it).
482                  */
483                 dmu_tx_abort(tx);
484                 zfs_znode_dmu_fini(zp);
485                 zfs_znode_free(zp);
486                 return;
487         }
488
489 #if defined(__FreeBSD__)
490         /*
491          * FreeBSD's implemention of zfs_zget requires a vnode to back it.
492          * This means that we could end up calling into getnewvnode while
493          * calling zfs_rmnode as a result of a prior call to getnewvnode
494          * trying to clear vnodes out of the cache. If this repeats we can
495          * recurse enough that we overflow our stack. To avoid this, we
496          * avoid calling zfs_zget on the xattr znode and instead simply add
497          * it to the unlinked set and schedule a call to zfs_unlinked_drain.
498          */
499         if (xattr_obj) {
500                 /* Add extended attribute directory to the unlinked set. */
501                 VERIFY3U(0, ==,
502                     zap_add_int(os, zfsvfs->z_unlinkedobj, xattr_obj, tx));
503         }
504 #else
505         if (xzp) {
506                 ASSERT(error == 0);
507                 xzp->z_unlinked = B_TRUE;       /* mark xzp for deletion */
508                 xzp->z_links = 0;       /* no more links to it */
509                 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
510                     &xzp->z_links, sizeof (xzp->z_links), tx));
511                 zfs_unlinked_add(xzp, tx);
512         }
513 #endif
514
515         /* Remove this znode from the unlinked set */
516         VERIFY3U(0, ==,
517             zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
518
519         zfs_znode_delete(zp, tx);
520
521         dmu_tx_commit(tx);
522
523         if (xattr_obj) {
524                 /*
525                  * We're using the FreeBSD taskqueue API here instead of
526                  * the Solaris taskq API since the FreeBSD API allows for a
527                  * task to be enqueued multiple times but executed once.
528                  */
529                 taskqueue_enqueue(system_taskq->tq_queue,
530                     &zfsvfs->z_unlinked_drain_task);
531         }
532 }
533
534 static uint64_t
535 zfs_dirent(znode_t *zp, uint64_t mode)
536 {
537         uint64_t de = zp->z_id;
538
539         if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
540                 de |= IFTODT(mode) << 60;
541         return (de);
542 }
543
544 /*
545  * Link zp into dzp.  Can only fail if zp has been unlinked.
546  */
547 int
548 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
549     int flag)
550 {
551         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
552         vnode_t *vp = ZTOV(zp);
553         uint64_t value;
554         int zp_is_dir = (vp->v_type == VDIR);
555         sa_bulk_attr_t bulk[5];
556         uint64_t mtime[2], ctime[2];
557         int count = 0;
558         int error;
559
560         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
561         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
562 #ifdef __FreeBSD__
563         if (zp_is_dir) {
564                 if (dzp->z_links >= ZFS_LINK_MAX)
565                         return (SET_ERROR(EMLINK));
566         }
567 #endif
568         if (!(flag & ZRENAMING)) {
569                 if (zp->z_unlinked) {   /* no new links to unlinked zp */
570                         ASSERT(!(flag & (ZNEW | ZEXISTS)));
571                         return (SET_ERROR(ENOENT));
572                 }
573 #ifdef __FreeBSD__
574                 if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) {
575                         return (SET_ERROR(EMLINK));
576                 }
577 #endif
578                 zp->z_links++;
579                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
580                     &zp->z_links, sizeof (zp->z_links));
581
582         } else {
583                 ASSERT(zp->z_unlinked == 0);
584         }
585         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
586             &dzp->z_id, sizeof (dzp->z_id));
587         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
588             &zp->z_pflags, sizeof (zp->z_pflags));
589
590         if (!(flag & ZNEW)) {
591                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
592                     ctime, sizeof (ctime));
593                 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
594                     ctime, B_TRUE);
595         }
596         error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
597         ASSERT0(error);
598
599         dzp->z_size++;
600         dzp->z_links += zp_is_dir;
601         count = 0;
602         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
603             &dzp->z_size, sizeof (dzp->z_size));
604         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
605             &dzp->z_links, sizeof (dzp->z_links));
606         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
607             mtime, sizeof (mtime));
608         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
609             ctime, sizeof (ctime));
610         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
611             &dzp->z_pflags, sizeof (dzp->z_pflags));
612         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
613         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
614         ASSERT0(error);
615
616         value = zfs_dirent(zp, zp->z_mode);
617         error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
618             8, 1, &value, tx);
619         VERIFY0(error);
620
621         return (0);
622 }
623
624 /*
625  * The match type in the code for this function should conform to:
626  *
627  * ------------------------------------------------------------------------
628  * fs type  | z_norm      | lookup type | match type
629  * ---------|-------------|-------------|----------------------------------
630  * CS !norm | 0           |           0 | 0 (exact)
631  * CS  norm | formX       |           0 | MT_NORMALIZE
632  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
633  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
634  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
635  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
636  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
637  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
638  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
639  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
640  *
641  * Abbreviations:
642  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
643  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
644  *    formX = unicode normalization form set on fs creation
645  */
646 static int
647 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
648     int flag)
649 {
650         int error;
651
652         if (zp->z_zfsvfs->z_norm) {
653                 matchtype_t mt = MT_NORMALIZE;
654
655                 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
656                         mt |= MT_MATCH_CASE;
657                 }
658
659                 error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
660                     name, mt, tx);
661         } else {
662                 error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
663         }
664
665         return (error);
666 }
667
668 /*
669  * Unlink zp from dzp, and mark zp for deletion if this was the last link.
670  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
671  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
672  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
673  * and it's the caller's job to do it.
674  */
675 int
676 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
677     int flag, boolean_t *unlinkedp)
678 {
679         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
680         vnode_t *vp = ZTOV(zp);
681         int zp_is_dir = (vp->v_type == VDIR);
682         boolean_t unlinked = B_FALSE;
683         sa_bulk_attr_t bulk[5];
684         uint64_t mtime[2], ctime[2];
685         int count = 0;
686         int error;
687
688         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
689         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
690
691         if (!(flag & ZRENAMING)) {
692
693                 if (zp_is_dir && !zfs_dirempty(zp)) {
694 #ifdef illumos
695                         return (SET_ERROR(EEXIST));
696 #else
697                         return (SET_ERROR(ENOTEMPTY));
698 #endif
699                 }
700
701                 /*
702                  * If we get here, we are going to try to remove the object.
703                  * First try removing the name from the directory; if that
704                  * fails, return the error.
705                  */
706                 error = zfs_dropname(dzp, name, zp, tx, flag);
707                 if (error != 0) {
708                         return (error);
709                 }
710
711                 if (zp->z_links <= zp_is_dir) {
712                         zfs_panic_recover("zfs: link count on vnode %p is %u, "
713                             "should be at least %u", zp->z_vnode,
714                             (int)zp->z_links,
715                             zp_is_dir + 1);
716                         zp->z_links = zp_is_dir + 1;
717                 }
718                 if (--zp->z_links == zp_is_dir) {
719                         zp->z_unlinked = B_TRUE;
720                         zp->z_links = 0;
721                         unlinked = B_TRUE;
722                 } else {
723                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
724                             NULL, &ctime, sizeof (ctime));
725                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
726                             NULL, &zp->z_pflags, sizeof (zp->z_pflags));
727                         zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
728                             B_TRUE);
729                 }
730                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
731                     NULL, &zp->z_links, sizeof (zp->z_links));
732                 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
733                 count = 0;
734                 ASSERT0(error);
735         } else {
736                 ASSERT(zp->z_unlinked == 0);
737                 error = zfs_dropname(dzp, name, zp, tx, flag);
738                 if (error != 0)
739                         return (error);
740         }
741
742         dzp->z_size--;          /* one dirent removed */
743         dzp->z_links -= zp_is_dir;      /* ".." link from zp */
744         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
745             NULL, &dzp->z_links, sizeof (dzp->z_links));
746         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
747             NULL, &dzp->z_size, sizeof (dzp->z_size));
748         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
749             NULL, ctime, sizeof (ctime));
750         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
751             NULL, mtime, sizeof (mtime));
752         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
753             NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
754         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
755         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
756         ASSERT0(error);
757
758         if (unlinkedp != NULL)
759                 *unlinkedp = unlinked;
760         else if (unlinked)
761                 zfs_unlinked_add(zp, tx);
762
763         return (0);
764 }
765
766 /*
767  * Indicate whether the directory is empty.
768  */
769 boolean_t
770 zfs_dirempty(znode_t *dzp)
771 {
772         return (dzp->z_size == 2);
773 }
774
775 int
776 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
777 {
778         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
779         znode_t *xzp;
780         dmu_tx_t *tx;
781         int error;
782         zfs_acl_ids_t acl_ids;
783         boolean_t fuid_dirtied;
784         uint64_t parent;
785
786         *xvpp = NULL;
787
788         /*
789          * In FreeBSD, access checking for creating an EA is being done
790          * in zfs_setextattr(),
791          */
792 #ifndef __FreeBSD_kernel__
793         if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
794                 return (error);
795 #endif
796
797         if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
798             &acl_ids)) != 0)
799                 return (error);
800         if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
801                 zfs_acl_ids_free(&acl_ids);
802                 return (SET_ERROR(EDQUOT));
803         }
804
805         getnewvnode_reserve(1);
806
807         tx = dmu_tx_create(zfsvfs->z_os);
808         dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
809             ZFS_SA_BASE_ATTR_SIZE);
810         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
811         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
812         fuid_dirtied = zfsvfs->z_fuid_dirty;
813         if (fuid_dirtied)
814                 zfs_fuid_txhold(zfsvfs, tx);
815         error = dmu_tx_assign(tx, TXG_WAIT);
816         if (error) {
817                 zfs_acl_ids_free(&acl_ids);
818                 dmu_tx_abort(tx);
819                 return (error);
820         }
821         zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
822
823         if (fuid_dirtied)
824                 zfs_fuid_sync(zfsvfs, tx);
825
826 #ifdef DEBUG
827         error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
828             &parent, sizeof (parent));
829         ASSERT(error == 0 && parent == zp->z_id);
830 #endif
831
832         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
833             sizeof (xzp->z_id), tx));
834
835         (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
836             xzp, "", NULL, acl_ids.z_fuidp, vap);
837
838         zfs_acl_ids_free(&acl_ids);
839         dmu_tx_commit(tx);
840
841         getnewvnode_drop_reserve();
842
843         *xvpp = ZTOV(xzp);
844
845         return (0);
846 }
847
848 /*
849  * Return a znode for the extended attribute directory for zp.
850  * ** If the directory does not already exist, it is created **
851  *
852  *      IN:     zp      - znode to obtain attribute directory from
853  *              cr      - credentials of caller
854  *              flags   - flags from the VOP_LOOKUP call
855  *
856  *      OUT:    xzpp    - pointer to extended attribute znode
857  *
858  *      RETURN: 0 on success
859  *              error number on failure
860  */
861 int
862 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
863 {
864         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
865         znode_t         *xzp;
866         vattr_t         va;
867         int             error;
868 top:
869         error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
870         if (error)
871                 return (error);
872
873         if (xzp != NULL) {
874                 *xvpp = ZTOV(xzp);
875                 return (0);
876         }
877
878
879         if (!(flags & CREATE_XATTR_DIR)) {
880 #ifdef illumos
881                 return (SET_ERROR(ENOENT));
882 #else
883                 return (SET_ERROR(ENOATTR));
884 #endif
885         }
886
887         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
888                 return (SET_ERROR(EROFS));
889         }
890
891         /*
892          * The ability to 'create' files in an attribute
893          * directory comes from the write_xattr permission on the base file.
894          *
895          * The ability to 'search' an attribute directory requires
896          * read_xattr permission on the base file.
897          *
898          * Once in a directory the ability to read/write attributes
899          * is controlled by the permissions on the attribute file.
900          */
901         va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
902         va.va_type = VDIR;
903         va.va_mode = S_IFDIR | S_ISVTX | 0777;
904         zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
905
906         error = zfs_make_xattrdir(zp, &va, xvpp, cr);
907
908         if (error == ERESTART) {
909                 /* NB: we already did dmu_tx_wait() if necessary */
910                 goto top;
911         }
912         if (error == 0)
913                 VOP_UNLOCK(*xvpp, 0);
914
915         return (error);
916 }
917
918 /*
919  * Decide whether it is okay to remove within a sticky directory.
920  *
921  * In sticky directories, write access is not sufficient;
922  * you can remove entries from a directory only if:
923  *
924  *      you own the directory,
925  *      you own the entry,
926  *      the entry is a plain file and you have write access,
927  *      or you are privileged (checked in secpolicy...).
928  *
929  * The function returns 0 if remove access is granted.
930  */
931 int
932 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
933 {
934         uid_t           uid;
935         uid_t           downer;
936         uid_t           fowner;
937         zfsvfs_t        *zfsvfs = zdp->z_zfsvfs;
938
939         if (zdp->z_zfsvfs->z_replay)
940                 return (0);
941
942         if ((zdp->z_mode & S_ISVTX) == 0)
943                 return (0);
944
945         downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
946         fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
947
948         if ((uid = crgetuid(cr)) == downer || uid == fowner ||
949             (ZTOV(zp)->v_type == VREG &&
950             zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
951                 return (0);
952         else
953                 return (secpolicy_vnode_remove(ZTOV(zp), cr));
954 }