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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
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/mode.h>
36 #include <sys/kmem.h>
37 #include <sys/uio.h>
38 #include <sys/pathname.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/zfs_dir.h>
47 #include <sys/zfs_acl.h>
48 #include <sys/fs/zfs.h>
49 #include <sys/zap.h>
50 #include <sys/dmu.h>
51 #include <sys/atomic.h>
52 #include <sys/zfs_ctldir.h>
53 #include <sys/zfs_fuid.h>
54 #include <sys/dnlc.h>
55 #include <sys/extdirent.h>
56
57 /*
58 * zfs_match_find() is used by zfs_dirent_lock() to peform zap lookups
59 * of names after deciding which is the appropriate lookup interface.
60 */
61 static int
zfs_match_find(zfsvfs_t * zfsvfs,znode_t * dzp,char * name,boolean_t exact,boolean_t update,int * deflags,pathname_t * rpnp,uint64_t * zoid)62 zfs_match_find(zfsvfs_t *zfsvfs, znode_t *dzp, char *name, boolean_t exact,
63 boolean_t update, int *deflags, pathname_t *rpnp, uint64_t *zoid)
64 {
65 int error;
66
67 if (zfsvfs->z_norm) {
68 matchtype_t mt = MT_FIRST;
69 boolean_t conflict = B_FALSE;
70 size_t bufsz = 0;
71 char *buf = NULL;
72
73 if (rpnp) {
74 buf = rpnp->pn_buf;
75 bufsz = rpnp->pn_bufsize;
76 }
77 if (exact)
78 mt = MT_EXACT;
79 /*
80 * In the non-mixed case we only expect there would ever
81 * be one match, but we need to use the normalizing lookup.
82 */
83 error = zap_lookup_norm(zfsvfs->z_os, dzp->z_id, name, 8, 1,
84 zoid, mt, buf, bufsz, &conflict);
85 if (!error && deflags)
86 *deflags = conflict ? ED_CASE_CONFLICT : 0;
87 } else {
88 error = zap_lookup(zfsvfs->z_os, dzp->z_id, name, 8, 1, zoid);
89 }
90 *zoid = ZFS_DIRENT_OBJ(*zoid);
91
92 if (error == ENOENT && update)
93 dnlc_update(ZTOV(dzp), name, DNLC_NO_VNODE);
94
95 return (error);
96 }
97
98 /*
99 * Reference counting for dirlocks. Solaris destroys the condvar as
100 * soon as it broadcasts, which works for them because cv_wait doesn't
101 * need to use the condvar after it is woken, but which is too fast and
102 * loose with the abstraction for us in NetBSD.
103 */
104
105 static int
zfs_dirlock_hold(zfs_dirlock_t * dl)106 zfs_dirlock_hold(zfs_dirlock_t *dl)
107 {
108
109 KASSERT(mutex_owned(&dl->dl_dzp->z_lock));
110
111 if (dl->dl_refcnt >= ULONG_MAX) /* XXX Name this constant. */
112 return (ENFILE); /* XXX What to do? */
113
114 dl->dl_refcnt++;
115 return (0);
116 }
117
118 static void
zfs_dirlock_rele(zfs_dirlock_t * dl)119 zfs_dirlock_rele(zfs_dirlock_t *dl)
120 {
121
122 KASSERT(mutex_owned(&dl->dl_dzp->z_lock));
123 KASSERT(dl->dl_refcnt > 0);
124
125 if (--dl->dl_refcnt == 0) {
126 if (dl->dl_namesize != 0)
127 kmem_free(dl->dl_name, dl->dl_namesize);
128 cv_destroy(&dl->dl_cv);
129 kmem_free(dl, sizeof(*dl));
130 }
131 }
132
133 /*
134 * Lock a directory entry. A dirlock on <dzp, name> protects that name
135 * in dzp's directory zap object. As long as you hold a dirlock, you can
136 * assume two things: (1) dzp cannot be reaped, and (2) no other thread
137 * can change the zap entry for (i.e. link or unlink) this name.
138 *
139 * Input arguments:
140 * dzp - znode for directory
141 * name - name of entry to lock
142 * flag - ZNEW: if the entry already exists, fail with EEXIST.
143 * ZEXISTS: if the entry does not exist, fail with ENOENT.
144 * ZSHARED: allow concurrent access with other ZSHARED callers.
145 * ZXATTR: we want dzp's xattr directory
146 * ZCILOOK: On a mixed sensitivity file system,
147 * this lookup should be case-insensitive.
148 * ZCIEXACT: On a purely case-insensitive file system,
149 * this lookup should be case-sensitive.
150 * ZRENAMING: we are locking for renaming, force narrow locks
151 * ZHAVELOCK: Don't grab the z_name_lock for this call. The
152 * current thread already holds it.
153 *
154 * Output arguments:
155 * zpp - pointer to the znode for the entry (NULL if there isn't one)
156 * dlpp - pointer to the dirlock for this entry (NULL on error)
157 * direntflags - (case-insensitive lookup only)
158 * flags if multiple case-sensitive matches exist in directory
159 * realpnp - (case-insensitive lookup only)
160 * actual name matched within the directory
161 *
162 * Return value: 0 on success or errno on failure.
163 *
164 * NOTE: Always checks for, and rejects, '.' and '..'.
165 * NOTE: For case-insensitive file systems we take wide locks (see below),
166 * but return znode pointers to a single match.
167 */
168 int
zfs_dirent_lock(zfs_dirlock_t ** dlpp,znode_t * dzp,char * name,znode_t ** zpp,int flag,int * direntflags,pathname_t * realpnp)169 zfs_dirent_lock(zfs_dirlock_t **dlpp, znode_t *dzp, char *name, znode_t **zpp,
170 int flag, int *direntflags, pathname_t *realpnp)
171 {
172 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
173 zfs_dirlock_t *dl;
174 boolean_t update;
175 boolean_t exact;
176 uint64_t zoid;
177 vnode_t *vp = NULL;
178 int error = 0;
179 int cmpflags;
180
181 *zpp = NULL;
182 *dlpp = NULL;
183
184 /*
185 * Verify that we are not trying to lock '.', '..', or '.zfs'
186 */
187 if (name[0] == '.' &&
188 (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) ||
189 zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0)
190 return (EEXIST);
191
192 /*
193 * Case sensitivity and normalization preferences are set when
194 * the file system is created. These are stored in the
195 * zfsvfs->z_case and zfsvfs->z_norm fields. These choices
196 * affect what vnodes can be cached in the DNLC, how we
197 * perform zap lookups, and the "width" of our dirlocks.
198 *
199 * A normal dirlock locks a single name. Note that with
200 * normalization a name can be composed multiple ways, but
201 * when normalized, these names all compare equal. A wide
202 * dirlock locks multiple names. We need these when the file
203 * system is supporting mixed-mode access. It is sometimes
204 * necessary to lock all case permutations of file name at
205 * once so that simultaneous case-insensitive/case-sensitive
206 * behaves as rationally as possible.
207 */
208
209 /*
210 * Decide if exact matches should be requested when performing
211 * a zap lookup on file systems supporting case-insensitive
212 * access.
213 */
214 exact =
215 ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE) && (flag & ZCIEXACT)) ||
216 ((zfsvfs->z_case == ZFS_CASE_MIXED) && !(flag & ZCILOOK));
217
218 /*
219 * Only look in or update the DNLC if we are looking for the
220 * name on a file system that does not require normalization
221 * or case folding. We can also look there if we happen to be
222 * on a non-normalizing, mixed sensitivity file system IF we
223 * are looking for the exact name.
224 *
225 * Maybe can add TO-UPPERed version of name to dnlc in ci-only
226 * case for performance improvement?
227 */
228 update = !zfsvfs->z_norm ||
229 ((zfsvfs->z_case == ZFS_CASE_MIXED) &&
230 !(zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER) && !(flag & ZCILOOK));
231
232 /*
233 * ZRENAMING indicates we are in a situation where we should
234 * take narrow locks regardless of the file system's
235 * preferences for normalizing and case folding. This will
236 * prevent us deadlocking trying to grab the same wide lock
237 * twice if the two names happen to be case-insensitive
238 * matches.
239 */
240 if (flag & ZRENAMING)
241 cmpflags = 0;
242 else
243 cmpflags = zfsvfs->z_norm;
244
245 /*
246 * Wait until there are no locks on this name.
247 *
248 * Don't grab the the lock if it is already held. However, cannot
249 * have both ZSHARED and ZHAVELOCK together.
250 */
251 ASSERT(!(flag & ZSHARED) || !(flag & ZHAVELOCK));
252 if (!(flag & ZHAVELOCK))
253 rw_enter(&dzp->z_name_lock, RW_READER);
254
255 mutex_enter(&dzp->z_lock);
256 for (;;) {
257 if (dzp->z_unlinked) {
258 mutex_exit(&dzp->z_lock);
259 if (!(flag & ZHAVELOCK))
260 rw_exit(&dzp->z_name_lock);
261 return (ENOENT);
262 }
263 for (dl = dzp->z_dirlocks; dl != NULL; dl = dl->dl_next) {
264 if ((u8_strcmp(name, dl->dl_name, 0, cmpflags,
265 U8_UNICODE_LATEST, &error) == 0) || error != 0)
266 break;
267 }
268 if (error != 0) {
269 mutex_exit(&dzp->z_lock);
270 if (!(flag & ZHAVELOCK))
271 rw_exit(&dzp->z_name_lock);
272 return (ENOENT);
273 }
274 if (dl == NULL) {
275 /*
276 * Allocate a new dirlock and add it to the list.
277 */
278 dl = kmem_alloc(sizeof (zfs_dirlock_t), KM_SLEEP);
279 cv_init(&dl->dl_cv, NULL, CV_DEFAULT, NULL);
280 dl->dl_name = name;
281 dl->dl_sharecnt = 0;
282 dl->dl_namelock = 0;
283 dl->dl_namesize = 0;
284 dl->dl_refcnt = 1;
285 dl->dl_dzp = dzp;
286 dl->dl_next = dzp->z_dirlocks;
287 dzp->z_dirlocks = dl;
288 break;
289 }
290 if ((flag & ZSHARED) && dl->dl_sharecnt != 0)
291 break;
292 error = zfs_dirlock_hold(dl);
293 if (error) {
294 mutex_exit(&dzp->z_lock);
295 if (!(flag & ZHAVELOCK))
296 rw_exit(&dzp->z_name_lock);
297 return (error);
298 }
299 cv_wait(&dl->dl_cv, &dzp->z_lock);
300 zfs_dirlock_rele(dl);
301 }
302
303 /*
304 * If the z_name_lock was NOT held for this dirlock record it.
305 */
306 if (flag & ZHAVELOCK)
307 dl->dl_namelock = 1;
308
309 if ((flag & ZSHARED) && ++dl->dl_sharecnt > 1 && dl->dl_namesize == 0) {
310 /*
311 * We're the second shared reference to dl. Make a copy of
312 * dl_name in case the first thread goes away before we do.
313 * Note that we initialize the new name before storing its
314 * pointer into dl_name, because the first thread may load
315 * dl->dl_name at any time. He'll either see the old value,
316 * which is his, or the new shared copy; either is OK.
317 */
318 dl->dl_namesize = strlen(dl->dl_name) + 1;
319 name = kmem_alloc(dl->dl_namesize, KM_SLEEP);
320 bcopy(dl->dl_name, name, dl->dl_namesize);
321 dl->dl_name = name;
322 }
323
324 mutex_exit(&dzp->z_lock);
325
326 /*
327 * We have a dirlock on the name. (Note that it is the dirlock,
328 * not the dzp's z_lock, that protects the name in the zap object.)
329 * See if there's an object by this name; if so, put a hold on it.
330 */
331 if (flag & ZXATTR) {
332 zoid = dzp->z_phys->zp_xattr;
333 error = (zoid == 0 ? ENOENT : 0);
334 } else {
335 if (update)
336 vp = dnlc_lookup(ZTOV(dzp), name);
337 if (vp == DNLC_NO_VNODE) {
338 VN_RELE(vp);
339 error = ENOENT;
340 } else if (vp) {
341 if (flag & ZNEW) {
342 zfs_dirent_unlock(dl);
343 VN_RELE(vp);
344 return (EEXIST);
345 }
346 *dlpp = dl;
347 *zpp = VTOZ(vp);
348 return (0);
349 } else {
350 error = zfs_match_find(zfsvfs, dzp, name, exact,
351 update, direntflags, realpnp, &zoid);
352 }
353 }
354 if (error) {
355 if (error != ENOENT || (flag & ZEXISTS)) {
356 zfs_dirent_unlock(dl);
357 return (error);
358 }
359 } else {
360 if (flag & ZNEW) {
361 zfs_dirent_unlock(dl);
362 return (EEXIST);
363 }
364 error = zfs_zget(zfsvfs, zoid, zpp);
365 if (error) {
366 zfs_dirent_unlock(dl);
367 return (error);
368 }
369 if (!(flag & ZXATTR) && update)
370 dnlc_update(ZTOV(dzp), name, ZTOV(*zpp));
371 }
372
373 *dlpp = dl;
374
375 return (0);
376 }
377
378 /*
379 * Unlock this directory entry and wake anyone who was waiting for it.
380 */
381 void
zfs_dirent_unlock(zfs_dirlock_t * dl)382 zfs_dirent_unlock(zfs_dirlock_t *dl)
383 {
384 znode_t *dzp = dl->dl_dzp;
385 zfs_dirlock_t **prev_dl, *cur_dl;
386
387 mutex_enter(&dzp->z_lock);
388
389 if (!dl->dl_namelock)
390 rw_exit(&dzp->z_name_lock);
391
392 if (dl->dl_sharecnt > 1) {
393 dl->dl_sharecnt--;
394 mutex_exit(&dzp->z_lock);
395 return;
396 }
397 prev_dl = &dzp->z_dirlocks;
398 while ((cur_dl = *prev_dl) != dl)
399 prev_dl = &cur_dl->dl_next;
400 *prev_dl = dl->dl_next;
401 cv_broadcast(&dl->dl_cv);
402 zfs_dirlock_rele(dl);
403 mutex_exit(&dzp->z_lock);
404 }
405
406 /*
407 * Look up an entry in a directory.
408 *
409 * NOTE: '.' and '..' are handled as special cases because
410 * no directory entries are actually stored for them. If this is
411 * the root of a filesystem, then '.zfs' is also treated as a
412 * special pseudo-directory.
413 */
414 int
zfs_dirlook(znode_t * dzp,char * name,vnode_t ** vpp,int flags,int * deflg,pathname_t * rpnp)415 zfs_dirlook(znode_t *dzp, char *name, vnode_t **vpp, int flags,
416 int *deflg, pathname_t *rpnp)
417 {
418 zfs_dirlock_t *dl;
419 znode_t *zp;
420 int error = 0;
421
422 if (name[0] == 0 || (name[0] == '.' && name[1] == 0)) {
423 *vpp = ZTOV(dzp);
424 VN_HOLD(*vpp);
425 } else if (name[0] == '.' && name[1] == '.' && name[2] == 0) {
426 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
427 /*
428 * If we are a snapshot mounted under .zfs, return
429 * the vp for the snapshot directory.
430 */
431 if (dzp->z_phys->zp_parent == dzp->z_id &&
432 zfsvfs->z_parent != zfsvfs) {
433 error = zfsctl_root_lookup(zfsvfs->z_parent->z_ctldir,
434 "snapshot", vpp, NULL, 0, NULL, kcred,
435 NULL, NULL, NULL);
436 return (error);
437 }
438 rw_enter(&dzp->z_parent_lock, RW_READER);
439 mutex_enter(&dzp->z_lock);
440 if (dzp->z_phys->zp_links == 0) {
441 /* Directory has been rmdir'd. */
442 error = ENOENT;
443 } else {
444 error = zfs_zget(zfsvfs, dzp->z_phys->zp_parent, &zp);
445 if (error == 0)
446 *vpp = ZTOV(zp);
447 }
448 mutex_exit(&dzp->z_lock);
449 rw_exit(&dzp->z_parent_lock);
450 } else if (zfs_has_ctldir(dzp) && strcmp(name, ZFS_CTLDIR_NAME) == 0) {
451 *vpp = zfsctl_root(dzp);
452 } else {
453 int zf;
454
455 zf = ZEXISTS | ZSHARED;
456 if (flags & FIGNORECASE)
457 zf |= ZCILOOK;
458
459 error = zfs_dirent_lock(&dl, dzp, name, &zp, zf, deflg, rpnp);
460 if (error == 0) {
461 *vpp = ZTOV(zp);
462 zfs_dirent_unlock(dl);
463 dzp->z_zn_prefetch = B_TRUE; /* enable prefetching */
464 }
465 rpnp = NULL;
466 }
467
468 if ((flags & FIGNORECASE) && rpnp && !error)
469 (void) strlcpy(rpnp->pn_buf, name, rpnp->pn_bufsize);
470
471 return (error);
472 }
473
474 /*
475 * unlinked Set (formerly known as the "delete queue") Error Handling
476 *
477 * When dealing with the unlinked set, we dmu_tx_hold_zap(), but we
478 * don't specify the name of the entry that we will be manipulating. We
479 * also fib and say that we won't be adding any new entries to the
480 * unlinked set, even though we might (this is to lower the minimum file
481 * size that can be deleted in a full filesystem). So on the small
482 * chance that the nlink list is using a fat zap (ie. has more than
483 * 2000 entries), we *may* not pre-read a block that's needed.
484 * Therefore it is remotely possible for some of the assertions
485 * regarding the unlinked set below to fail due to i/o error. On a
486 * nondebug system, this will result in the space being leaked.
487 */
488 void
zfs_unlinked_add(znode_t * zp,dmu_tx_t * tx)489 zfs_unlinked_add(znode_t *zp, dmu_tx_t *tx)
490 {
491 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
492
493 ASSERT(zp->z_unlinked);
494 ASSERT3U(zp->z_phys->zp_links, ==, 0);
495
496 VERIFY3U(0, ==,
497 zap_add_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
498 }
499
500 /*
501 * Clean up any znodes that had no links when we either crashed or
502 * (force) umounted the file system.
503 */
504 void
zfs_unlinked_drain(zfsvfs_t * zfsvfs)505 zfs_unlinked_drain(zfsvfs_t *zfsvfs)
506 {
507 zap_cursor_t zc;
508 zap_attribute_t zap;
509 dmu_object_info_t doi;
510 znode_t *zp;
511 int error;
512
513 /*
514 * Interate over the contents of the unlinked set.
515 */
516 for (zap_cursor_init(&zc, zfsvfs->z_os, zfsvfs->z_unlinkedobj);
517 zap_cursor_retrieve(&zc, &zap) == 0;
518 zap_cursor_advance(&zc)) {
519
520 /*
521 * See what kind of object we have in list
522 */
523
524 error = dmu_object_info(zfsvfs->z_os,
525 zap.za_first_integer, &doi);
526 if (error != 0)
527 continue;
528
529 ASSERT((doi.doi_type == DMU_OT_PLAIN_FILE_CONTENTS) ||
530 (doi.doi_type == DMU_OT_DIRECTORY_CONTENTS));
531 /*
532 * We need to re-mark these list entries for deletion,
533 * so we pull them back into core and set zp->z_unlinked.
534 */
535 error = zfs_zget(zfsvfs, zap.za_first_integer, &zp);
536
537 /*
538 * We may pick up znodes that are already marked for deletion.
539 * This could happen during the purge of an extended attribute
540 * directory. All we need to do is skip over them, since they
541 * are already in the system marked z_unlinked.
542 */
543 if (error != 0)
544 continue;
545
546 zp->z_unlinked = B_TRUE;
547 VN_RELE(ZTOV(zp));
548 }
549 zap_cursor_fini(&zc);
550 }
551
552 /*
553 * Delete the entire contents of a directory. Return a count
554 * of the number of entries that could not be deleted. If we encounter
555 * an error, return a count of at least one so that the directory stays
556 * in the unlinked set.
557 *
558 * NOTE: this function assumes that the directory is inactive,
559 * so there is no need to lock its entries before deletion.
560 * Also, it assumes the directory contents is *only* regular
561 * files.
562 */
563 static int
zfs_purgedir(znode_t * dzp)564 zfs_purgedir(znode_t *dzp)
565 {
566 zap_cursor_t zc;
567 zap_attribute_t zap;
568 znode_t *xzp;
569 dmu_tx_t *tx;
570 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
571 zfs_dirlock_t dl;
572 int skipped = 0;
573 int error;
574
575 for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id);
576 (error = zap_cursor_retrieve(&zc, &zap)) == 0;
577 zap_cursor_advance(&zc)) {
578 error = zfs_zget(zfsvfs,
579 ZFS_DIRENT_OBJ(zap.za_first_integer), &xzp);
580 if (error) {
581 skipped += 1;
582 continue;
583 }
584
585 ASSERT((ZTOV(xzp)->v_type == VREG) ||
586 (ZTOV(xzp)->v_type == VLNK));
587
588 tx = dmu_tx_create(zfsvfs->z_os);
589 dmu_tx_hold_bonus(tx, dzp->z_id);
590 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, zap.za_name);
591 dmu_tx_hold_bonus(tx, xzp->z_id);
592 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
593 error = dmu_tx_assign(tx, TXG_WAIT);
594 if (error) {
595 dmu_tx_abort(tx);
596 VN_RELE(ZTOV(xzp));
597 skipped += 1;
598 continue;
599 }
600 bzero(&dl, sizeof (dl));
601 dl.dl_dzp = dzp;
602 dl.dl_name = zap.za_name;
603
604 error = zfs_link_destroy(&dl, xzp, tx, 0, NULL);
605 if (error)
606 skipped += 1;
607 dmu_tx_commit(tx);
608
609 VN_RELE(ZTOV(xzp));
610 }
611 zap_cursor_fini(&zc);
612 if (error != ENOENT)
613 skipped += 1;
614 return (skipped);
615 }
616
617 void
zfs_rmnode(znode_t * zp)618 zfs_rmnode(znode_t *zp)
619 {
620 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
621 objset_t *os = zfsvfs->z_os;
622 znode_t *xzp = NULL;
623 dmu_tx_t *tx;
624 uint64_t acl_obj;
625 int error;
626
627 ASSERT(ZTOV(zp)->v_count == 0);
628 ASSERT(zp->z_phys->zp_links == 0);
629
630 /*
631 * If this is an attribute directory, purge its contents.
632 */
633 if (ZTOV(zp)->v_type == VDIR && (zp->z_phys->zp_flags & ZFS_XATTR)) {
634 if (zfs_purgedir(zp) != 0) {
635 /*
636 * Not enough space to delete some xattrs.
637 * Leave it in the unlinked set.
638 */
639 zfs_znode_dmu_fini(zp);
640 zfs_znode_free(zp);
641 return;
642 }
643 }
644
645 /*
646 * Free up all the data in the file.
647 */
648 error = dmu_free_long_range(os, zp->z_id, 0, DMU_OBJECT_END);
649 if (error) {
650 /*
651 * Not enough space. Leave the file in the unlinked set.
652 */
653 zfs_znode_dmu_fini(zp);
654 zfs_znode_free(zp);
655 return;
656 }
657
658 /*
659 * If the file has extended attributes, we're going to unlink
660 * the xattr dir.
661 */
662 if (zp->z_phys->zp_xattr) {
663 error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp);
664 ASSERT(error == 0);
665 }
666
667 acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj;
668
669 /*
670 * Set up the final transaction.
671 */
672 tx = dmu_tx_create(os);
673 dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END);
674 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
675 if (xzp) {
676 dmu_tx_hold_bonus(tx, xzp->z_id);
677 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, TRUE, NULL);
678 }
679 if (acl_obj)
680 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
681 error = dmu_tx_assign(tx, TXG_WAIT);
682 if (error) {
683 /*
684 * Not enough space to delete the file. Leave it in the
685 * unlinked set, leaking it until the fs is remounted (at
686 * which point we'll call zfs_unlinked_drain() to process it).
687 */
688 dmu_tx_abort(tx);
689 zfs_znode_dmu_fini(zp);
690 zfs_znode_free(zp);
691 goto out;
692 }
693
694 if (xzp) {
695 dmu_buf_will_dirty(xzp->z_dbuf, tx);
696 mutex_enter(&xzp->z_lock);
697 xzp->z_unlinked = B_TRUE; /* mark xzp for deletion */
698 xzp->z_phys->zp_links = 0; /* no more links to it */
699 mutex_exit(&xzp->z_lock);
700 zfs_unlinked_add(xzp, tx);
701 }
702
703 /* Remove this znode from the unlinked set */
704 VERIFY3U(0, ==,
705 zap_remove_int(zfsvfs->z_os, zfsvfs->z_unlinkedobj, zp->z_id, tx));
706
707 zfs_znode_delete(zp, tx);
708
709 dmu_tx_commit(tx);
710 out:
711 if (xzp)
712 VN_RELE(ZTOV(xzp));
713 }
714
715 static uint64_t
zfs_dirent(znode_t * zp)716 zfs_dirent(znode_t *zp)
717 {
718 uint64_t de = zp->z_id;
719 if (zp->z_zfsvfs->z_version >= ZPL_VERSION_DIRENT_TYPE)
720 de |= IFTODT((zp)->z_phys->zp_mode) << 60;
721 return (de);
722 }
723
724 /*
725 * Link zp into dl. Can only fail if zp has been unlinked.
726 */
727 int
zfs_link_create(zfs_dirlock_t * dl,znode_t * zp,dmu_tx_t * tx,int flag)728 zfs_link_create(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag)
729 {
730 znode_t *dzp = dl->dl_dzp;
731 vnode_t *vp = ZTOV(zp);
732 uint64_t value;
733 int zp_is_dir = (vp->v_type == VDIR);
734 int error;
735
736 dmu_buf_will_dirty(zp->z_dbuf, tx);
737 mutex_enter(&zp->z_lock);
738
739 if (!(flag & ZRENAMING)) {
740 if (zp->z_unlinked) { /* no new links to unlinked zp */
741 ASSERT(!(flag & (ZNEW | ZEXISTS)));
742 mutex_exit(&zp->z_lock);
743 return (ENOENT);
744 }
745 zp->z_phys->zp_links++;
746 }
747 zp->z_phys->zp_parent = dzp->z_id; /* dzp is now zp's parent */
748
749 if (!(flag & ZNEW))
750 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
751 mutex_exit(&zp->z_lock);
752
753 dmu_buf_will_dirty(dzp->z_dbuf, tx);
754 mutex_enter(&dzp->z_lock);
755 dzp->z_phys->zp_size++; /* one dirent added */
756 dzp->z_phys->zp_links += zp_is_dir; /* ".." link from zp */
757 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
758 mutex_exit(&dzp->z_lock);
759
760 value = zfs_dirent(zp);
761 error = zap_add(zp->z_zfsvfs->z_os, dzp->z_id, dl->dl_name,
762 8, 1, &value, tx);
763 ASSERT(error == 0);
764
765 dnlc_update(ZTOV(dzp), dl->dl_name, vp);
766
767 return (0);
768 }
769
770 /*
771 * Unlink zp from dl, and mark zp for deletion if this was the last link.
772 * Can fail if zp is a mount point (EBUSY) or a non-empty directory (EEXIST).
773 * If 'unlinkedp' is NULL, we put unlinked znodes on the unlinked list.
774 * If it's non-NULL, we use it to indicate whether the znode needs deletion,
775 * and it's the caller's job to do it.
776 */
777 int
zfs_link_destroy(zfs_dirlock_t * dl,znode_t * zp,dmu_tx_t * tx,int flag,boolean_t * unlinkedp)778 zfs_link_destroy(zfs_dirlock_t *dl, znode_t *zp, dmu_tx_t *tx, int flag,
779 boolean_t *unlinkedp)
780 {
781 znode_t *dzp = dl->dl_dzp;
782 vnode_t *vp = ZTOV(zp);
783 int zp_is_dir = (vp->v_type == VDIR);
784 boolean_t unlinked = B_FALSE;
785 int error;
786
787 dnlc_remove(ZTOV(dzp), dl->dl_name);
788
789 if (!(flag & ZRENAMING)) {
790 dmu_buf_will_dirty(zp->z_dbuf, tx);
791
792 if (vn_vfswlock(vp)) /* prevent new mounts on zp */
793 return (EBUSY);
794
795 if (vn_ismntpt(vp)) { /* don't remove mount point */
796 vn_vfsunlock(vp);
797 return (EBUSY);
798 }
799
800 mutex_enter(&zp->z_lock);
801 if (zp_is_dir && !zfs_dirempty(zp)) { /* dir not empty */
802 mutex_exit(&zp->z_lock);
803 vn_vfsunlock(vp);
804 return (ENOTEMPTY);
805 }
806 if (zp->z_phys->zp_links <= zp_is_dir) {
807 zfs_panic_recover("zfs: link count on vnode %p is %u, "
808 "should be at least %u",
809 zp->z_vnode, (int)zp->z_phys->zp_links,
810 zp_is_dir + 1);
811 zp->z_phys->zp_links = zp_is_dir + 1;
812 }
813 if (--zp->z_phys->zp_links == zp_is_dir) {
814 zp->z_unlinked = B_TRUE;
815 zp->z_phys->zp_links = 0;
816 unlinked = B_TRUE;
817 } else {
818 zfs_time_stamper_locked(zp, STATE_CHANGED, tx);
819 }
820 mutex_exit(&zp->z_lock);
821 vn_vfsunlock(vp);
822 }
823
824 dmu_buf_will_dirty(dzp->z_dbuf, tx);
825 mutex_enter(&dzp->z_lock);
826 dzp->z_phys->zp_size--; /* one dirent removed */
827 dzp->z_phys->zp_links -= zp_is_dir; /* ".." link from zp */
828 zfs_time_stamper_locked(dzp, CONTENT_MODIFIED, tx);
829 mutex_exit(&dzp->z_lock);
830
831 if (zp->z_zfsvfs->z_norm) {
832 if (((zp->z_zfsvfs->z_case == ZFS_CASE_INSENSITIVE) &&
833 (flag & ZCIEXACT)) ||
834 ((zp->z_zfsvfs->z_case == ZFS_CASE_MIXED) &&
835 !(flag & ZCILOOK)))
836 error = zap_remove_norm(zp->z_zfsvfs->z_os,
837 dzp->z_id, dl->dl_name, MT_EXACT, tx);
838 else
839 error = zap_remove_norm(zp->z_zfsvfs->z_os,
840 dzp->z_id, dl->dl_name, MT_FIRST, tx);
841 } else {
842 error = zap_remove(zp->z_zfsvfs->z_os,
843 dzp->z_id, dl->dl_name, tx);
844 }
845 ASSERT(error == 0);
846
847 if (unlinkedp != NULL)
848 *unlinkedp = unlinked;
849 else if (unlinked)
850 zfs_unlinked_add(zp, tx);
851
852 return (0);
853 }
854
855 /*
856 * Indicate whether the directory is empty. Works with or without z_lock
857 * held, but can only be consider a hint in the latter case. Returns true
858 * if only "." and ".." remain and there's no work in progress.
859 */
860 boolean_t
zfs_dirempty(znode_t * dzp)861 zfs_dirempty(znode_t *dzp)
862 {
863 return (dzp->z_phys->zp_size == 2 && dzp->z_dirlocks == 0);
864 }
865
866 int
zfs_make_xattrdir(znode_t * zp,vattr_t * vap,vnode_t ** xvpp,cred_t * cr)867 zfs_make_xattrdir(znode_t *zp, vattr_t *vap, vnode_t **xvpp, cred_t *cr)
868 {
869 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
870 znode_t *xzp;
871 dmu_tx_t *tx;
872 int error;
873 zfs_acl_ids_t acl_ids;
874 boolean_t fuid_dirtied;
875
876 *xvpp = NULL;
877
878 if (error = zfs_zaccess(zp, ACE_WRITE_NAMED_ATTRS, 0, B_FALSE, cr))
879 return (error);
880
881 if ((error = zfs_acl_ids_create(zp, IS_XATTR, vap, cr, NULL,
882 &acl_ids)) != 0)
883 return (error);
884 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
885 zfs_acl_ids_free(&acl_ids);
886 return (EDQUOT);
887 }
888
889 tx = dmu_tx_create(zfsvfs->z_os);
890 dmu_tx_hold_bonus(tx, zp->z_id);
891 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
892 fuid_dirtied = zfsvfs->z_fuid_dirty;
893 if (fuid_dirtied)
894 zfs_fuid_txhold(zfsvfs, tx);
895 error = dmu_tx_assign(tx, TXG_NOWAIT);
896 if (error) {
897 zfs_acl_ids_free(&acl_ids);
898 if (error == ERESTART)
899 dmu_tx_wait(tx);
900 dmu_tx_abort(tx);
901 return (error);
902 }
903 zfs_mknode(zp, vap, tx, cr, IS_XATTR, &xzp, 0, &acl_ids);
904
905 if (fuid_dirtied)
906 zfs_fuid_sync(zfsvfs, tx);
907
908 ASSERT(xzp->z_phys->zp_parent == zp->z_id);
909 dmu_buf_will_dirty(zp->z_dbuf, tx);
910 zp->z_phys->zp_xattr = xzp->z_id;
911
912 (void) zfs_log_create(zfsvfs->z_log, tx, TX_MKXATTR, zp,
913 xzp, "", NULL, acl_ids.z_fuidp, vap);
914
915 zfs_acl_ids_free(&acl_ids);
916 dmu_tx_commit(tx);
917
918 *xvpp = ZTOV(xzp);
919
920 return (0);
921 }
922
923 /*
924 * Return a znode for the extended attribute directory for zp.
925 * ** If the directory does not already exist, it is created **
926 *
927 * IN: zp - znode to obtain attribute directory from
928 * cr - credentials of caller
929 * flags - flags from the VOP_LOOKUP call
930 *
931 * OUT: xzpp - pointer to extended attribute znode
932 *
933 * RETURN: 0 on success
934 * error number on failure
935 */
936 int
zfs_get_xattrdir(znode_t * zp,vnode_t ** xvpp,cred_t * cr,int flags)937 zfs_get_xattrdir(znode_t *zp, vnode_t **xvpp, cred_t *cr, int flags)
938 {
939 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
940 znode_t *xzp;
941 zfs_dirlock_t *dl;
942 vattr_t va;
943 int error;
944 top:
945 error = zfs_dirent_lock(&dl, zp, "", &xzp, ZXATTR, NULL, NULL);
946 if (error)
947 return (error);
948
949 if (xzp != NULL) {
950 *xvpp = ZTOV(xzp);
951 zfs_dirent_unlock(dl);
952 return (0);
953 }
954
955 ASSERT(zp->z_phys->zp_xattr == 0);
956
957 if (!(flags & CREATE_XATTR_DIR)) {
958 zfs_dirent_unlock(dl);
959 return (ENOENT);
960 }
961
962 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
963 zfs_dirent_unlock(dl);
964 return (EROFS);
965 }
966
967 /*
968 * The ability to 'create' files in an attribute
969 * directory comes from the write_xattr permission on the base file.
970 *
971 * The ability to 'search' an attribute directory requires
972 * read_xattr permission on the base file.
973 *
974 * Once in a directory the ability to read/write attributes
975 * is controlled by the permissions on the attribute file.
976 */
977 va.va_mask = AT_TYPE | AT_MODE | AT_UID | AT_GID;
978 va.va_type = VDIR;
979 va.va_mode = S_IFDIR | S_ISVTX | 0777;
980 zfs_fuid_map_ids(zp, cr, &va.va_uid, &va.va_gid);
981
982 error = zfs_make_xattrdir(zp, &va, xvpp, cr);
983 zfs_dirent_unlock(dl);
984
985 if (error == ERESTART) {
986 /* NB: we already did dmu_tx_wait() if necessary */
987 goto top;
988 }
989
990 return (error);
991 }
992
993 /*
994 * Decide whether it is okay to remove within a sticky directory.
995 *
996 * In sticky directories, write access is not sufficient;
997 * you can remove entries from a directory only if:
998 *
999 * you own the directory,
1000 * you own the entry,
1001 * the entry is a plain file and you have write access,
1002 * or you are privileged (checked in secpolicy...).
1003 *
1004 * The function returns 0 if remove access is granted.
1005 */
1006 int
zfs_sticky_remove_access(znode_t * zdp,znode_t * zp,cred_t * cr)1007 zfs_sticky_remove_access(znode_t *zdp, znode_t *zp, cred_t *cr)
1008 {
1009 uid_t uid;
1010 uid_t downer;
1011 uid_t fowner;
1012 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1013
1014 if (zdp->z_zfsvfs->z_replay)
1015 return (0);
1016
1017 if ((zdp->z_phys->zp_mode & S_ISVTX) == 0)
1018 return (0);
1019
1020 downer = zfs_fuid_map_id(zfsvfs, zdp->z_phys->zp_uid, cr, ZFS_OWNER);
1021 fowner = zfs_fuid_map_id(zfsvfs, zp->z_phys->zp_uid, cr, ZFS_OWNER);
1022
1023 if ((uid = crgetuid(cr)) == downer || uid == fowner ||
1024 (ZTOV(zp)->v_type == VREG &&
1025 zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr) == 0))
1026 return (0);
1027 else
1028 return (secpolicy_vnode_remove(cr));
1029 }
1030