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