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