]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/cddl/contrib/opensolaris/uts/common/fs/zfs/zfs_dir.c
MFC r316910: 7812 Remove gender specific language
[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         int             error;
285
286         /*
287          * Interate over the contents of the unlinked set.
288          */
289         for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
290             zap_cursor_retrieve(&zc, &zap) == 0;
291             zap_cursor_advance(&zc)) {
292
293                 /*
294                  * See what kind of object we have in list
295                  */
296
297                 error = dmu_object_info(zfsvfs->z_os,
298                     zap.za_first_integer, &doi);
299                 if (error != 0)
300                         continue;
301
302                 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
303                     (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
304                 /*
305                  * We need to re-mark these list entries for deletion,
306                  * so we pull them back into core and set zp->z_unlinked.
307                  */
308                 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
309
310                 /*
311                  * We may pick up znodes that are already marked for deletion.
312                  * This could happen during the purge of an extended attribute
313                  * directory.  All we need to do is skip over them, since they
314                  * are already in the system marked z_unlinked.
315                  */
316                 if (error != 0)
317                         continue;
318
319                 vn_lock(ZTOV(zp), LK_EXCLUSIVE | LK_RETRY);
320                 zp->z_unlinked = B_TRUE;
321                 vput(ZTOV(zp));
322         }
323         zap_cursor_fini(&zc);
324 }
325
326 /*
327  * Delete the entire contents of a directory.  Return a count
328  * of the number of entries that could not be deleted. If we encounter
329  * an error, return a count of at least one so that the directory stays
330  * in the unlinked set.
331  *
332  * NOTE: this function assumes that the directory is inactive,
333  *      so there is no need to lock its entries before deletion.
334  *      Also, it assumes the directory contents is *only* regular
335  *      files.
336  */
337 static int
338 zfs_purgedir(znode_t *dzp)
339 {
340         zap_cursor_t    zc;
341         zap_attribute_t zap;
342         znode_t         *xzp;
343         dmu_tx_t        *tx;
344         zfsvfs_t        *zfsvfs = dzp->z_zfsvfs;
345         int skipped = 0;
346         int error;
347
348         for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
349             (error = zap_cursor_retrieve(&zc, &zap)) == 0;
350             zap_cursor_advance(&zc)) {
351                 error = zfs_zget(zfsvfs,
352                     ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
353                 if (error) {
354                         skipped += 1;
355                         continue;
356                 }
357
358                 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
359                 ASSERT((ZTOV(xzp)->v_type == VREG) ||
360                     (ZTOV(xzp)->v_type == VLNK));
361
362                 tx = dmu_tx_create(zfsvfs->z_os);
363                 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
364                 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
365                 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
366                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
367                 /* Is this really needed ? */
368                 zfs_sa_upgrade_txholds(tx, xzp);
369                 dmu_tx_mark_netfree(tx);
370                 error = dmu_tx_assign(tx, TXG_WAIT);
371                 if (error) {
372                         dmu_tx_abort(tx);
373                         vput(ZTOV(xzp));
374                         skipped += 1;
375                         continue;
376                 }
377
378                 error = zfs_link_destroy(dzp, zap.za_name, xzp, tx, 0, NULL);
379                 if (error)
380                         skipped += 1;
381                 dmu_tx_commit(tx);
382
383                 vput(ZTOV(xzp));
384         }
385         zap_cursor_fini(&zc);
386         if (error != ENOENT)
387                 skipped += 1;
388         return (skipped);
389 }
390
391 void
392 zfs_rmnode(znode_t *zp)
393 {
394         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
395         objset_t        *os = zfsvfs->z_os;
396         znode_t         *xzp = NULL;
397         dmu_tx_t        *tx;
398         uint64_t        acl_obj;
399         uint64_t        xattr_obj;
400         int             error;
401
402         ASSERT(zp->z_links == 0);
403         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
404
405         /*
406          * If this is an attribute directory, purge its contents.
407          */
408         if (ZTOV(zp) != NULL && ZTOV(zp)->v_type == VDIR &&
409             (zp->z_pflags & ZFS_XATTR)) {
410                 if (zfs_purgedir(zp) != 0) {
411                         /*
412                          * Not enough space to delete some xattrs.
413                          * Leave it in the unlinked set.
414                          */
415                         zfs_znode_dmu_fini(zp);
416                         zfs_znode_free(zp);
417                         return;
418                 }
419         } else {
420                 /*
421                  * Free up all the data in the file.  We don't do this for
422                  * XATTR directories because we need truncate and remove to be
423                  * in the same tx, like in zfs_znode_delete(). Otherwise, if
424                  * we crash here we'll end up with an inconsistent truncated
425                  * zap object in the delete queue.  Note a truncated file is
426                  * harmless since it only contains user data.
427                  */
428                 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
429                 if (error) {
430                         /*
431                          * Not enough space or we were interrupted by unmount.
432                          * Leave the file in the unlinked set.
433                          */
434                         zfs_znode_dmu_fini(zp);
435                         zfs_znode_free(zp);
436                         return;
437                 }
438         }
439
440         /*
441          * If the file has extended attributes, we're going to unlink
442          * the xattr dir.
443          */
444         error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
445             &xattr_obj, sizeof (xattr_obj));
446         if (error == 0 && xattr_obj) {
447                 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
448                 ASSERT3S(error, ==, 0);
449                 vn_lock(ZTOV(xzp), LK_EXCLUSIVE | LK_RETRY);
450         }
451
452         acl_obj = zfs_external_acl(zp);
453
454         /*
455          * Set up the final transaction.
456          */
457         tx = dmu_tx_create(os);
458         dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
459         dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
460         if (xzp) {
461                 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
462                 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
463         }
464         if (acl_obj)
465                 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
466
467         zfs_sa_upgrade_txholds(tx, zp);
468         error = dmu_tx_assign(tx, TXG_WAIT);
469         if (error) {
470                 /*
471                  * Not enough space to delete the file.  Leave it in the
472                  * unlinked set, leaking it until the fs is remounted (at
473                  * which point we'll call zfs_unlinked_drain() to process it).
474                  */
475                 dmu_tx_abort(tx);
476                 zfs_znode_dmu_fini(zp);
477                 zfs_znode_free(zp);
478                 goto out;
479         }
480
481         if (xzp) {
482                 ASSERT(error == 0);
483                 xzp->z_unlinked = B_TRUE;       /* mark xzp for deletion */
484                 xzp->z_links = 0;       /* no more links to it */
485                 VERIFY(0 == sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
486                     &xzp->z_links, sizeof (xzp->z_links), tx));
487                 zfs_unlinked_add(xzp, tx);
488         }
489
490         /* Remove this znode from the unlinked set */
491         VERIFY3U(0, ==,
492             zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
493
494         zfs_znode_delete(zp, tx);
495
496         dmu_tx_commit(tx);
497 out:
498         if (xzp)
499                 vput(ZTOV(xzp));
500 }
501
502 static uint64_t
503 zfs_dirent(znode_t *zp, uint64_t mode)
504 {
505         uint64_t de = zp->z_id;
506
507         if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
508                 de |= IFTODT(mode) << 60;
509         return (de);
510 }
511
512 /*
513  * Link zp into dzp.  Can only fail if zp has been unlinked.
514  */
515 int
516 zfs_link_create(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
517     int flag)
518 {
519         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
520         vnode_t *vp = ZTOV(zp);
521         uint64_t value;
522         int zp_is_dir = (vp->v_type == VDIR);
523         sa_bulk_attr_t bulk[5];
524         uint64_t mtime[2], ctime[2];
525         int count = 0;
526         int error;
527
528         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
529         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
530 #ifdef __FreeBSD__
531         if (zp_is_dir) {
532                 if (dzp->z_links >= ZFS_LINK_MAX)
533                         return (SET_ERROR(EMLINK));
534         }
535 #endif
536         if (!(flag & ZRENAMING)) {
537                 if (zp->z_unlinked) {   /* no new links to unlinked zp */
538                         ASSERT(!(flag & (ZNEW | ZEXISTS)));
539                         return (SET_ERROR(ENOENT));
540                 }
541 #ifdef __FreeBSD__
542                 if (zp->z_links >= ZFS_LINK_MAX - zp_is_dir) {
543                         return (SET_ERROR(EMLINK));
544                 }
545 #endif
546                 zp->z_links++;
547                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
548                     &zp->z_links, sizeof (zp->z_links));
549
550         } else {
551                 ASSERT(zp->z_unlinked == 0);
552         }
553         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PARENT(zfsvfs), NULL,
554             &dzp->z_id, sizeof (dzp->z_id));
555         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
556             &zp->z_pflags, sizeof (zp->z_pflags));
557
558         if (!(flag & ZNEW)) {
559                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
560                     ctime, sizeof (ctime));
561                 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime,
562                     ctime, B_TRUE);
563         }
564         error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
565         ASSERT0(error);
566
567         dzp->z_size++;
568         dzp->z_links += zp_is_dir;
569         count = 0;
570         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
571             &dzp->z_size, sizeof (dzp->z_size));
572         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs), NULL,
573             &dzp->z_links, sizeof (dzp->z_links));
574         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
575             mtime, sizeof (mtime));
576         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
577             ctime, sizeof (ctime));
578         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
579             &dzp->z_pflags, sizeof (dzp->z_pflags));
580         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
581         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
582         ASSERT0(error);
583
584         value = zfs_dirent(zp, zp->z_mode);
585         error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, name,
586             8, 1, &value, tx);
587         VERIFY0(error);
588
589         return (0);
590 }
591
592 /*
593  * The match type in the code for this function should conform to:
594  *
595  * ------------------------------------------------------------------------
596  * fs type  | z_norm      | lookup type | match type
597  * ---------|-------------|-------------|----------------------------------
598  * CS !norm | 0           |           0 | 0 (exact)
599  * CS  norm | formX       |           0 | MT_NORMALIZE
600  * CI !norm | upper       |   !ZCIEXACT | MT_NORMALIZE
601  * CI !norm | upper       |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
602  * CI  norm | upper|formX |   !ZCIEXACT | MT_NORMALIZE
603  * CI  norm | upper|formX |    ZCIEXACT | MT_NORMALIZE | MT_MATCH_CASE
604  * CM !norm | upper       |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
605  * CM !norm | upper       |     ZCILOOK | MT_NORMALIZE
606  * CM  norm | upper|formX |    !ZCILOOK | MT_NORMALIZE | MT_MATCH_CASE
607  * CM  norm | upper|formX |     ZCILOOK | MT_NORMALIZE
608  *
609  * Abbreviations:
610  *    CS = Case Sensitive, CI = Case Insensitive, CM = Case Mixed
611  *    upper = case folding set by fs type on creation (U8_TEXTPREP_TOUPPER)
612  *    formX = unicode normalization form set on fs creation
613  */
614 static int
615 zfs_dropname(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
616     int flag)
617 {
618         int error;
619
620         if (zp->z_zfsvfs->z_norm) {
621                 matchtype_t mt = MT_NORMALIZE;
622
623                 if (zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) {
624                         mt |= MT_MATCH_CASE;
625                 }
626
627                 error = zap_remove_norm(zp->z_zfsvfs->z_os, dzp->z_id,
628                     name, mt, tx);
629         } else {
630                 error = zap_remove(zp->z_zfsvfs->z_os, dzp->z_id, name, tx);
631         }
632
633         return (error);
634 }
635
636 /*
637  * Unlink zp from dzp, and mark zp for deletion if this was the last link.
638  * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
639  * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
640  * If it's non-NULL, we use it to indicate whether the znode needs deletion,
641  * and it's the caller's job to do it.
642  */
643 int
644 zfs_link_destroy(znode_t *dzp, const char *name, znode_t *zp, dmu_tx_t *tx,
645     int flag, boolean_t *unlinkedp)
646 {
647         zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
648         vnode_t *vp = ZTOV(zp);
649         int zp_is_dir = (vp->v_type == VDIR);
650         boolean_t unlinked = B_FALSE;
651         sa_bulk_attr_t bulk[5];
652         uint64_t mtime[2], ctime[2];
653         int count = 0;
654         int error;
655
656         ASSERT_VOP_ELOCKED(ZTOV(dzp), __func__);
657         ASSERT_VOP_ELOCKED(ZTOV(zp), __func__);
658
659         if (!(flag & ZRENAMING)) {
660
661                 if (zp_is_dir && !zfs_dirempty(zp)) {
662 #ifdef illumos
663                         return (SET_ERROR(EEXIST));
664 #else
665                         return (SET_ERROR(ENOTEMPTY));
666 #endif
667                 }
668
669                 /*
670                  * If we get here, we are going to try to remove the object.
671                  * First try removing the name from the directory; if that
672                  * fails, return the error.
673                  */
674                 error = zfs_dropname(dzp, name, zp, tx, flag);
675                 if (error != 0) {
676                         return (error);
677                 }
678
679                 if (zp->z_links <= zp_is_dir) {
680                         zfs_panic_recover("zfs: link count on vnode %p is %u, "
681                             "should be at least %u", zp->z_vnode,
682                             (int)zp->z_links,
683                             zp_is_dir + 1);
684                         zp->z_links = zp_is_dir + 1;
685                 }
686                 if (--zp->z_links == zp_is_dir) {
687                         zp->z_unlinked = B_TRUE;
688                         zp->z_links = 0;
689                         unlinked = B_TRUE;
690                 } else {
691                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
692                             NULL, &ctime, sizeof (ctime));
693                         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
694                             NULL, &zp->z_pflags, sizeof (zp->z_pflags));
695                         zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
696                             B_TRUE);
697                 }
698                 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
699                     NULL, &zp->z_links, sizeof (zp->z_links));
700                 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
701                 count = 0;
702                 ASSERT0(error);
703         } else {
704                 ASSERT(zp->z_unlinked == 0);
705                 error = zfs_dropname(dzp, name, zp, tx, flag);
706                 if (error != 0)
707                         return (error);
708         }
709
710         dzp->z_size--;          /* one dirent removed */
711         dzp->z_links -= zp_is_dir;      /* ".." link from zp */
712         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_LINKS(zfsvfs),
713             NULL, &dzp->z_links, sizeof (dzp->z_links));
714         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs),
715             NULL, &dzp->z_size, sizeof (dzp->z_size));
716         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs),
717             NULL, ctime, sizeof (ctime));
718         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
719             NULL, mtime, sizeof (mtime));
720         SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs),
721             NULL, &dzp->z_pflags, sizeof (dzp->z_pflags));
722         zfs_tstamp_update_setup(dzp, CONTENT_MODIFIED, mtime, ctime, B_TRUE);
723         error = sa_bulk_update(dzp->z_sa_hdl, bulk, count, tx);
724         ASSERT0(error);
725
726         if (unlinkedp != NULL)
727                 *unlinkedp = unlinked;
728         else if (unlinked)
729                 zfs_unlinked_add(zp, tx);
730
731         return (0);
732 }
733
734 /*
735  * Indicate whether the directory is empty.
736  */
737 boolean_t
738 zfs_dirempty(znode_t *dzp)
739 {
740         return (dzp->z_size == 2);
741 }
742
743 int
744 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
745 {
746         zfsvfs_t *zfsvfs = zp->z_zfsvfs;
747         znode_t *xzp;
748         dmu_tx_t *tx;
749         int error;
750         zfs_acl_ids_t acl_ids;
751         boolean_t fuid_dirtied;
752         uint64_t parent;
753
754         *xvpp = NULL;
755
756         /*
757          * In FreeBSD, access checking for creating an EA is being done
758          * in zfs_setextattr(),
759          */
760 #ifndef __FreeBSD_kernel__
761         if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
762                 return (error);
763 #endif
764
765         if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
766             &acl_ids)) != 0)
767                 return (error);
768         if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
769                 zfs_acl_ids_free(&acl_ids);
770                 return (SET_ERROR(EDQUOT));
771         }
772
773         getnewvnode_reserve(1);
774
775         tx = dmu_tx_create(zfsvfs->z_os);
776         dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
777             ZFS_SA_BASE_ATTR_SIZE);
778         dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
779         dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
780         fuid_dirtied = zfsvfs->z_fuid_dirty;
781         if (fuid_dirtied)
782                 zfs_fuid_txhold(zfsvfs, tx);
783         error = dmu_tx_assign(tx, TXG_WAIT);
784         if (error) {
785                 zfs_acl_ids_free(&acl_ids);
786                 dmu_tx_abort(tx);
787                 return (error);
788         }
789         zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, &acl_ids);
790
791         if (fuid_dirtied)
792                 zfs_fuid_sync(zfsvfs, tx);
793
794 #ifdef DEBUG
795         error = sa_lookup(xzp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
796             &parent, sizeof (parent));
797         ASSERT(error == 0 && parent == zp->z_id);
798 #endif
799
800         VERIFY(0 == sa_update(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xzp->z_id,
801             sizeof (xzp->z_id), tx));
802
803         (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
804             xzp, "", NULL, acl_ids.z_fuidp, vap);
805
806         zfs_acl_ids_free(&acl_ids);
807         dmu_tx_commit(tx);
808
809         getnewvnode_drop_reserve();
810
811         *xvpp = ZTOV(xzp);
812
813         return (0);
814 }
815
816 /*
817  * Return a znode for the extended attribute directory for zp.
818  * ** If the directory does not already exist, it is created **
819  *
820  *      IN:     zp      - znode to obtain attribute directory from
821  *              cr      - credentials of caller
822  *              flags   - flags from the VOP_LOOKUP call
823  *
824  *      OUT:    xzpp    - pointer to extended attribute znode
825  *
826  *      RETURN: 0 on success
827  *              error number on failure
828  */
829 int
830 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
831 {
832         zfsvfs_t        *zfsvfs = zp->z_zfsvfs;
833         znode_t         *xzp;
834         vattr_t         va;
835         int             error;
836 top:
837         error = zfs_dirent_lookup(zp, "", &xzp, ZXATTR);
838         if (error)
839                 return (error);
840
841         if (xzp != NULL) {
842                 *xvpp = ZTOV(xzp);
843                 return (0);
844         }
845
846
847         if (!(flags & CREATE_XATTR_DIR)) {
848 #ifdef illumos
849                 return (SET_ERROR(ENOENT));
850 #else
851                 return (SET_ERROR(ENOATTR));
852 #endif
853         }
854
855         if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
856                 return (SET_ERROR(EROFS));
857         }
858
859         /*
860          * The ability to 'create' files in an attribute
861          * directory comes from the write_xattr permission on the base file.
862          *
863          * The ability to 'search' an attribute directory requires
864          * read_xattr permission on the base file.
865          *
866          * Once in a directory the ability to read/write attributes
867          * is controlled by the permissions on the attribute file.
868          */
869         va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
870         va.va_type = VDIR;
871         va.va_mode = S_IFDIR | S_ISVTX | 0777;
872         zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
873
874         error = zfs_make_xattrdir(zp, &va, xvpp, cr);
875
876         if (error == ERESTART) {
877                 /* NB: we already did dmu_tx_wait() if necessary */
878                 goto top;
879         }
880         if (error == 0)
881                 VOP_UNLOCK(*xvpp, 0);
882
883         return (error);
884 }
885
886 /*
887  * Decide whether it is okay to remove within a sticky directory.
888  *
889  * In sticky directories, write access is not sufficient;
890  * you can remove entries from a directory only if:
891  *
892  *      you own the directory,
893  *      you own the entry,
894  *      the entry is a plain file and you have write access,
895  *      or you are privileged (checked in secpolicy...).
896  *
897  * The function returns 0 if remove access is granted.
898  */
899 int
900 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
901 {
902         uid_t           uid;
903         uid_t           downer;
904         uid_t           fowner;
905         zfsvfs_t        *zfsvfs = zdp->z_zfsvfs;
906
907         if (zdp->z_zfsvfs->z_replay)
908                 return (0);
909
910         if ((zdp->z_mode & S_ISVTX) == 0)
911                 return (0);
912
913         downer = zfs_fuid_map_id(zfsvfs, zdp->z_uid, cr, ZFS_OWNER);
914         fowner = zfs_fuid_map_id(zfsvfs, zp->z_uid, cr, ZFS_OWNER);
915
916         if ((uid = crgetuid(cr)) == downer || uid == fowner ||
917             (ZTOV(zp)->v_type == VREG &&
918             zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
919                 return (0);
920         else
921                 return (secpolicy_vnode_remove(ZTOV(zp), cr));
922 }