Print this page
4347 ZPL can use dmu_tx_assign(TXG_WAIT)
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Adam Leventhal <ahl@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/fs/zfs/zfs_vnops.c
+++ new/usr/src/uts/common/fs/zfs/zfs_vnops.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright 2013 Nexenta Systems, Inc. All rights reserved.
25 25 */
26 26
27 27 /* Portions Copyright 2007 Jeremy Teo */
28 28 /* Portions Copyright 2010 Robert Milkowski */
29 29
30 30 #include <sys/types.h>
31 31 #include <sys/param.h>
32 32 #include <sys/time.h>
33 33 #include <sys/systm.h>
34 34 #include <sys/sysmacros.h>
35 35 #include <sys/resource.h>
36 36 #include <sys/vfs.h>
37 37 #include <sys/vfs_opreg.h>
38 38 #include <sys/vnode.h>
39 39 #include <sys/file.h>
40 40 #include <sys/stat.h>
41 41 #include <sys/kmem.h>
42 42 #include <sys/taskq.h>
43 43 #include <sys/uio.h>
44 44 #include <sys/vmsystm.h>
45 45 #include <sys/atomic.h>
46 46 #include <sys/vm.h>
47 47 #include <vm/seg_vn.h>
48 48 #include <vm/pvn.h>
49 49 #include <vm/as.h>
50 50 #include <vm/kpm.h>
51 51 #include <vm/seg_kpm.h>
52 52 #include <sys/mman.h>
53 53 #include <sys/pathname.h>
54 54 #include <sys/cmn_err.h>
55 55 #include <sys/errno.h>
56 56 #include <sys/unistd.h>
57 57 #include <sys/zfs_dir.h>
58 58 #include <sys/zfs_acl.h>
59 59 #include <sys/zfs_ioctl.h>
60 60 #include <sys/fs/zfs.h>
61 61 #include <sys/dmu.h>
62 62 #include <sys/dmu_objset.h>
63 63 #include <sys/spa.h>
64 64 #include <sys/txg.h>
65 65 #include <sys/dbuf.h>
66 66 #include <sys/zap.h>
67 67 #include <sys/sa.h>
68 68 #include <sys/dirent.h>
69 69 #include <sys/policy.h>
70 70 #include <sys/sunddi.h>
71 71 #include <sys/filio.h>
72 72 #include <sys/sid.h>
73 73 #include "fs/fs_subr.h"
74 74 #include <sys/zfs_ctldir.h>
75 75 #include <sys/zfs_fuid.h>
76 76 #include <sys/zfs_sa.h>
77 77 #include <sys/dnlc.h>
78 78 #include <sys/zfs_rlock.h>
79 79 #include <sys/extdirent.h>
80 80 #include <sys/kidmap.h>
81 81 #include <sys/cred.h>
82 82 #include <sys/attr.h>
83 83
84 84 /*
85 85 * Programming rules.
86 86 *
87 87 * Each vnode op performs some logical unit of work. To do this, the ZPL must
88 88 * properly lock its in-core state, create a DMU transaction, do the work,
89 89 * record this work in the intent log (ZIL), commit the DMU transaction,
90 90 * and wait for the intent log to commit if it is a synchronous operation.
91 91 * Moreover, the vnode ops must work in both normal and log replay context.
92 92 * The ordering of events is important to avoid deadlocks and references
93 93 * to freed memory. The example below illustrates the following Big Rules:
94 94 *
95 95 * (1) A check must be made in each zfs thread for a mounted file system.
96 96 * This is done avoiding races using ZFS_ENTER(zfsvfs).
97 97 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes
98 98 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros
99 99 * can return EIO from the calling function.
100 100 *
101 101 * (2) VN_RELE() should always be the last thing except for zil_commit()
102 102 * (if necessary) and ZFS_EXIT(). This is for 3 reasons:
103 103 * First, if it's the last reference, the vnode/znode
↓ open down ↓ |
103 lines elided |
↑ open up ↑ |
104 104 * can be freed, so the zp may point to freed memory. Second, the last
105 105 * reference will call zfs_zinactive(), which may induce a lot of work --
106 106 * pushing cached pages (which acquires range locks) and syncing out
107 107 * cached atime changes. Third, zfs_zinactive() may require a new tx,
108 108 * which could deadlock the system if you were already holding one.
109 109 * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC().
110 110 *
111 111 * (3) All range locks must be grabbed before calling dmu_tx_assign(),
112 112 * as they can span dmu_tx_assign() calls.
113 113 *
114 - * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign().
115 - * This is critical because we don't want to block while holding locks.
116 - * Note, in particular, that if a lock is sometimes acquired before
117 - * the tx assigns, and sometimes after (e.g. z_lock), then failing to
118 - * use a non-blocking assign can deadlock the system. The scenario:
114 + * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to
115 + * dmu_tx_assign(). This is critical because we don't want to block
116 + * while holding locks.
119 117 *
118 + * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This
119 + * reduces lock contention and CPU usage when we must wait (note that if
120 + * throughput is constrained by the storage, nearly every transaction
121 + * must wait).
122 + *
123 + * Note, in particular, that if a lock is sometimes acquired before
124 + * the tx assigns, and sometimes after (e.g. z_lock), then failing
125 + * to use a non-blocking assign can deadlock the system. The scenario:
126 + *
120 127 * Thread A has grabbed a lock before calling dmu_tx_assign().
121 128 * Thread B is in an already-assigned tx, and blocks for this lock.
122 129 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
123 130 * forever, because the previous txg can't quiesce until B's tx commits.
124 131 *
125 132 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
126 133 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent
127 134 * calls to dmu_tx_assign(), pass TXG_WAITED rather than TXG_NOWAIT,
128 135 * to indicate that this operation has already called dmu_tx_wait().
129 136 * This will ensure that we don't retry forever, waiting a short bit
130 137 * each time.
131 138 *
132 139 * (5) If the operation succeeded, generate the intent log entry for it
133 140 * before dropping locks. This ensures that the ordering of events
134 141 * in the intent log matches the order in which they actually occurred.
135 142 * During ZIL replay the zfs_log_* functions will update the sequence
136 143 * number to indicate the zil transaction has replayed.
137 144 *
138 145 * (6) At the end of each vnode op, the DMU tx must always commit,
139 146 * regardless of whether there were any errors.
140 147 *
141 148 * (7) After dropping all locks, invoke zil_commit(zilog, foid)
142 149 * to ensure that synchronous semantics are provided when necessary.
143 150 *
144 151 * In general, this is how things should be ordered in each vnode op:
145 152 *
146 153 * ZFS_ENTER(zfsvfs); // exit if unmounted
147 154 * top:
148 155 * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
149 156 * rw_enter(...); // grab any other locks you need
150 157 * tx = dmu_tx_create(...); // get DMU tx
151 158 * dmu_tx_hold_*(); // hold each object you might modify
152 159 * error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
153 160 * if (error) {
154 161 * rw_exit(...); // drop locks
155 162 * zfs_dirent_unlock(dl); // unlock directory entry
156 163 * VN_RELE(...); // release held vnodes
157 164 * if (error == ERESTART) {
158 165 * waited = B_TRUE;
159 166 * dmu_tx_wait(tx);
160 167 * dmu_tx_abort(tx);
161 168 * goto top;
162 169 * }
163 170 * dmu_tx_abort(tx); // abort DMU tx
164 171 * ZFS_EXIT(zfsvfs); // finished in zfs
165 172 * return (error); // really out of space
166 173 * }
167 174 * error = do_real_work(); // do whatever this VOP does
168 175 * if (error == 0)
169 176 * zfs_log_*(...); // on success, make ZIL entry
170 177 * dmu_tx_commit(tx); // commit DMU tx -- error or not
171 178 * rw_exit(...); // drop locks
172 179 * zfs_dirent_unlock(dl); // unlock directory entry
173 180 * VN_RELE(...); // release held vnodes
174 181 * zil_commit(zilog, foid); // synchronous when necessary
175 182 * ZFS_EXIT(zfsvfs); // finished in zfs
176 183 * return (error); // done, report error
177 184 */
178 185
179 186 /* ARGSUSED */
180 187 static int
181 188 zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct)
182 189 {
183 190 znode_t *zp = VTOZ(*vpp);
184 191 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
185 192
186 193 ZFS_ENTER(zfsvfs);
187 194 ZFS_VERIFY_ZP(zp);
188 195
189 196 if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) &&
190 197 ((flag & FAPPEND) == 0)) {
191 198 ZFS_EXIT(zfsvfs);
192 199 return (SET_ERROR(EPERM));
193 200 }
194 201
195 202 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
196 203 ZTOV(zp)->v_type == VREG &&
197 204 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) {
198 205 if (fs_vscan(*vpp, cr, 0) != 0) {
199 206 ZFS_EXIT(zfsvfs);
200 207 return (SET_ERROR(EACCES));
201 208 }
202 209 }
203 210
204 211 /* Keep a count of the synchronous opens in the znode */
205 212 if (flag & (FSYNC | FDSYNC))
206 213 atomic_inc_32(&zp->z_sync_cnt);
207 214
208 215 ZFS_EXIT(zfsvfs);
209 216 return (0);
210 217 }
211 218
212 219 /* ARGSUSED */
213 220 static int
214 221 zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr,
215 222 caller_context_t *ct)
216 223 {
217 224 znode_t *zp = VTOZ(vp);
218 225 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
219 226
220 227 /*
221 228 * Clean up any locks held by this process on the vp.
222 229 */
223 230 cleanlocks(vp, ddi_get_pid(), 0);
224 231 cleanshares(vp, ddi_get_pid());
225 232
226 233 ZFS_ENTER(zfsvfs);
227 234 ZFS_VERIFY_ZP(zp);
228 235
229 236 /* Decrement the synchronous opens in the znode */
230 237 if ((flag & (FSYNC | FDSYNC)) && (count == 1))
231 238 atomic_dec_32(&zp->z_sync_cnt);
232 239
233 240 if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan &&
234 241 ZTOV(zp)->v_type == VREG &&
235 242 !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0)
236 243 VERIFY(fs_vscan(vp, cr, 1) == 0);
237 244
238 245 ZFS_EXIT(zfsvfs);
239 246 return (0);
240 247 }
241 248
242 249 /*
243 250 * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
244 251 * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
245 252 */
246 253 static int
247 254 zfs_holey(vnode_t *vp, int cmd, offset_t *off)
248 255 {
249 256 znode_t *zp = VTOZ(vp);
250 257 uint64_t noff = (uint64_t)*off; /* new offset */
251 258 uint64_t file_sz;
252 259 int error;
253 260 boolean_t hole;
254 261
255 262 file_sz = zp->z_size;
256 263 if (noff >= file_sz) {
257 264 return (SET_ERROR(ENXIO));
258 265 }
259 266
260 267 if (cmd == _FIO_SEEK_HOLE)
261 268 hole = B_TRUE;
262 269 else
263 270 hole = B_FALSE;
264 271
265 272 error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
266 273
267 274 /* end of file? */
268 275 if ((error == ESRCH) || (noff > file_sz)) {
269 276 /*
270 277 * Handle the virtual hole at the end of file.
271 278 */
272 279 if (hole) {
273 280 *off = file_sz;
274 281 return (0);
275 282 }
276 283 return (SET_ERROR(ENXIO));
277 284 }
278 285
279 286 if (noff < *off)
280 287 return (error);
281 288 *off = noff;
282 289 return (error);
283 290 }
284 291
285 292 /* ARGSUSED */
286 293 static int
287 294 zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
288 295 int *rvalp, caller_context_t *ct)
289 296 {
290 297 offset_t off;
291 298 int error;
292 299 zfsvfs_t *zfsvfs;
293 300 znode_t *zp;
294 301
295 302 switch (com) {
296 303 case _FIOFFS:
297 304 return (zfs_sync(vp->v_vfsp, 0, cred));
298 305
299 306 /*
300 307 * The following two ioctls are used by bfu. Faking out,
301 308 * necessary to avoid bfu errors.
302 309 */
303 310 case _FIOGDIO:
304 311 case _FIOSDIO:
305 312 return (0);
306 313
307 314 case _FIO_SEEK_DATA:
308 315 case _FIO_SEEK_HOLE:
309 316 if (ddi_copyin((void *)data, &off, sizeof (off), flag))
310 317 return (SET_ERROR(EFAULT));
311 318
312 319 zp = VTOZ(vp);
313 320 zfsvfs = zp->z_zfsvfs;
314 321 ZFS_ENTER(zfsvfs);
315 322 ZFS_VERIFY_ZP(zp);
316 323
317 324 /* offset parameter is in/out */
318 325 error = zfs_holey(vp, com, &off);
319 326 ZFS_EXIT(zfsvfs);
320 327 if (error)
321 328 return (error);
322 329 if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
323 330 return (SET_ERROR(EFAULT));
324 331 return (0);
325 332 }
326 333 return (SET_ERROR(ENOTTY));
327 334 }
328 335
329 336 /*
330 337 * Utility functions to map and unmap a single physical page. These
331 338 * are used to manage the mappable copies of ZFS file data, and therefore
332 339 * do not update ref/mod bits.
333 340 */
334 341 caddr_t
335 342 zfs_map_page(page_t *pp, enum seg_rw rw)
336 343 {
337 344 if (kpm_enable)
338 345 return (hat_kpm_mapin(pp, 0));
339 346 ASSERT(rw == S_READ || rw == S_WRITE);
340 347 return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0),
341 348 (caddr_t)-1));
342 349 }
343 350
344 351 void
345 352 zfs_unmap_page(page_t *pp, caddr_t addr)
346 353 {
347 354 if (kpm_enable) {
348 355 hat_kpm_mapout(pp, 0, addr);
349 356 } else {
350 357 ppmapout(addr);
351 358 }
352 359 }
353 360
354 361 /*
355 362 * When a file is memory mapped, we must keep the IO data synchronized
356 363 * between the DMU cache and the memory mapped pages. What this means:
357 364 *
358 365 * On Write: If we find a memory mapped page, we write to *both*
359 366 * the page and the dmu buffer.
360 367 */
361 368 static void
362 369 update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid)
363 370 {
364 371 int64_t off;
365 372
366 373 off = start & PAGEOFFSET;
367 374 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
368 375 page_t *pp;
369 376 uint64_t nbytes = MIN(PAGESIZE - off, len);
370 377
371 378 if (pp = page_lookup(vp, start, SE_SHARED)) {
372 379 caddr_t va;
373 380
374 381 va = zfs_map_page(pp, S_WRITE);
375 382 (void) dmu_read(os, oid, start+off, nbytes, va+off,
376 383 DMU_READ_PREFETCH);
377 384 zfs_unmap_page(pp, va);
378 385 page_unlock(pp);
379 386 }
380 387 len -= nbytes;
381 388 off = 0;
382 389 }
383 390 }
384 391
385 392 /*
386 393 * When a file is memory mapped, we must keep the IO data synchronized
387 394 * between the DMU cache and the memory mapped pages. What this means:
388 395 *
389 396 * On Read: We "read" preferentially from memory mapped pages,
390 397 * else we default from the dmu buffer.
391 398 *
392 399 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
393 400 * the file is memory mapped.
394 401 */
395 402 static int
396 403 mappedread(vnode_t *vp, int nbytes, uio_t *uio)
397 404 {
398 405 znode_t *zp = VTOZ(vp);
399 406 objset_t *os = zp->z_zfsvfs->z_os;
400 407 int64_t start, off;
401 408 int len = nbytes;
402 409 int error = 0;
403 410
404 411 start = uio->uio_loffset;
405 412 off = start & PAGEOFFSET;
406 413 for (start &= PAGEMASK; len > 0; start += PAGESIZE) {
407 414 page_t *pp;
408 415 uint64_t bytes = MIN(PAGESIZE - off, len);
409 416
410 417 if (pp = page_lookup(vp, start, SE_SHARED)) {
411 418 caddr_t va;
412 419
413 420 va = zfs_map_page(pp, S_READ);
414 421 error = uiomove(va + off, bytes, UIO_READ, uio);
415 422 zfs_unmap_page(pp, va);
416 423 page_unlock(pp);
417 424 } else {
418 425 error = dmu_read_uio(os, zp->z_id, uio, bytes);
419 426 }
420 427 len -= bytes;
421 428 off = 0;
422 429 if (error)
423 430 break;
424 431 }
425 432 return (error);
426 433 }
427 434
428 435 offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
429 436
430 437 /*
431 438 * Read bytes from specified file into supplied buffer.
432 439 *
433 440 * IN: vp - vnode of file to be read from.
434 441 * uio - structure supplying read location, range info,
435 442 * and return buffer.
436 443 * ioflag - SYNC flags; used to provide FRSYNC semantics.
437 444 * cr - credentials of caller.
438 445 * ct - caller context
439 446 *
440 447 * OUT: uio - updated offset and range, buffer filled.
441 448 *
442 449 * RETURN: 0 on success, error code on failure.
443 450 *
444 451 * Side Effects:
445 452 * vp - atime updated if byte count > 0
446 453 */
447 454 /* ARGSUSED */
448 455 static int
449 456 zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
450 457 {
451 458 znode_t *zp = VTOZ(vp);
452 459 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
453 460 objset_t *os;
454 461 ssize_t n, nbytes;
455 462 int error = 0;
456 463 rl_t *rl;
457 464 xuio_t *xuio = NULL;
458 465
459 466 ZFS_ENTER(zfsvfs);
460 467 ZFS_VERIFY_ZP(zp);
461 468 os = zfsvfs->z_os;
462 469
463 470 if (zp->z_pflags & ZFS_AV_QUARANTINED) {
464 471 ZFS_EXIT(zfsvfs);
465 472 return (SET_ERROR(EACCES));
466 473 }
467 474
468 475 /*
469 476 * Validate file offset
470 477 */
471 478 if (uio->uio_loffset < (offset_t)0) {
472 479 ZFS_EXIT(zfsvfs);
473 480 return (SET_ERROR(EINVAL));
474 481 }
475 482
476 483 /*
477 484 * Fasttrack empty reads
478 485 */
479 486 if (uio->uio_resid == 0) {
480 487 ZFS_EXIT(zfsvfs);
481 488 return (0);
482 489 }
483 490
484 491 /*
485 492 * Check for mandatory locks
486 493 */
487 494 if (MANDMODE(zp->z_mode)) {
488 495 if (error = chklock(vp, FREAD,
489 496 uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
490 497 ZFS_EXIT(zfsvfs);
491 498 return (error);
492 499 }
493 500 }
494 501
495 502 /*
496 503 * If we're in FRSYNC mode, sync out this znode before reading it.
497 504 */
498 505 if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
499 506 zil_commit(zfsvfs->z_log, zp->z_id);
500 507
501 508 /*
502 509 * Lock the range against changes.
503 510 */
504 511 rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
505 512
506 513 /*
507 514 * If we are reading past end-of-file we can skip
508 515 * to the end; but we might still need to set atime.
509 516 */
510 517 if (uio->uio_loffset >= zp->z_size) {
511 518 error = 0;
512 519 goto out;
513 520 }
514 521
515 522 ASSERT(uio->uio_loffset < zp->z_size);
516 523 n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset);
517 524
518 525 if ((uio->uio_extflg == UIO_XUIO) &&
519 526 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) {
520 527 int nblk;
521 528 int blksz = zp->z_blksz;
522 529 uint64_t offset = uio->uio_loffset;
523 530
524 531 xuio = (xuio_t *)uio;
525 532 if ((ISP2(blksz))) {
526 533 nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset,
527 534 blksz)) / blksz;
528 535 } else {
529 536 ASSERT(offset + n <= blksz);
530 537 nblk = 1;
531 538 }
532 539 (void) dmu_xuio_init(xuio, nblk);
533 540
534 541 if (vn_has_cached_data(vp)) {
535 542 /*
536 543 * For simplicity, we always allocate a full buffer
537 544 * even if we only expect to read a portion of a block.
538 545 */
539 546 while (--nblk >= 0) {
540 547 (void) dmu_xuio_add(xuio,
541 548 dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
542 549 blksz), 0, blksz);
543 550 }
544 551 }
545 552 }
546 553
547 554 while (n > 0) {
548 555 nbytes = MIN(n, zfs_read_chunk_size -
549 556 P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
550 557
551 558 if (vn_has_cached_data(vp))
552 559 error = mappedread(vp, nbytes, uio);
553 560 else
554 561 error = dmu_read_uio(os, zp->z_id, uio, nbytes);
555 562 if (error) {
556 563 /* convert checksum errors into IO errors */
557 564 if (error == ECKSUM)
558 565 error = SET_ERROR(EIO);
559 566 break;
560 567 }
561 568
562 569 n -= nbytes;
563 570 }
564 571 out:
565 572 zfs_range_unlock(rl);
566 573
567 574 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
568 575 ZFS_EXIT(zfsvfs);
569 576 return (error);
570 577 }
571 578
572 579 /*
573 580 * Write the bytes to a file.
574 581 *
575 582 * IN: vp - vnode of file to be written to.
576 583 * uio - structure supplying write location, range info,
577 584 * and data buffer.
578 585 * ioflag - FAPPEND, FSYNC, and/or FDSYNC. FAPPEND is
579 586 * set if in append mode.
580 587 * cr - credentials of caller.
581 588 * ct - caller context (NFS/CIFS fem monitor only)
582 589 *
583 590 * OUT: uio - updated offset and range.
584 591 *
585 592 * RETURN: 0 on success, error code on failure.
586 593 *
587 594 * Timestamps:
588 595 * vp - ctime|mtime updated if byte count > 0
589 596 */
590 597
591 598 /* ARGSUSED */
592 599 static int
593 600 zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
594 601 {
595 602 znode_t *zp = VTOZ(vp);
596 603 rlim64_t limit = uio->uio_llimit;
597 604 ssize_t start_resid = uio->uio_resid;
598 605 ssize_t tx_bytes;
599 606 uint64_t end_size;
600 607 dmu_tx_t *tx;
601 608 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
602 609 zilog_t *zilog;
603 610 offset_t woff;
604 611 ssize_t n, nbytes;
605 612 rl_t *rl;
606 613 int max_blksz = zfsvfs->z_max_blksz;
607 614 int error = 0;
608 615 arc_buf_t *abuf;
609 616 iovec_t *aiov = NULL;
610 617 xuio_t *xuio = NULL;
611 618 int i_iov = 0;
612 619 int iovcnt = uio->uio_iovcnt;
613 620 iovec_t *iovp = uio->uio_iov;
614 621 int write_eof;
615 622 int count = 0;
616 623 sa_bulk_attr_t bulk[4];
617 624 uint64_t mtime[2], ctime[2];
618 625
619 626 /*
620 627 * Fasttrack empty write
621 628 */
622 629 n = start_resid;
623 630 if (n == 0)
624 631 return (0);
625 632
626 633 if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
627 634 limit = MAXOFFSET_T;
628 635
629 636 ZFS_ENTER(zfsvfs);
630 637 ZFS_VERIFY_ZP(zp);
631 638
632 639 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
633 640 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
634 641 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL,
635 642 &zp->z_size, 8);
636 643 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
637 644 &zp->z_pflags, 8);
638 645
639 646 /*
640 647 * If immutable or not appending then return EPERM
641 648 */
642 649 if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) ||
643 650 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) &&
644 651 (uio->uio_loffset < zp->z_size))) {
645 652 ZFS_EXIT(zfsvfs);
646 653 return (SET_ERROR(EPERM));
647 654 }
648 655
649 656 zilog = zfsvfs->z_log;
650 657
651 658 /*
652 659 * Validate file offset
653 660 */
654 661 woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset;
655 662 if (woff < 0) {
656 663 ZFS_EXIT(zfsvfs);
657 664 return (SET_ERROR(EINVAL));
658 665 }
659 666
660 667 /*
661 668 * Check for mandatory locks before calling zfs_range_lock()
662 669 * in order to prevent a deadlock with locks set via fcntl().
663 670 */
664 671 if (MANDMODE((mode_t)zp->z_mode) &&
665 672 (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
666 673 ZFS_EXIT(zfsvfs);
667 674 return (error);
668 675 }
669 676
670 677 /*
671 678 * Pre-fault the pages to ensure slow (eg NFS) pages
672 679 * don't hold up txg.
673 680 * Skip this if uio contains loaned arc_buf.
674 681 */
675 682 if ((uio->uio_extflg == UIO_XUIO) &&
676 683 (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY))
677 684 xuio = (xuio_t *)uio;
678 685 else
679 686 uio_prefaultpages(MIN(n, max_blksz), uio);
680 687
681 688 /*
682 689 * If in append mode, set the io offset pointer to eof.
683 690 */
684 691 if (ioflag & FAPPEND) {
685 692 /*
686 693 * Obtain an appending range lock to guarantee file append
687 694 * semantics. We reset the write offset once we have the lock.
688 695 */
689 696 rl = zfs_range_lock(zp, 0, n, RL_APPEND);
690 697 woff = rl->r_off;
691 698 if (rl->r_len == UINT64_MAX) {
692 699 /*
693 700 * We overlocked the file because this write will cause
694 701 * the file block size to increase.
695 702 * Note that zp_size cannot change with this lock held.
696 703 */
697 704 woff = zp->z_size;
698 705 }
699 706 uio->uio_loffset = woff;
700 707 } else {
701 708 /*
702 709 * Note that if the file block size will change as a result of
703 710 * this write, then this range lock will lock the entire file
704 711 * so that we can re-write the block safely.
705 712 */
706 713 rl = zfs_range_lock(zp, woff, n, RL_WRITER);
707 714 }
708 715
709 716 if (woff >= limit) {
710 717 zfs_range_unlock(rl);
711 718 ZFS_EXIT(zfsvfs);
712 719 return (SET_ERROR(EFBIG));
713 720 }
714 721
715 722 if ((woff + n) > limit || woff > (limit - n))
716 723 n = limit - woff;
717 724
718 725 /* Will this write extend the file length? */
719 726 write_eof = (woff + n > zp->z_size);
720 727
↓ open down ↓ |
591 lines elided |
↑ open up ↑ |
721 728 end_size = MAX(zp->z_size, woff + n);
722 729
723 730 /*
724 731 * Write the file in reasonable size chunks. Each chunk is written
725 732 * in a separate transaction; this keeps the intent log records small
726 733 * and allows us to do more fine-grained space accounting.
727 734 */
728 735 while (n > 0) {
729 736 abuf = NULL;
730 737 woff = uio->uio_loffset;
731 -again:
732 738 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
733 739 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
734 740 if (abuf != NULL)
735 741 dmu_return_arcbuf(abuf);
736 742 error = SET_ERROR(EDQUOT);
737 743 break;
738 744 }
739 745
740 746 if (xuio && abuf == NULL) {
741 747 ASSERT(i_iov < iovcnt);
742 748 aiov = &iovp[i_iov];
743 749 abuf = dmu_xuio_arcbuf(xuio, i_iov);
744 750 dmu_xuio_clear(xuio, i_iov);
745 751 DTRACE_PROBE3(zfs_cp_write, int, i_iov,
746 752 iovec_t *, aiov, arc_buf_t *, abuf);
747 753 ASSERT((aiov->iov_base == abuf->b_data) ||
748 754 ((char *)aiov->iov_base - (char *)abuf->b_data +
749 755 aiov->iov_len == arc_buf_size(abuf)));
750 756 i_iov++;
751 757 } else if (abuf == NULL && n >= max_blksz &&
752 758 woff >= zp->z_size &&
753 759 P2PHASE(woff, max_blksz) == 0 &&
754 760 zp->z_blksz == max_blksz) {
755 761 /*
756 762 * This write covers a full block. "Borrow" a buffer
757 763 * from the dmu so that we can fill it before we enter
758 764 * a transaction. This avoids the possibility of
759 765 * holding up the transaction if the data copy hangs
760 766 * up on a pagefault (e.g., from an NFS server mapping).
761 767 */
762 768 size_t cbytes;
763 769
764 770 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
765 771 max_blksz);
766 772 ASSERT(abuf != NULL);
767 773 ASSERT(arc_buf_size(abuf) == max_blksz);
768 774 if (error = uiocopy(abuf->b_data, max_blksz,
769 775 UIO_WRITE, uio, &cbytes)) {
770 776 dmu_return_arcbuf(abuf);
771 777 break;
772 778 }
↓ open down ↓ |
31 lines elided |
↑ open up ↑ |
773 779 ASSERT(cbytes == max_blksz);
774 780 }
775 781
776 782 /*
777 783 * Start a transaction.
778 784 */
779 785 tx = dmu_tx_create(zfsvfs->z_os);
780 786 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
781 787 dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz));
782 788 zfs_sa_upgrade_txholds(tx, zp);
783 - error = dmu_tx_assign(tx, TXG_NOWAIT);
789 + error = dmu_tx_assign(tx, TXG_WAIT);
784 790 if (error) {
785 - if (error == ERESTART) {
786 - dmu_tx_wait(tx);
787 - dmu_tx_abort(tx);
788 - goto again;
789 - }
790 791 dmu_tx_abort(tx);
791 792 if (abuf != NULL)
792 793 dmu_return_arcbuf(abuf);
793 794 break;
794 795 }
795 796
796 797 /*
797 798 * If zfs_range_lock() over-locked we grow the blocksize
798 799 * and then reduce the lock range. This will only happen
799 800 * on the first iteration since zfs_range_reduce() will
800 801 * shrink down r_len to the appropriate size.
801 802 */
802 803 if (rl->r_len == UINT64_MAX) {
803 804 uint64_t new_blksz;
804 805
805 806 if (zp->z_blksz > max_blksz) {
806 807 ASSERT(!ISP2(zp->z_blksz));
807 808 new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE);
808 809 } else {
809 810 new_blksz = MIN(end_size, max_blksz);
810 811 }
811 812 zfs_grow_blocksize(zp, new_blksz, tx);
812 813 zfs_range_reduce(rl, woff, n);
813 814 }
814 815
815 816 /*
816 817 * XXX - should we really limit each write to z_max_blksz?
817 818 * Perhaps we should use SPA_MAXBLOCKSIZE chunks?
818 819 */
819 820 nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz));
820 821
821 822 if (abuf == NULL) {
822 823 tx_bytes = uio->uio_resid;
823 824 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl),
824 825 uio, nbytes, tx);
825 826 tx_bytes -= uio->uio_resid;
826 827 } else {
827 828 tx_bytes = nbytes;
828 829 ASSERT(xuio == NULL || tx_bytes == aiov->iov_len);
829 830 /*
830 831 * If this is not a full block write, but we are
831 832 * extending the file past EOF and this data starts
832 833 * block-aligned, use assign_arcbuf(). Otherwise,
833 834 * write via dmu_write().
834 835 */
835 836 if (tx_bytes < max_blksz && (!write_eof ||
836 837 aiov->iov_base != abuf->b_data)) {
837 838 ASSERT(xuio);
838 839 dmu_write(zfsvfs->z_os, zp->z_id, woff,
839 840 aiov->iov_len, aiov->iov_base, tx);
840 841 dmu_return_arcbuf(abuf);
841 842 xuio_stat_wbuf_copied();
842 843 } else {
843 844 ASSERT(xuio || tx_bytes == max_blksz);
844 845 dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl),
845 846 woff, abuf, tx);
846 847 }
847 848 ASSERT(tx_bytes <= uio->uio_resid);
848 849 uioskip(uio, tx_bytes);
849 850 }
850 851 if (tx_bytes && vn_has_cached_data(vp)) {
851 852 update_pages(vp, woff,
852 853 tx_bytes, zfsvfs->z_os, zp->z_id);
853 854 }
854 855
855 856 /*
856 857 * If we made no progress, we're done. If we made even
857 858 * partial progress, update the znode and ZIL accordingly.
858 859 */
859 860 if (tx_bytes == 0) {
860 861 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
861 862 (void *)&zp->z_size, sizeof (uint64_t), tx);
862 863 dmu_tx_commit(tx);
863 864 ASSERT(error != 0);
864 865 break;
865 866 }
866 867
867 868 /*
868 869 * Clear Set-UID/Set-GID bits on successful write if not
869 870 * privileged and at least one of the excute bits is set.
870 871 *
871 872 * It would be nice to to this after all writes have
872 873 * been done, but that would still expose the ISUID/ISGID
873 874 * to another app after the partial write is committed.
874 875 *
875 876 * Note: we don't call zfs_fuid_map_id() here because
876 877 * user 0 is not an ephemeral uid.
877 878 */
878 879 mutex_enter(&zp->z_acl_lock);
879 880 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) |
880 881 (S_IXUSR >> 6))) != 0 &&
881 882 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 &&
882 883 secpolicy_vnode_setid_retain(cr,
883 884 (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) {
884 885 uint64_t newmode;
885 886 zp->z_mode &= ~(S_ISUID | S_ISGID);
886 887 newmode = zp->z_mode;
887 888 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs),
888 889 (void *)&newmode, sizeof (uint64_t), tx);
889 890 }
890 891 mutex_exit(&zp->z_acl_lock);
891 892
892 893 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
893 894 B_TRUE);
894 895
895 896 /*
896 897 * Update the file size (zp_size) if it has changed;
897 898 * account for possible concurrent updates.
898 899 */
899 900 while ((end_size = zp->z_size) < uio->uio_loffset) {
900 901 (void) atomic_cas_64(&zp->z_size, end_size,
901 902 uio->uio_loffset);
902 903 ASSERT(error == 0);
903 904 }
904 905 /*
905 906 * If we are replaying and eof is non zero then force
906 907 * the file size to the specified eof. Note, there's no
907 908 * concurrency during replay.
908 909 */
909 910 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0)
910 911 zp->z_size = zfsvfs->z_replay_eof;
911 912
912 913 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
913 914
914 915 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag);
915 916 dmu_tx_commit(tx);
916 917
917 918 if (error != 0)
918 919 break;
919 920 ASSERT(tx_bytes == nbytes);
920 921 n -= nbytes;
921 922
922 923 if (!xuio && n > 0)
923 924 uio_prefaultpages(MIN(n, max_blksz), uio);
924 925 }
925 926
926 927 zfs_range_unlock(rl);
927 928
928 929 /*
929 930 * If we're in replay mode, or we made no progress, return error.
930 931 * Otherwise, it's at least a partial write, so it's successful.
931 932 */
932 933 if (zfsvfs->z_replay || uio->uio_resid == start_resid) {
933 934 ZFS_EXIT(zfsvfs);
934 935 return (error);
935 936 }
936 937
937 938 if (ioflag & (FSYNC | FDSYNC) ||
938 939 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
939 940 zil_commit(zilog, zp->z_id);
940 941
941 942 ZFS_EXIT(zfsvfs);
942 943 return (0);
943 944 }
944 945
945 946 void
946 947 zfs_get_done(zgd_t *zgd, int error)
947 948 {
948 949 znode_t *zp = zgd->zgd_private;
949 950 objset_t *os = zp->z_zfsvfs->z_os;
950 951
951 952 if (zgd->zgd_db)
952 953 dmu_buf_rele(zgd->zgd_db, zgd);
953 954
954 955 zfs_range_unlock(zgd->zgd_rl);
955 956
956 957 /*
957 958 * Release the vnode asynchronously as we currently have the
958 959 * txg stopped from syncing.
959 960 */
960 961 VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
961 962
962 963 if (error == 0 && zgd->zgd_bp)
963 964 zil_add_block(zgd->zgd_zilog, zgd->zgd_bp);
964 965
965 966 kmem_free(zgd, sizeof (zgd_t));
966 967 }
967 968
968 969 #ifdef DEBUG
969 970 static int zil_fault_io = 0;
970 971 #endif
971 972
972 973 /*
973 974 * Get data to generate a TX_WRITE intent log record.
974 975 */
975 976 int
976 977 zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio)
977 978 {
978 979 zfsvfs_t *zfsvfs = arg;
979 980 objset_t *os = zfsvfs->z_os;
980 981 znode_t *zp;
981 982 uint64_t object = lr->lr_foid;
982 983 uint64_t offset = lr->lr_offset;
983 984 uint64_t size = lr->lr_length;
984 985 blkptr_t *bp = &lr->lr_blkptr;
985 986 dmu_buf_t *db;
986 987 zgd_t *zgd;
987 988 int error = 0;
988 989
989 990 ASSERT(zio != NULL);
990 991 ASSERT(size != 0);
991 992
992 993 /*
993 994 * Nothing to do if the file has been removed
994 995 */
995 996 if (zfs_zget(zfsvfs, object, &zp) != 0)
996 997 return (SET_ERROR(ENOENT));
997 998 if (zp->z_unlinked) {
998 999 /*
999 1000 * Release the vnode asynchronously as we currently have the
1000 1001 * txg stopped from syncing.
1001 1002 */
1002 1003 VN_RELE_ASYNC(ZTOV(zp),
1003 1004 dsl_pool_vnrele_taskq(dmu_objset_pool(os)));
1004 1005 return (SET_ERROR(ENOENT));
1005 1006 }
1006 1007
1007 1008 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP);
1008 1009 zgd->zgd_zilog = zfsvfs->z_log;
1009 1010 zgd->zgd_private = zp;
1010 1011
1011 1012 /*
1012 1013 * Write records come in two flavors: immediate and indirect.
1013 1014 * For small writes it's cheaper to store the data with the
1014 1015 * log record (immediate); for large writes it's cheaper to
1015 1016 * sync the data and get a pointer to it (indirect) so that
1016 1017 * we don't have to write the data twice.
1017 1018 */
1018 1019 if (buf != NULL) { /* immediate write */
1019 1020 zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER);
1020 1021 /* test for truncation needs to be done while range locked */
1021 1022 if (offset >= zp->z_size) {
1022 1023 error = SET_ERROR(ENOENT);
1023 1024 } else {
1024 1025 error = dmu_read(os, object, offset, size, buf,
1025 1026 DMU_READ_NO_PREFETCH);
1026 1027 }
1027 1028 ASSERT(error == 0 || error == ENOENT);
1028 1029 } else { /* indirect write */
1029 1030 /*
1030 1031 * Have to lock the whole block to ensure when it's
1031 1032 * written out and it's checksum is being calculated
1032 1033 * that no one can change the data. We need to re-check
1033 1034 * blocksize after we get the lock in case it's changed!
1034 1035 */
1035 1036 for (;;) {
1036 1037 uint64_t blkoff;
1037 1038 size = zp->z_blksz;
1038 1039 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset;
1039 1040 offset -= blkoff;
1040 1041 zgd->zgd_rl = zfs_range_lock(zp, offset, size,
1041 1042 RL_READER);
1042 1043 if (zp->z_blksz == size)
1043 1044 break;
1044 1045 offset += blkoff;
1045 1046 zfs_range_unlock(zgd->zgd_rl);
1046 1047 }
1047 1048 /* test for truncation needs to be done while range locked */
1048 1049 if (lr->lr_offset >= zp->z_size)
1049 1050 error = SET_ERROR(ENOENT);
1050 1051 #ifdef DEBUG
1051 1052 if (zil_fault_io) {
1052 1053 error = SET_ERROR(EIO);
1053 1054 zil_fault_io = 0;
1054 1055 }
1055 1056 #endif
1056 1057 if (error == 0)
1057 1058 error = dmu_buf_hold(os, object, offset, zgd, &db,
1058 1059 DMU_READ_NO_PREFETCH);
1059 1060
1060 1061 if (error == 0) {
1061 1062 blkptr_t *obp = dmu_buf_get_blkptr(db);
1062 1063 if (obp) {
1063 1064 ASSERT(BP_IS_HOLE(bp));
1064 1065 *bp = *obp;
1065 1066 }
1066 1067
1067 1068 zgd->zgd_db = db;
1068 1069 zgd->zgd_bp = bp;
1069 1070
1070 1071 ASSERT(db->db_offset == offset);
1071 1072 ASSERT(db->db_size == size);
1072 1073
1073 1074 error = dmu_sync(zio, lr->lr_common.lrc_txg,
1074 1075 zfs_get_done, zgd);
1075 1076 ASSERT(error || lr->lr_length <= zp->z_blksz);
1076 1077
1077 1078 /*
1078 1079 * On success, we need to wait for the write I/O
1079 1080 * initiated by dmu_sync() to complete before we can
1080 1081 * release this dbuf. We will finish everything up
1081 1082 * in the zfs_get_done() callback.
1082 1083 */
1083 1084 if (error == 0)
1084 1085 return (0);
1085 1086
1086 1087 if (error == EALREADY) {
1087 1088 lr->lr_common.lrc_txtype = TX_WRITE2;
1088 1089 error = 0;
1089 1090 }
1090 1091 }
1091 1092 }
1092 1093
1093 1094 zfs_get_done(zgd, error);
1094 1095
1095 1096 return (error);
1096 1097 }
1097 1098
1098 1099 /*ARGSUSED*/
1099 1100 static int
1100 1101 zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr,
1101 1102 caller_context_t *ct)
1102 1103 {
1103 1104 znode_t *zp = VTOZ(vp);
1104 1105 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
1105 1106 int error;
1106 1107
1107 1108 ZFS_ENTER(zfsvfs);
1108 1109 ZFS_VERIFY_ZP(zp);
1109 1110
1110 1111 if (flag & V_ACE_MASK)
1111 1112 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr);
1112 1113 else
1113 1114 error = zfs_zaccess_rwx(zp, mode, flag, cr);
1114 1115
1115 1116 ZFS_EXIT(zfsvfs);
1116 1117 return (error);
1117 1118 }
1118 1119
1119 1120 /*
1120 1121 * If vnode is for a device return a specfs vnode instead.
1121 1122 */
1122 1123 static int
1123 1124 specvp_check(vnode_t **vpp, cred_t *cr)
1124 1125 {
1125 1126 int error = 0;
1126 1127
1127 1128 if (IS_DEVVP(*vpp)) {
1128 1129 struct vnode *svp;
1129 1130
1130 1131 svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr);
1131 1132 VN_RELE(*vpp);
1132 1133 if (svp == NULL)
1133 1134 error = SET_ERROR(ENOSYS);
1134 1135 *vpp = svp;
1135 1136 }
1136 1137 return (error);
1137 1138 }
1138 1139
1139 1140
1140 1141 /*
1141 1142 * Lookup an entry in a directory, or an extended attribute directory.
1142 1143 * If it exists, return a held vnode reference for it.
1143 1144 *
1144 1145 * IN: dvp - vnode of directory to search.
1145 1146 * nm - name of entry to lookup.
1146 1147 * pnp - full pathname to lookup [UNUSED].
1147 1148 * flags - LOOKUP_XATTR set if looking for an attribute.
1148 1149 * rdir - root directory vnode [UNUSED].
1149 1150 * cr - credentials of caller.
1150 1151 * ct - caller context
1151 1152 * direntflags - directory lookup flags
1152 1153 * realpnp - returned pathname.
1153 1154 *
1154 1155 * OUT: vpp - vnode of located entry, NULL if not found.
1155 1156 *
1156 1157 * RETURN: 0 on success, error code on failure.
1157 1158 *
1158 1159 * Timestamps:
1159 1160 * NA
1160 1161 */
1161 1162 /* ARGSUSED */
1162 1163 static int
1163 1164 zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp,
1164 1165 int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct,
1165 1166 int *direntflags, pathname_t *realpnp)
1166 1167 {
1167 1168 znode_t *zdp = VTOZ(dvp);
1168 1169 zfsvfs_t *zfsvfs = zdp->z_zfsvfs;
1169 1170 int error = 0;
1170 1171
1171 1172 /* fast path */
1172 1173 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) {
1173 1174
1174 1175 if (dvp->v_type != VDIR) {
1175 1176 return (SET_ERROR(ENOTDIR));
1176 1177 } else if (zdp->z_sa_hdl == NULL) {
1177 1178 return (SET_ERROR(EIO));
1178 1179 }
1179 1180
1180 1181 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) {
1181 1182 error = zfs_fastaccesschk_execute(zdp, cr);
1182 1183 if (!error) {
1183 1184 *vpp = dvp;
1184 1185 VN_HOLD(*vpp);
1185 1186 return (0);
1186 1187 }
1187 1188 return (error);
1188 1189 } else {
1189 1190 vnode_t *tvp = dnlc_lookup(dvp, nm);
1190 1191
1191 1192 if (tvp) {
1192 1193 error = zfs_fastaccesschk_execute(zdp, cr);
1193 1194 if (error) {
1194 1195 VN_RELE(tvp);
1195 1196 return (error);
1196 1197 }
1197 1198 if (tvp == DNLC_NO_VNODE) {
1198 1199 VN_RELE(tvp);
1199 1200 return (SET_ERROR(ENOENT));
1200 1201 } else {
1201 1202 *vpp = tvp;
1202 1203 return (specvp_check(vpp, cr));
1203 1204 }
1204 1205 }
1205 1206 }
1206 1207 }
1207 1208
1208 1209 DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm);
1209 1210
1210 1211 ZFS_ENTER(zfsvfs);
1211 1212 ZFS_VERIFY_ZP(zdp);
1212 1213
1213 1214 *vpp = NULL;
1214 1215
1215 1216 if (flags & LOOKUP_XATTR) {
1216 1217 /*
1217 1218 * If the xattr property is off, refuse the lookup request.
1218 1219 */
1219 1220 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) {
1220 1221 ZFS_EXIT(zfsvfs);
1221 1222 return (SET_ERROR(EINVAL));
1222 1223 }
1223 1224
1224 1225 /*
1225 1226 * We don't allow recursive attributes..
1226 1227 * Maybe someday we will.
1227 1228 */
1228 1229 if (zdp->z_pflags & ZFS_XATTR) {
1229 1230 ZFS_EXIT(zfsvfs);
1230 1231 return (SET_ERROR(EINVAL));
1231 1232 }
1232 1233
1233 1234 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) {
1234 1235 ZFS_EXIT(zfsvfs);
1235 1236 return (error);
1236 1237 }
1237 1238
1238 1239 /*
1239 1240 * Do we have permission to get into attribute directory?
1240 1241 */
1241 1242
1242 1243 if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0,
1243 1244 B_FALSE, cr)) {
1244 1245 VN_RELE(*vpp);
1245 1246 *vpp = NULL;
1246 1247 }
1247 1248
1248 1249 ZFS_EXIT(zfsvfs);
1249 1250 return (error);
1250 1251 }
1251 1252
1252 1253 if (dvp->v_type != VDIR) {
1253 1254 ZFS_EXIT(zfsvfs);
1254 1255 return (SET_ERROR(ENOTDIR));
1255 1256 }
1256 1257
1257 1258 /*
1258 1259 * Check accessibility of directory.
1259 1260 */
1260 1261
1261 1262 if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) {
1262 1263 ZFS_EXIT(zfsvfs);
1263 1264 return (error);
1264 1265 }
1265 1266
1266 1267 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm),
1267 1268 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1268 1269 ZFS_EXIT(zfsvfs);
1269 1270 return (SET_ERROR(EILSEQ));
1270 1271 }
1271 1272
1272 1273 error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp);
1273 1274 if (error == 0)
1274 1275 error = specvp_check(vpp, cr);
1275 1276
1276 1277 ZFS_EXIT(zfsvfs);
1277 1278 return (error);
1278 1279 }
1279 1280
1280 1281 /*
1281 1282 * Attempt to create a new entry in a directory. If the entry
1282 1283 * already exists, truncate the file if permissible, else return
1283 1284 * an error. Return the vp of the created or trunc'd file.
1284 1285 *
1285 1286 * IN: dvp - vnode of directory to put new file entry in.
1286 1287 * name - name of new file entry.
1287 1288 * vap - attributes of new file.
1288 1289 * excl - flag indicating exclusive or non-exclusive mode.
1289 1290 * mode - mode to open file with.
1290 1291 * cr - credentials of caller.
1291 1292 * flag - large file flag [UNUSED].
1292 1293 * ct - caller context
1293 1294 * vsecp - ACL to be set
1294 1295 *
1295 1296 * OUT: vpp - vnode of created or trunc'd entry.
1296 1297 *
1297 1298 * RETURN: 0 on success, error code on failure.
1298 1299 *
1299 1300 * Timestamps:
1300 1301 * dvp - ctime|mtime updated if new entry created
1301 1302 * vp - ctime|mtime always, atime if new
1302 1303 */
1303 1304
1304 1305 /* ARGSUSED */
1305 1306 static int
1306 1307 zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl,
1307 1308 int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct,
1308 1309 vsecattr_t *vsecp)
1309 1310 {
1310 1311 znode_t *zp, *dzp = VTOZ(dvp);
1311 1312 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1312 1313 zilog_t *zilog;
1313 1314 objset_t *os;
1314 1315 zfs_dirlock_t *dl;
1315 1316 dmu_tx_t *tx;
1316 1317 int error;
1317 1318 ksid_t *ksid;
1318 1319 uid_t uid;
1319 1320 gid_t gid = crgetgid(cr);
1320 1321 zfs_acl_ids_t acl_ids;
1321 1322 boolean_t fuid_dirtied;
1322 1323 boolean_t have_acl = B_FALSE;
1323 1324 boolean_t waited = B_FALSE;
1324 1325
1325 1326 /*
1326 1327 * If we have an ephemeral id, ACL, or XVATTR then
1327 1328 * make sure file system is at proper version
1328 1329 */
1329 1330
1330 1331 ksid = crgetsid(cr, KSID_OWNER);
1331 1332 if (ksid)
1332 1333 uid = ksid_getid(ksid);
1333 1334 else
1334 1335 uid = crgetuid(cr);
1335 1336
1336 1337 if (zfsvfs->z_use_fuids == B_FALSE &&
1337 1338 (vsecp || (vap->va_mask & AT_XVATTR) ||
1338 1339 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1339 1340 return (SET_ERROR(EINVAL));
1340 1341
1341 1342 ZFS_ENTER(zfsvfs);
1342 1343 ZFS_VERIFY_ZP(dzp);
1343 1344 os = zfsvfs->z_os;
1344 1345 zilog = zfsvfs->z_log;
1345 1346
1346 1347 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
1347 1348 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1348 1349 ZFS_EXIT(zfsvfs);
1349 1350 return (SET_ERROR(EILSEQ));
1350 1351 }
1351 1352
1352 1353 if (vap->va_mask & AT_XVATTR) {
1353 1354 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1354 1355 crgetuid(cr), cr, vap->va_type)) != 0) {
1355 1356 ZFS_EXIT(zfsvfs);
1356 1357 return (error);
1357 1358 }
1358 1359 }
1359 1360 top:
1360 1361 *vpp = NULL;
1361 1362
1362 1363 if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr))
1363 1364 vap->va_mode &= ~VSVTX;
1364 1365
1365 1366 if (*name == '\0') {
1366 1367 /*
1367 1368 * Null component name refers to the directory itself.
1368 1369 */
1369 1370 VN_HOLD(dvp);
1370 1371 zp = dzp;
1371 1372 dl = NULL;
1372 1373 error = 0;
1373 1374 } else {
1374 1375 /* possible VN_HOLD(zp) */
1375 1376 int zflg = 0;
1376 1377
1377 1378 if (flag & FIGNORECASE)
1378 1379 zflg |= ZCILOOK;
1379 1380
1380 1381 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1381 1382 NULL, NULL);
1382 1383 if (error) {
1383 1384 if (have_acl)
1384 1385 zfs_acl_ids_free(&acl_ids);
1385 1386 if (strcmp(name, "..") == 0)
1386 1387 error = SET_ERROR(EISDIR);
1387 1388 ZFS_EXIT(zfsvfs);
1388 1389 return (error);
1389 1390 }
1390 1391 }
1391 1392
1392 1393 if (zp == NULL) {
1393 1394 uint64_t txtype;
1394 1395
1395 1396 /*
1396 1397 * Create a new file object and update the directory
1397 1398 * to reference it.
1398 1399 */
1399 1400 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
1400 1401 if (have_acl)
1401 1402 zfs_acl_ids_free(&acl_ids);
1402 1403 goto out;
1403 1404 }
1404 1405
1405 1406 /*
1406 1407 * We only support the creation of regular files in
1407 1408 * extended attribute directories.
1408 1409 */
1409 1410
1410 1411 if ((dzp->z_pflags & ZFS_XATTR) &&
1411 1412 (vap->va_type != VREG)) {
1412 1413 if (have_acl)
1413 1414 zfs_acl_ids_free(&acl_ids);
1414 1415 error = SET_ERROR(EINVAL);
1415 1416 goto out;
1416 1417 }
1417 1418
1418 1419 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap,
1419 1420 cr, vsecp, &acl_ids)) != 0)
1420 1421 goto out;
1421 1422 have_acl = B_TRUE;
1422 1423
1423 1424 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1424 1425 zfs_acl_ids_free(&acl_ids);
1425 1426 error = SET_ERROR(EDQUOT);
1426 1427 goto out;
1427 1428 }
1428 1429
1429 1430 tx = dmu_tx_create(os);
1430 1431
1431 1432 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1432 1433 ZFS_SA_BASE_ATTR_SIZE);
1433 1434
1434 1435 fuid_dirtied = zfsvfs->z_fuid_dirty;
1435 1436 if (fuid_dirtied)
1436 1437 zfs_fuid_txhold(zfsvfs, tx);
1437 1438 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
1438 1439 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
1439 1440 if (!zfsvfs->z_use_sa &&
1440 1441 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1441 1442 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
1442 1443 0, acl_ids.z_aclp->z_acl_bytes);
1443 1444 }
1444 1445 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1445 1446 if (error) {
1446 1447 zfs_dirent_unlock(dl);
1447 1448 if (error == ERESTART) {
1448 1449 waited = B_TRUE;
1449 1450 dmu_tx_wait(tx);
1450 1451 dmu_tx_abort(tx);
1451 1452 goto top;
1452 1453 }
1453 1454 zfs_acl_ids_free(&acl_ids);
1454 1455 dmu_tx_abort(tx);
1455 1456 ZFS_EXIT(zfsvfs);
1456 1457 return (error);
1457 1458 }
1458 1459 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1459 1460
1460 1461 if (fuid_dirtied)
1461 1462 zfs_fuid_sync(zfsvfs, tx);
1462 1463
1463 1464 (void) zfs_link_create(dl, zp, tx, ZNEW);
1464 1465 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap);
1465 1466 if (flag & FIGNORECASE)
1466 1467 txtype |= TX_CI;
1467 1468 zfs_log_create(zilog, tx, txtype, dzp, zp, name,
1468 1469 vsecp, acl_ids.z_fuidp, vap);
1469 1470 zfs_acl_ids_free(&acl_ids);
1470 1471 dmu_tx_commit(tx);
1471 1472 } else {
1472 1473 int aflags = (flag & FAPPEND) ? V_APPEND : 0;
1473 1474
1474 1475 if (have_acl)
1475 1476 zfs_acl_ids_free(&acl_ids);
1476 1477 have_acl = B_FALSE;
1477 1478
1478 1479 /*
1479 1480 * A directory entry already exists for this name.
1480 1481 */
1481 1482 /*
1482 1483 * Can't truncate an existing file if in exclusive mode.
1483 1484 */
1484 1485 if (excl == EXCL) {
1485 1486 error = SET_ERROR(EEXIST);
1486 1487 goto out;
1487 1488 }
1488 1489 /*
1489 1490 * Can't open a directory for writing.
1490 1491 */
1491 1492 if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) {
1492 1493 error = SET_ERROR(EISDIR);
1493 1494 goto out;
1494 1495 }
1495 1496 /*
1496 1497 * Verify requested access to file.
1497 1498 */
1498 1499 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) {
1499 1500 goto out;
1500 1501 }
1501 1502
1502 1503 mutex_enter(&dzp->z_lock);
1503 1504 dzp->z_seq++;
1504 1505 mutex_exit(&dzp->z_lock);
1505 1506
1506 1507 /*
1507 1508 * Truncate regular files if requested.
1508 1509 */
1509 1510 if ((ZTOV(zp)->v_type == VREG) &&
1510 1511 (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) {
1511 1512 /* we can't hold any locks when calling zfs_freesp() */
1512 1513 zfs_dirent_unlock(dl);
1513 1514 dl = NULL;
1514 1515 error = zfs_freesp(zp, 0, 0, mode, TRUE);
1515 1516 if (error == 0) {
1516 1517 vnevent_create(ZTOV(zp), ct);
1517 1518 }
1518 1519 }
1519 1520 }
1520 1521 out:
1521 1522
1522 1523 if (dl)
1523 1524 zfs_dirent_unlock(dl);
1524 1525
1525 1526 if (error) {
1526 1527 if (zp)
1527 1528 VN_RELE(ZTOV(zp));
1528 1529 } else {
1529 1530 *vpp = ZTOV(zp);
1530 1531 error = specvp_check(vpp, cr);
1531 1532 }
1532 1533
1533 1534 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1534 1535 zil_commit(zilog, 0);
1535 1536
1536 1537 ZFS_EXIT(zfsvfs);
1537 1538 return (error);
1538 1539 }
1539 1540
1540 1541 /*
1541 1542 * Remove an entry from a directory.
1542 1543 *
1543 1544 * IN: dvp - vnode of directory to remove entry from.
1544 1545 * name - name of entry to remove.
1545 1546 * cr - credentials of caller.
1546 1547 * ct - caller context
1547 1548 * flags - case flags
1548 1549 *
1549 1550 * RETURN: 0 on success, error code on failure.
1550 1551 *
1551 1552 * Timestamps:
1552 1553 * dvp - ctime|mtime
1553 1554 * vp - ctime (if nlink > 0)
1554 1555 */
1555 1556
1556 1557 uint64_t null_xattr = 0;
1557 1558
1558 1559 /*ARGSUSED*/
1559 1560 static int
1560 1561 zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct,
1561 1562 int flags)
1562 1563 {
1563 1564 znode_t *zp, *dzp = VTOZ(dvp);
1564 1565 znode_t *xzp;
1565 1566 vnode_t *vp;
1566 1567 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1567 1568 zilog_t *zilog;
1568 1569 uint64_t acl_obj, xattr_obj;
1569 1570 uint64_t xattr_obj_unlinked = 0;
1570 1571 uint64_t obj = 0;
1571 1572 zfs_dirlock_t *dl;
1572 1573 dmu_tx_t *tx;
1573 1574 boolean_t may_delete_now, delete_now = FALSE;
1574 1575 boolean_t unlinked, toobig = FALSE;
1575 1576 uint64_t txtype;
1576 1577 pathname_t *realnmp = NULL;
1577 1578 pathname_t realnm;
1578 1579 int error;
1579 1580 int zflg = ZEXISTS;
1580 1581 boolean_t waited = B_FALSE;
1581 1582
1582 1583 ZFS_ENTER(zfsvfs);
1583 1584 ZFS_VERIFY_ZP(dzp);
1584 1585 zilog = zfsvfs->z_log;
1585 1586
1586 1587 if (flags & FIGNORECASE) {
1587 1588 zflg |= ZCILOOK;
1588 1589 pn_alloc(&realnm);
1589 1590 realnmp = &realnm;
1590 1591 }
1591 1592
1592 1593 top:
1593 1594 xattr_obj = 0;
1594 1595 xzp = NULL;
1595 1596 /*
1596 1597 * Attempt to lock directory; fail if entry doesn't exist.
1597 1598 */
1598 1599 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
1599 1600 NULL, realnmp)) {
1600 1601 if (realnmp)
1601 1602 pn_free(realnmp);
1602 1603 ZFS_EXIT(zfsvfs);
1603 1604 return (error);
1604 1605 }
1605 1606
1606 1607 vp = ZTOV(zp);
1607 1608
1608 1609 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
1609 1610 goto out;
1610 1611 }
1611 1612
1612 1613 /*
1613 1614 * Need to use rmdir for removing directories.
1614 1615 */
1615 1616 if (vp->v_type == VDIR) {
1616 1617 error = SET_ERROR(EPERM);
1617 1618 goto out;
1618 1619 }
1619 1620
1620 1621 vnevent_remove(vp, dvp, name, ct);
1621 1622
1622 1623 if (realnmp)
1623 1624 dnlc_remove(dvp, realnmp->pn_buf);
1624 1625 else
1625 1626 dnlc_remove(dvp, name);
1626 1627
1627 1628 mutex_enter(&vp->v_lock);
1628 1629 may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp);
1629 1630 mutex_exit(&vp->v_lock);
1630 1631
1631 1632 /*
1632 1633 * We may delete the znode now, or we may put it in the unlinked set;
1633 1634 * it depends on whether we're the last link, and on whether there are
1634 1635 * other holds on the vnode. So we dmu_tx_hold() the right things to
1635 1636 * allow for either case.
1636 1637 */
1637 1638 obj = zp->z_id;
1638 1639 tx = dmu_tx_create(zfsvfs->z_os);
1639 1640 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
1640 1641 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
1641 1642 zfs_sa_upgrade_txholds(tx, zp);
1642 1643 zfs_sa_upgrade_txholds(tx, dzp);
1643 1644 if (may_delete_now) {
1644 1645 toobig =
1645 1646 zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT;
1646 1647 /* if the file is too big, only hold_free a token amount */
1647 1648 dmu_tx_hold_free(tx, zp->z_id, 0,
1648 1649 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END));
1649 1650 }
1650 1651
1651 1652 /* are there any extended attributes? */
1652 1653 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1653 1654 &xattr_obj, sizeof (xattr_obj));
1654 1655 if (error == 0 && xattr_obj) {
1655 1656 error = zfs_zget(zfsvfs, xattr_obj, &xzp);
1656 1657 ASSERT0(error);
1657 1658 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
1658 1659 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE);
1659 1660 }
1660 1661
1661 1662 mutex_enter(&zp->z_lock);
1662 1663 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now)
1663 1664 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END);
1664 1665 mutex_exit(&zp->z_lock);
1665 1666
1666 1667 /* charge as an update -- would be nice not to charge at all */
1667 1668 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
1668 1669
1669 1670 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1670 1671 if (error) {
1671 1672 zfs_dirent_unlock(dl);
1672 1673 VN_RELE(vp);
1673 1674 if (xzp)
1674 1675 VN_RELE(ZTOV(xzp));
1675 1676 if (error == ERESTART) {
1676 1677 waited = B_TRUE;
1677 1678 dmu_tx_wait(tx);
1678 1679 dmu_tx_abort(tx);
1679 1680 goto top;
1680 1681 }
1681 1682 if (realnmp)
1682 1683 pn_free(realnmp);
1683 1684 dmu_tx_abort(tx);
1684 1685 ZFS_EXIT(zfsvfs);
1685 1686 return (error);
1686 1687 }
1687 1688
1688 1689 /*
1689 1690 * Remove the directory entry.
1690 1691 */
1691 1692 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked);
1692 1693
1693 1694 if (error) {
1694 1695 dmu_tx_commit(tx);
1695 1696 goto out;
1696 1697 }
1697 1698
1698 1699 if (unlinked) {
1699 1700
1700 1701 /*
1701 1702 * Hold z_lock so that we can make sure that the ACL obj
1702 1703 * hasn't changed. Could have been deleted due to
1703 1704 * zfs_sa_upgrade().
1704 1705 */
1705 1706 mutex_enter(&zp->z_lock);
1706 1707 mutex_enter(&vp->v_lock);
1707 1708 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
1708 1709 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked));
1709 1710 delete_now = may_delete_now && !toobig &&
1710 1711 vp->v_count == 1 && !vn_has_cached_data(vp) &&
1711 1712 xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) ==
1712 1713 acl_obj;
1713 1714 mutex_exit(&vp->v_lock);
1714 1715 }
1715 1716
1716 1717 if (delete_now) {
1717 1718 if (xattr_obj_unlinked) {
1718 1719 ASSERT3U(xzp->z_links, ==, 2);
1719 1720 mutex_enter(&xzp->z_lock);
1720 1721 xzp->z_unlinked = 1;
1721 1722 xzp->z_links = 0;
1722 1723 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs),
1723 1724 &xzp->z_links, sizeof (xzp->z_links), tx);
1724 1725 ASSERT3U(error, ==, 0);
1725 1726 mutex_exit(&xzp->z_lock);
1726 1727 zfs_unlinked_add(xzp, tx);
1727 1728
1728 1729 if (zp->z_is_sa)
1729 1730 error = sa_remove(zp->z_sa_hdl,
1730 1731 SA_ZPL_XATTR(zfsvfs), tx);
1731 1732 else
1732 1733 error = sa_update(zp->z_sa_hdl,
1733 1734 SA_ZPL_XATTR(zfsvfs), &null_xattr,
1734 1735 sizeof (uint64_t), tx);
1735 1736 ASSERT0(error);
1736 1737 }
1737 1738 mutex_enter(&vp->v_lock);
1738 1739 vp->v_count--;
1739 1740 ASSERT0(vp->v_count);
1740 1741 mutex_exit(&vp->v_lock);
1741 1742 mutex_exit(&zp->z_lock);
1742 1743 zfs_znode_delete(zp, tx);
1743 1744 } else if (unlinked) {
1744 1745 mutex_exit(&zp->z_lock);
1745 1746 zfs_unlinked_add(zp, tx);
1746 1747 }
1747 1748
1748 1749 txtype = TX_REMOVE;
1749 1750 if (flags & FIGNORECASE)
1750 1751 txtype |= TX_CI;
1751 1752 zfs_log_remove(zilog, tx, txtype, dzp, name, obj);
1752 1753
1753 1754 dmu_tx_commit(tx);
1754 1755 out:
1755 1756 if (realnmp)
1756 1757 pn_free(realnmp);
1757 1758
1758 1759 zfs_dirent_unlock(dl);
1759 1760
1760 1761 if (!delete_now)
1761 1762 VN_RELE(vp);
1762 1763 if (xzp)
1763 1764 VN_RELE(ZTOV(xzp));
1764 1765
1765 1766 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1766 1767 zil_commit(zilog, 0);
1767 1768
1768 1769 ZFS_EXIT(zfsvfs);
1769 1770 return (error);
1770 1771 }
1771 1772
1772 1773 /*
1773 1774 * Create a new directory and insert it into dvp using the name
1774 1775 * provided. Return a pointer to the inserted directory.
1775 1776 *
1776 1777 * IN: dvp - vnode of directory to add subdir to.
1777 1778 * dirname - name of new directory.
1778 1779 * vap - attributes of new directory.
1779 1780 * cr - credentials of caller.
1780 1781 * ct - caller context
1781 1782 * flags - case flags
1782 1783 * vsecp - ACL to be set
1783 1784 *
1784 1785 * OUT: vpp - vnode of created directory.
1785 1786 *
1786 1787 * RETURN: 0 on success, error code on failure.
1787 1788 *
1788 1789 * Timestamps:
1789 1790 * dvp - ctime|mtime updated
1790 1791 * vp - ctime|mtime|atime updated
1791 1792 */
1792 1793 /*ARGSUSED*/
1793 1794 static int
1794 1795 zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr,
1795 1796 caller_context_t *ct, int flags, vsecattr_t *vsecp)
1796 1797 {
1797 1798 znode_t *zp, *dzp = VTOZ(dvp);
1798 1799 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1799 1800 zilog_t *zilog;
1800 1801 zfs_dirlock_t *dl;
1801 1802 uint64_t txtype;
1802 1803 dmu_tx_t *tx;
1803 1804 int error;
1804 1805 int zf = ZNEW;
1805 1806 ksid_t *ksid;
1806 1807 uid_t uid;
1807 1808 gid_t gid = crgetgid(cr);
1808 1809 zfs_acl_ids_t acl_ids;
1809 1810 boolean_t fuid_dirtied;
1810 1811 boolean_t waited = B_FALSE;
1811 1812
1812 1813 ASSERT(vap->va_type == VDIR);
1813 1814
1814 1815 /*
1815 1816 * If we have an ephemeral id, ACL, or XVATTR then
1816 1817 * make sure file system is at proper version
1817 1818 */
1818 1819
1819 1820 ksid = crgetsid(cr, KSID_OWNER);
1820 1821 if (ksid)
1821 1822 uid = ksid_getid(ksid);
1822 1823 else
1823 1824 uid = crgetuid(cr);
1824 1825 if (zfsvfs->z_use_fuids == B_FALSE &&
1825 1826 (vsecp || (vap->va_mask & AT_XVATTR) ||
1826 1827 IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid)))
1827 1828 return (SET_ERROR(EINVAL));
1828 1829
1829 1830 ZFS_ENTER(zfsvfs);
1830 1831 ZFS_VERIFY_ZP(dzp);
1831 1832 zilog = zfsvfs->z_log;
1832 1833
1833 1834 if (dzp->z_pflags & ZFS_XATTR) {
1834 1835 ZFS_EXIT(zfsvfs);
1835 1836 return (SET_ERROR(EINVAL));
1836 1837 }
1837 1838
1838 1839 if (zfsvfs->z_utf8 && u8_validate(dirname,
1839 1840 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
1840 1841 ZFS_EXIT(zfsvfs);
1841 1842 return (SET_ERROR(EILSEQ));
1842 1843 }
1843 1844 if (flags & FIGNORECASE)
1844 1845 zf |= ZCILOOK;
1845 1846
1846 1847 if (vap->va_mask & AT_XVATTR) {
1847 1848 if ((error = secpolicy_xvattr((xvattr_t *)vap,
1848 1849 crgetuid(cr), cr, vap->va_type)) != 0) {
1849 1850 ZFS_EXIT(zfsvfs);
1850 1851 return (error);
1851 1852 }
1852 1853 }
1853 1854
1854 1855 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr,
1855 1856 vsecp, &acl_ids)) != 0) {
1856 1857 ZFS_EXIT(zfsvfs);
1857 1858 return (error);
1858 1859 }
1859 1860 /*
1860 1861 * First make sure the new directory doesn't exist.
1861 1862 *
1862 1863 * Existence is checked first to make sure we don't return
1863 1864 * EACCES instead of EEXIST which can cause some applications
1864 1865 * to fail.
1865 1866 */
1866 1867 top:
1867 1868 *vpp = NULL;
1868 1869
1869 1870 if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf,
1870 1871 NULL, NULL)) {
1871 1872 zfs_acl_ids_free(&acl_ids);
1872 1873 ZFS_EXIT(zfsvfs);
1873 1874 return (error);
1874 1875 }
1875 1876
1876 1877 if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) {
1877 1878 zfs_acl_ids_free(&acl_ids);
1878 1879 zfs_dirent_unlock(dl);
1879 1880 ZFS_EXIT(zfsvfs);
1880 1881 return (error);
1881 1882 }
1882 1883
1883 1884 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
1884 1885 zfs_acl_ids_free(&acl_ids);
1885 1886 zfs_dirent_unlock(dl);
1886 1887 ZFS_EXIT(zfsvfs);
1887 1888 return (SET_ERROR(EDQUOT));
1888 1889 }
1889 1890
1890 1891 /*
1891 1892 * Add a new entry to the directory.
1892 1893 */
1893 1894 tx = dmu_tx_create(zfsvfs->z_os);
1894 1895 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname);
1895 1896 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL);
1896 1897 fuid_dirtied = zfsvfs->z_fuid_dirty;
1897 1898 if (fuid_dirtied)
1898 1899 zfs_fuid_txhold(zfsvfs, tx);
1899 1900 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
1900 1901 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
1901 1902 acl_ids.z_aclp->z_acl_bytes);
1902 1903 }
1903 1904
1904 1905 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
1905 1906 ZFS_SA_BASE_ATTR_SIZE);
1906 1907
1907 1908 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
1908 1909 if (error) {
1909 1910 zfs_dirent_unlock(dl);
1910 1911 if (error == ERESTART) {
1911 1912 waited = B_TRUE;
1912 1913 dmu_tx_wait(tx);
1913 1914 dmu_tx_abort(tx);
1914 1915 goto top;
1915 1916 }
1916 1917 zfs_acl_ids_free(&acl_ids);
1917 1918 dmu_tx_abort(tx);
1918 1919 ZFS_EXIT(zfsvfs);
1919 1920 return (error);
1920 1921 }
1921 1922
1922 1923 /*
1923 1924 * Create new node.
1924 1925 */
1925 1926 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
1926 1927
1927 1928 if (fuid_dirtied)
1928 1929 zfs_fuid_sync(zfsvfs, tx);
1929 1930
1930 1931 /*
1931 1932 * Now put new name in parent dir.
1932 1933 */
1933 1934 (void) zfs_link_create(dl, zp, tx, ZNEW);
1934 1935
1935 1936 *vpp = ZTOV(zp);
1936 1937
1937 1938 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap);
1938 1939 if (flags & FIGNORECASE)
1939 1940 txtype |= TX_CI;
1940 1941 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp,
1941 1942 acl_ids.z_fuidp, vap);
1942 1943
1943 1944 zfs_acl_ids_free(&acl_ids);
1944 1945
1945 1946 dmu_tx_commit(tx);
1946 1947
1947 1948 zfs_dirent_unlock(dl);
1948 1949
1949 1950 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
1950 1951 zil_commit(zilog, 0);
1951 1952
1952 1953 ZFS_EXIT(zfsvfs);
1953 1954 return (0);
1954 1955 }
1955 1956
1956 1957 /*
1957 1958 * Remove a directory subdir entry. If the current working
1958 1959 * directory is the same as the subdir to be removed, the
1959 1960 * remove will fail.
1960 1961 *
1961 1962 * IN: dvp - vnode of directory to remove from.
1962 1963 * name - name of directory to be removed.
1963 1964 * cwd - vnode of current working directory.
1964 1965 * cr - credentials of caller.
1965 1966 * ct - caller context
1966 1967 * flags - case flags
1967 1968 *
1968 1969 * RETURN: 0 on success, error code on failure.
1969 1970 *
1970 1971 * Timestamps:
1971 1972 * dvp - ctime|mtime updated
1972 1973 */
1973 1974 /*ARGSUSED*/
1974 1975 static int
1975 1976 zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
1976 1977 caller_context_t *ct, int flags)
1977 1978 {
1978 1979 znode_t *dzp = VTOZ(dvp);
1979 1980 znode_t *zp;
1980 1981 vnode_t *vp;
1981 1982 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
1982 1983 zilog_t *zilog;
1983 1984 zfs_dirlock_t *dl;
1984 1985 dmu_tx_t *tx;
1985 1986 int error;
1986 1987 int zflg = ZEXISTS;
1987 1988 boolean_t waited = B_FALSE;
1988 1989
1989 1990 ZFS_ENTER(zfsvfs);
1990 1991 ZFS_VERIFY_ZP(dzp);
1991 1992 zilog = zfsvfs->z_log;
1992 1993
1993 1994 if (flags & FIGNORECASE)
1994 1995 zflg |= ZCILOOK;
1995 1996 top:
1996 1997 zp = NULL;
1997 1998
1998 1999 /*
1999 2000 * Attempt to lock directory; fail if entry doesn't exist.
2000 2001 */
2001 2002 if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg,
2002 2003 NULL, NULL)) {
2003 2004 ZFS_EXIT(zfsvfs);
2004 2005 return (error);
2005 2006 }
2006 2007
2007 2008 vp = ZTOV(zp);
2008 2009
2009 2010 if (error = zfs_zaccess_delete(dzp, zp, cr)) {
2010 2011 goto out;
2011 2012 }
2012 2013
2013 2014 if (vp->v_type != VDIR) {
2014 2015 error = SET_ERROR(ENOTDIR);
2015 2016 goto out;
2016 2017 }
2017 2018
2018 2019 if (vp == cwd) {
2019 2020 error = SET_ERROR(EINVAL);
2020 2021 goto out;
2021 2022 }
2022 2023
2023 2024 vnevent_rmdir(vp, dvp, name, ct);
2024 2025
2025 2026 /*
2026 2027 * Grab a lock on the directory to make sure that noone is
2027 2028 * trying to add (or lookup) entries while we are removing it.
2028 2029 */
2029 2030 rw_enter(&zp->z_name_lock, RW_WRITER);
2030 2031
2031 2032 /*
2032 2033 * Grab a lock on the parent pointer to make sure we play well
2033 2034 * with the treewalk and directory rename code.
2034 2035 */
2035 2036 rw_enter(&zp->z_parent_lock, RW_WRITER);
2036 2037
2037 2038 tx = dmu_tx_create(zfsvfs->z_os);
2038 2039 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name);
2039 2040 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
2040 2041 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
2041 2042 zfs_sa_upgrade_txholds(tx, zp);
2042 2043 zfs_sa_upgrade_txholds(tx, dzp);
2043 2044 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
2044 2045 if (error) {
2045 2046 rw_exit(&zp->z_parent_lock);
2046 2047 rw_exit(&zp->z_name_lock);
2047 2048 zfs_dirent_unlock(dl);
2048 2049 VN_RELE(vp);
2049 2050 if (error == ERESTART) {
2050 2051 waited = B_TRUE;
2051 2052 dmu_tx_wait(tx);
2052 2053 dmu_tx_abort(tx);
2053 2054 goto top;
2054 2055 }
2055 2056 dmu_tx_abort(tx);
2056 2057 ZFS_EXIT(zfsvfs);
2057 2058 return (error);
2058 2059 }
2059 2060
2060 2061 error = zfs_link_destroy(dl, zp, tx, zflg, NULL);
2061 2062
2062 2063 if (error == 0) {
2063 2064 uint64_t txtype = TX_RMDIR;
2064 2065 if (flags & FIGNORECASE)
2065 2066 txtype |= TX_CI;
2066 2067 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT);
2067 2068 }
2068 2069
2069 2070 dmu_tx_commit(tx);
2070 2071
2071 2072 rw_exit(&zp->z_parent_lock);
2072 2073 rw_exit(&zp->z_name_lock);
2073 2074 out:
2074 2075 zfs_dirent_unlock(dl);
2075 2076
2076 2077 VN_RELE(vp);
2077 2078
2078 2079 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
2079 2080 zil_commit(zilog, 0);
2080 2081
2081 2082 ZFS_EXIT(zfsvfs);
2082 2083 return (error);
2083 2084 }
2084 2085
2085 2086 /*
2086 2087 * Read as many directory entries as will fit into the provided
2087 2088 * buffer from the given directory cursor position (specified in
2088 2089 * the uio structure).
2089 2090 *
2090 2091 * IN: vp - vnode of directory to read.
2091 2092 * uio - structure supplying read location, range info,
2092 2093 * and return buffer.
2093 2094 * cr - credentials of caller.
2094 2095 * ct - caller context
2095 2096 * flags - case flags
2096 2097 *
2097 2098 * OUT: uio - updated offset and range, buffer filled.
2098 2099 * eofp - set to true if end-of-file detected.
2099 2100 *
2100 2101 * RETURN: 0 on success, error code on failure.
2101 2102 *
2102 2103 * Timestamps:
2103 2104 * vp - atime updated
2104 2105 *
2105 2106 * Note that the low 4 bits of the cookie returned by zap is always zero.
2106 2107 * This allows us to use the low range for "special" directory entries:
2107 2108 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem,
2108 2109 * we use the offset 2 for the '.zfs' directory.
2109 2110 */
2110 2111 /* ARGSUSED */
2111 2112 static int
2112 2113 zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp,
2113 2114 caller_context_t *ct, int flags)
2114 2115 {
2115 2116 znode_t *zp = VTOZ(vp);
2116 2117 iovec_t *iovp;
2117 2118 edirent_t *eodp;
2118 2119 dirent64_t *odp;
2119 2120 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2120 2121 objset_t *os;
2121 2122 caddr_t outbuf;
2122 2123 size_t bufsize;
2123 2124 zap_cursor_t zc;
2124 2125 zap_attribute_t zap;
2125 2126 uint_t bytes_wanted;
2126 2127 uint64_t offset; /* must be unsigned; checks for < 1 */
2127 2128 uint64_t parent;
2128 2129 int local_eof;
2129 2130 int outcount;
2130 2131 int error;
2131 2132 uint8_t prefetch;
2132 2133 boolean_t check_sysattrs;
2133 2134
2134 2135 ZFS_ENTER(zfsvfs);
2135 2136 ZFS_VERIFY_ZP(zp);
2136 2137
2137 2138 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
2138 2139 &parent, sizeof (parent))) != 0) {
2139 2140 ZFS_EXIT(zfsvfs);
2140 2141 return (error);
2141 2142 }
2142 2143
2143 2144 /*
2144 2145 * If we are not given an eof variable,
2145 2146 * use a local one.
2146 2147 */
2147 2148 if (eofp == NULL)
2148 2149 eofp = &local_eof;
2149 2150
2150 2151 /*
2151 2152 * Check for valid iov_len.
2152 2153 */
2153 2154 if (uio->uio_iov->iov_len <= 0) {
2154 2155 ZFS_EXIT(zfsvfs);
2155 2156 return (SET_ERROR(EINVAL));
2156 2157 }
2157 2158
2158 2159 /*
2159 2160 * Quit if directory has been removed (posix)
2160 2161 */
2161 2162 if ((*eofp = zp->z_unlinked) != 0) {
2162 2163 ZFS_EXIT(zfsvfs);
2163 2164 return (0);
2164 2165 }
2165 2166
2166 2167 error = 0;
2167 2168 os = zfsvfs->z_os;
2168 2169 offset = uio->uio_loffset;
2169 2170 prefetch = zp->z_zn_prefetch;
2170 2171
2171 2172 /*
2172 2173 * Initialize the iterator cursor.
2173 2174 */
2174 2175 if (offset <= 3) {
2175 2176 /*
2176 2177 * Start iteration from the beginning of the directory.
2177 2178 */
2178 2179 zap_cursor_init(&zc, os, zp->z_id);
2179 2180 } else {
2180 2181 /*
2181 2182 * The offset is a serialized cursor.
2182 2183 */
2183 2184 zap_cursor_init_serialized(&zc, os, zp->z_id, offset);
2184 2185 }
2185 2186
2186 2187 /*
2187 2188 * Get space to change directory entries into fs independent format.
2188 2189 */
2189 2190 iovp = uio->uio_iov;
2190 2191 bytes_wanted = iovp->iov_len;
2191 2192 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) {
2192 2193 bufsize = bytes_wanted;
2193 2194 outbuf = kmem_alloc(bufsize, KM_SLEEP);
2194 2195 odp = (struct dirent64 *)outbuf;
2195 2196 } else {
2196 2197 bufsize = bytes_wanted;
2197 2198 outbuf = NULL;
2198 2199 odp = (struct dirent64 *)iovp->iov_base;
2199 2200 }
2200 2201 eodp = (struct edirent *)odp;
2201 2202
2202 2203 /*
2203 2204 * If this VFS supports the system attribute view interface; and
2204 2205 * we're looking at an extended attribute directory; and we care
2205 2206 * about normalization conflicts on this vfs; then we must check
2206 2207 * for normalization conflicts with the sysattr name space.
2207 2208 */
2208 2209 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
2209 2210 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm &&
2210 2211 (flags & V_RDDIR_ENTFLAGS);
2211 2212
2212 2213 /*
2213 2214 * Transform to file-system independent format
2214 2215 */
2215 2216 outcount = 0;
2216 2217 while (outcount < bytes_wanted) {
2217 2218 ino64_t objnum;
2218 2219 ushort_t reclen;
2219 2220 off64_t *next = NULL;
2220 2221
2221 2222 /*
2222 2223 * Special case `.', `..', and `.zfs'.
2223 2224 */
2224 2225 if (offset == 0) {
2225 2226 (void) strcpy(zap.za_name, ".");
2226 2227 zap.za_normalization_conflict = 0;
2227 2228 objnum = zp->z_id;
2228 2229 } else if (offset == 1) {
2229 2230 (void) strcpy(zap.za_name, "..");
2230 2231 zap.za_normalization_conflict = 0;
2231 2232 objnum = parent;
2232 2233 } else if (offset == 2 && zfs_show_ctldir(zp)) {
2233 2234 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME);
2234 2235 zap.za_normalization_conflict = 0;
2235 2236 objnum = ZFSCTL_INO_ROOT;
2236 2237 } else {
2237 2238 /*
2238 2239 * Grab next entry.
2239 2240 */
2240 2241 if (error = zap_cursor_retrieve(&zc, &zap)) {
2241 2242 if ((*eofp = (error == ENOENT)) != 0)
2242 2243 break;
2243 2244 else
2244 2245 goto update;
2245 2246 }
2246 2247
2247 2248 if (zap.za_integer_length != 8 ||
2248 2249 zap.za_num_integers != 1) {
2249 2250 cmn_err(CE_WARN, "zap_readdir: bad directory "
2250 2251 "entry, obj = %lld, offset = %lld\n",
2251 2252 (u_longlong_t)zp->z_id,
2252 2253 (u_longlong_t)offset);
2253 2254 error = SET_ERROR(ENXIO);
2254 2255 goto update;
2255 2256 }
2256 2257
2257 2258 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer);
2258 2259 /*
2259 2260 * MacOS X can extract the object type here such as:
2260 2261 * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer);
2261 2262 */
2262 2263
2263 2264 if (check_sysattrs && !zap.za_normalization_conflict) {
2264 2265 zap.za_normalization_conflict =
2265 2266 xattr_sysattr_casechk(zap.za_name);
2266 2267 }
2267 2268 }
2268 2269
2269 2270 if (flags & V_RDDIR_ACCFILTER) {
2270 2271 /*
2271 2272 * If we have no access at all, don't include
2272 2273 * this entry in the returned information
2273 2274 */
2274 2275 znode_t *ezp;
2275 2276 if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0)
2276 2277 goto skip_entry;
2277 2278 if (!zfs_has_access(ezp, cr)) {
2278 2279 VN_RELE(ZTOV(ezp));
2279 2280 goto skip_entry;
2280 2281 }
2281 2282 VN_RELE(ZTOV(ezp));
2282 2283 }
2283 2284
2284 2285 if (flags & V_RDDIR_ENTFLAGS)
2285 2286 reclen = EDIRENT_RECLEN(strlen(zap.za_name));
2286 2287 else
2287 2288 reclen = DIRENT64_RECLEN(strlen(zap.za_name));
2288 2289
2289 2290 /*
2290 2291 * Will this entry fit in the buffer?
2291 2292 */
2292 2293 if (outcount + reclen > bufsize) {
2293 2294 /*
2294 2295 * Did we manage to fit anything in the buffer?
2295 2296 */
2296 2297 if (!outcount) {
2297 2298 error = SET_ERROR(EINVAL);
2298 2299 goto update;
2299 2300 }
2300 2301 break;
2301 2302 }
2302 2303 if (flags & V_RDDIR_ENTFLAGS) {
2303 2304 /*
2304 2305 * Add extended flag entry:
2305 2306 */
2306 2307 eodp->ed_ino = objnum;
2307 2308 eodp->ed_reclen = reclen;
2308 2309 /* NOTE: ed_off is the offset for the *next* entry */
2309 2310 next = &(eodp->ed_off);
2310 2311 eodp->ed_eflags = zap.za_normalization_conflict ?
2311 2312 ED_CASE_CONFLICT : 0;
2312 2313 (void) strncpy(eodp->ed_name, zap.za_name,
2313 2314 EDIRENT_NAMELEN(reclen));
2314 2315 eodp = (edirent_t *)((intptr_t)eodp + reclen);
2315 2316 } else {
2316 2317 /*
2317 2318 * Add normal entry:
2318 2319 */
2319 2320 odp->d_ino = objnum;
2320 2321 odp->d_reclen = reclen;
2321 2322 /* NOTE: d_off is the offset for the *next* entry */
2322 2323 next = &(odp->d_off);
2323 2324 (void) strncpy(odp->d_name, zap.za_name,
2324 2325 DIRENT64_NAMELEN(reclen));
2325 2326 odp = (dirent64_t *)((intptr_t)odp + reclen);
2326 2327 }
2327 2328 outcount += reclen;
2328 2329
2329 2330 ASSERT(outcount <= bufsize);
2330 2331
2331 2332 /* Prefetch znode */
2332 2333 if (prefetch)
2333 2334 dmu_prefetch(os, objnum, 0, 0);
2334 2335
2335 2336 skip_entry:
2336 2337 /*
2337 2338 * Move to the next entry, fill in the previous offset.
2338 2339 */
2339 2340 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) {
2340 2341 zap_cursor_advance(&zc);
2341 2342 offset = zap_cursor_serialize(&zc);
2342 2343 } else {
2343 2344 offset += 1;
2344 2345 }
2345 2346 if (next)
2346 2347 *next = offset;
2347 2348 }
2348 2349 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */
2349 2350
2350 2351 if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) {
2351 2352 iovp->iov_base += outcount;
2352 2353 iovp->iov_len -= outcount;
2353 2354 uio->uio_resid -= outcount;
2354 2355 } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) {
2355 2356 /*
2356 2357 * Reset the pointer.
2357 2358 */
2358 2359 offset = uio->uio_loffset;
2359 2360 }
2360 2361
2361 2362 update:
2362 2363 zap_cursor_fini(&zc);
2363 2364 if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1)
2364 2365 kmem_free(outbuf, bufsize);
2365 2366
2366 2367 if (error == ENOENT)
2367 2368 error = 0;
2368 2369
2369 2370 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
2370 2371
2371 2372 uio->uio_loffset = offset;
2372 2373 ZFS_EXIT(zfsvfs);
2373 2374 return (error);
2374 2375 }
2375 2376
2376 2377 ulong_t zfs_fsync_sync_cnt = 4;
2377 2378
2378 2379 static int
2379 2380 zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct)
2380 2381 {
2381 2382 znode_t *zp = VTOZ(vp);
2382 2383 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2383 2384
2384 2385 /*
2385 2386 * Regardless of whether this is required for standards conformance,
2386 2387 * this is the logical behavior when fsync() is called on a file with
2387 2388 * dirty pages. We use B_ASYNC since the ZIL transactions are already
2388 2389 * going to be pushed out as part of the zil_commit().
2389 2390 */
2390 2391 if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
2391 2392 (vp->v_type == VREG) && !(IS_SWAPVP(vp)))
2392 2393 (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct);
2393 2394
2394 2395 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt);
2395 2396
2396 2397 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) {
2397 2398 ZFS_ENTER(zfsvfs);
2398 2399 ZFS_VERIFY_ZP(zp);
2399 2400 zil_commit(zfsvfs->z_log, zp->z_id);
2400 2401 ZFS_EXIT(zfsvfs);
2401 2402 }
2402 2403 return (0);
2403 2404 }
2404 2405
2405 2406
2406 2407 /*
2407 2408 * Get the requested file attributes and place them in the provided
2408 2409 * vattr structure.
2409 2410 *
2410 2411 * IN: vp - vnode of file.
2411 2412 * vap - va_mask identifies requested attributes.
2412 2413 * If AT_XVATTR set, then optional attrs are requested
2413 2414 * flags - ATTR_NOACLCHECK (CIFS server context)
2414 2415 * cr - credentials of caller.
2415 2416 * ct - caller context
2416 2417 *
2417 2418 * OUT: vap - attribute values.
2418 2419 *
2419 2420 * RETURN: 0 (always succeeds).
2420 2421 */
2421 2422 /* ARGSUSED */
2422 2423 static int
2423 2424 zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2424 2425 caller_context_t *ct)
2425 2426 {
2426 2427 znode_t *zp = VTOZ(vp);
2427 2428 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2428 2429 int error = 0;
2429 2430 uint64_t links;
2430 2431 uint64_t mtime[2], ctime[2];
2431 2432 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2432 2433 xoptattr_t *xoap = NULL;
2433 2434 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2434 2435 sa_bulk_attr_t bulk[2];
2435 2436 int count = 0;
2436 2437
2437 2438 ZFS_ENTER(zfsvfs);
2438 2439 ZFS_VERIFY_ZP(zp);
2439 2440
2440 2441 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid);
2441 2442
2442 2443 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16);
2443 2444 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16);
2444 2445
2445 2446 if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) {
2446 2447 ZFS_EXIT(zfsvfs);
2447 2448 return (error);
2448 2449 }
2449 2450
2450 2451 /*
2451 2452 * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES.
2452 2453 * Also, if we are the owner don't bother, since owner should
2453 2454 * always be allowed to read basic attributes of file.
2454 2455 */
2455 2456 if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) &&
2456 2457 (vap->va_uid != crgetuid(cr))) {
2457 2458 if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0,
2458 2459 skipaclchk, cr)) {
2459 2460 ZFS_EXIT(zfsvfs);
2460 2461 return (error);
2461 2462 }
2462 2463 }
2463 2464
2464 2465 /*
2465 2466 * Return all attributes. It's cheaper to provide the answer
2466 2467 * than to determine whether we were asked the question.
2467 2468 */
2468 2469
2469 2470 mutex_enter(&zp->z_lock);
2470 2471 vap->va_type = vp->v_type;
2471 2472 vap->va_mode = zp->z_mode & MODEMASK;
2472 2473 vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev;
2473 2474 vap->va_nodeid = zp->z_id;
2474 2475 if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp))
2475 2476 links = zp->z_links + 1;
2476 2477 else
2477 2478 links = zp->z_links;
2478 2479 vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */
2479 2480 vap->va_size = zp->z_size;
2480 2481 vap->va_rdev = vp->v_rdev;
2481 2482 vap->va_seq = zp->z_seq;
2482 2483
2483 2484 /*
2484 2485 * Add in any requested optional attributes and the create time.
2485 2486 * Also set the corresponding bits in the returned attribute bitmap.
2486 2487 */
2487 2488 if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) {
2488 2489 if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) {
2489 2490 xoap->xoa_archive =
2490 2491 ((zp->z_pflags & ZFS_ARCHIVE) != 0);
2491 2492 XVA_SET_RTN(xvap, XAT_ARCHIVE);
2492 2493 }
2493 2494
2494 2495 if (XVA_ISSET_REQ(xvap, XAT_READONLY)) {
2495 2496 xoap->xoa_readonly =
2496 2497 ((zp->z_pflags & ZFS_READONLY) != 0);
2497 2498 XVA_SET_RTN(xvap, XAT_READONLY);
2498 2499 }
2499 2500
2500 2501 if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) {
2501 2502 xoap->xoa_system =
2502 2503 ((zp->z_pflags & ZFS_SYSTEM) != 0);
2503 2504 XVA_SET_RTN(xvap, XAT_SYSTEM);
2504 2505 }
2505 2506
2506 2507 if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) {
2507 2508 xoap->xoa_hidden =
2508 2509 ((zp->z_pflags & ZFS_HIDDEN) != 0);
2509 2510 XVA_SET_RTN(xvap, XAT_HIDDEN);
2510 2511 }
2511 2512
2512 2513 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2513 2514 xoap->xoa_nounlink =
2514 2515 ((zp->z_pflags & ZFS_NOUNLINK) != 0);
2515 2516 XVA_SET_RTN(xvap, XAT_NOUNLINK);
2516 2517 }
2517 2518
2518 2519 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2519 2520 xoap->xoa_immutable =
2520 2521 ((zp->z_pflags & ZFS_IMMUTABLE) != 0);
2521 2522 XVA_SET_RTN(xvap, XAT_IMMUTABLE);
2522 2523 }
2523 2524
2524 2525 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2525 2526 xoap->xoa_appendonly =
2526 2527 ((zp->z_pflags & ZFS_APPENDONLY) != 0);
2527 2528 XVA_SET_RTN(xvap, XAT_APPENDONLY);
2528 2529 }
2529 2530
2530 2531 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2531 2532 xoap->xoa_nodump =
2532 2533 ((zp->z_pflags & ZFS_NODUMP) != 0);
2533 2534 XVA_SET_RTN(xvap, XAT_NODUMP);
2534 2535 }
2535 2536
2536 2537 if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) {
2537 2538 xoap->xoa_opaque =
2538 2539 ((zp->z_pflags & ZFS_OPAQUE) != 0);
2539 2540 XVA_SET_RTN(xvap, XAT_OPAQUE);
2540 2541 }
2541 2542
2542 2543 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2543 2544 xoap->xoa_av_quarantined =
2544 2545 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0);
2545 2546 XVA_SET_RTN(xvap, XAT_AV_QUARANTINED);
2546 2547 }
2547 2548
2548 2549 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2549 2550 xoap->xoa_av_modified =
2550 2551 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0);
2551 2552 XVA_SET_RTN(xvap, XAT_AV_MODIFIED);
2552 2553 }
2553 2554
2554 2555 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) &&
2555 2556 vp->v_type == VREG) {
2556 2557 zfs_sa_get_scanstamp(zp, xvap);
2557 2558 }
2558 2559
2559 2560 if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
2560 2561 uint64_t times[2];
2561 2562
2562 2563 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs),
2563 2564 times, sizeof (times));
2564 2565 ZFS_TIME_DECODE(&xoap->xoa_createtime, times);
2565 2566 XVA_SET_RTN(xvap, XAT_CREATETIME);
2566 2567 }
2567 2568
2568 2569 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2569 2570 xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0);
2570 2571 XVA_SET_RTN(xvap, XAT_REPARSE);
2571 2572 }
2572 2573 if (XVA_ISSET_REQ(xvap, XAT_GEN)) {
2573 2574 xoap->xoa_generation = zp->z_gen;
2574 2575 XVA_SET_RTN(xvap, XAT_GEN);
2575 2576 }
2576 2577
2577 2578 if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) {
2578 2579 xoap->xoa_offline =
2579 2580 ((zp->z_pflags & ZFS_OFFLINE) != 0);
2580 2581 XVA_SET_RTN(xvap, XAT_OFFLINE);
2581 2582 }
2582 2583
2583 2584 if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
2584 2585 xoap->xoa_sparse =
2585 2586 ((zp->z_pflags & ZFS_SPARSE) != 0);
2586 2587 XVA_SET_RTN(xvap, XAT_SPARSE);
2587 2588 }
2588 2589 }
2589 2590
2590 2591 ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime);
2591 2592 ZFS_TIME_DECODE(&vap->va_mtime, mtime);
2592 2593 ZFS_TIME_DECODE(&vap->va_ctime, ctime);
2593 2594
2594 2595 mutex_exit(&zp->z_lock);
2595 2596
2596 2597 sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks);
2597 2598
2598 2599 if (zp->z_blksz == 0) {
2599 2600 /*
2600 2601 * Block size hasn't been set; suggest maximal I/O transfers.
2601 2602 */
2602 2603 vap->va_blksize = zfsvfs->z_max_blksz;
2603 2604 }
2604 2605
2605 2606 ZFS_EXIT(zfsvfs);
2606 2607 return (0);
2607 2608 }
2608 2609
2609 2610 /*
2610 2611 * Set the file attributes to the values contained in the
2611 2612 * vattr structure.
2612 2613 *
2613 2614 * IN: vp - vnode of file to be modified.
2614 2615 * vap - new attribute values.
2615 2616 * If AT_XVATTR set, then optional attrs are being set
2616 2617 * flags - ATTR_UTIME set if non-default time values provided.
2617 2618 * - ATTR_NOACLCHECK (CIFS context only).
2618 2619 * cr - credentials of caller.
2619 2620 * ct - caller context
2620 2621 *
2621 2622 * RETURN: 0 on success, error code on failure.
2622 2623 *
2623 2624 * Timestamps:
2624 2625 * vp - ctime updated, mtime updated if size changed.
2625 2626 */
2626 2627 /* ARGSUSED */
2627 2628 static int
2628 2629 zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr,
2629 2630 caller_context_t *ct)
2630 2631 {
2631 2632 znode_t *zp = VTOZ(vp);
2632 2633 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
2633 2634 zilog_t *zilog;
2634 2635 dmu_tx_t *tx;
2635 2636 vattr_t oldva;
2636 2637 xvattr_t tmpxvattr;
2637 2638 uint_t mask = vap->va_mask;
2638 2639 uint_t saved_mask = 0;
2639 2640 int trim_mask = 0;
2640 2641 uint64_t new_mode;
2641 2642 uint64_t new_uid, new_gid;
2642 2643 uint64_t xattr_obj;
2643 2644 uint64_t mtime[2], ctime[2];
2644 2645 znode_t *attrzp;
2645 2646 int need_policy = FALSE;
2646 2647 int err, err2;
2647 2648 zfs_fuid_info_t *fuidp = NULL;
2648 2649 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */
2649 2650 xoptattr_t *xoap;
2650 2651 zfs_acl_t *aclp;
2651 2652 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
2652 2653 boolean_t fuid_dirtied = B_FALSE;
2653 2654 sa_bulk_attr_t bulk[7], xattr_bulk[7];
2654 2655 int count = 0, xattr_count = 0;
2655 2656
2656 2657 if (mask == 0)
2657 2658 return (0);
2658 2659
2659 2660 if (mask & AT_NOSET)
2660 2661 return (SET_ERROR(EINVAL));
2661 2662
2662 2663 ZFS_ENTER(zfsvfs);
2663 2664 ZFS_VERIFY_ZP(zp);
2664 2665
2665 2666 zilog = zfsvfs->z_log;
2666 2667
2667 2668 /*
2668 2669 * Make sure that if we have ephemeral uid/gid or xvattr specified
2669 2670 * that file system is at proper version level
2670 2671 */
2671 2672
2672 2673 if (zfsvfs->z_use_fuids == B_FALSE &&
2673 2674 (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) ||
2674 2675 ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) ||
2675 2676 (mask & AT_XVATTR))) {
2676 2677 ZFS_EXIT(zfsvfs);
2677 2678 return (SET_ERROR(EINVAL));
2678 2679 }
2679 2680
2680 2681 if (mask & AT_SIZE && vp->v_type == VDIR) {
2681 2682 ZFS_EXIT(zfsvfs);
2682 2683 return (SET_ERROR(EISDIR));
2683 2684 }
2684 2685
2685 2686 if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) {
2686 2687 ZFS_EXIT(zfsvfs);
2687 2688 return (SET_ERROR(EINVAL));
2688 2689 }
2689 2690
2690 2691 /*
2691 2692 * If this is an xvattr_t, then get a pointer to the structure of
2692 2693 * optional attributes. If this is NULL, then we have a vattr_t.
2693 2694 */
2694 2695 xoap = xva_getxoptattr(xvap);
2695 2696
2696 2697 xva_init(&tmpxvattr);
2697 2698
2698 2699 /*
2699 2700 * Immutable files can only alter immutable bit and atime
2700 2701 */
2701 2702 if ((zp->z_pflags & ZFS_IMMUTABLE) &&
2702 2703 ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) ||
2703 2704 ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) {
2704 2705 ZFS_EXIT(zfsvfs);
2705 2706 return (SET_ERROR(EPERM));
2706 2707 }
2707 2708
2708 2709 if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) {
2709 2710 ZFS_EXIT(zfsvfs);
2710 2711 return (SET_ERROR(EPERM));
2711 2712 }
2712 2713
2713 2714 /*
2714 2715 * Verify timestamps doesn't overflow 32 bits.
2715 2716 * ZFS can handle large timestamps, but 32bit syscalls can't
2716 2717 * handle times greater than 2039. This check should be removed
2717 2718 * once large timestamps are fully supported.
2718 2719 */
2719 2720 if (mask & (AT_ATIME | AT_MTIME)) {
2720 2721 if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) ||
2721 2722 ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) {
2722 2723 ZFS_EXIT(zfsvfs);
2723 2724 return (SET_ERROR(EOVERFLOW));
2724 2725 }
2725 2726 }
2726 2727
2727 2728 top:
2728 2729 attrzp = NULL;
2729 2730 aclp = NULL;
2730 2731
2731 2732 /* Can this be moved to before the top label? */
2732 2733 if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) {
2733 2734 ZFS_EXIT(zfsvfs);
2734 2735 return (SET_ERROR(EROFS));
2735 2736 }
2736 2737
2737 2738 /*
2738 2739 * First validate permissions
2739 2740 */
2740 2741
2741 2742 if (mask & AT_SIZE) {
2742 2743 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr);
2743 2744 if (err) {
2744 2745 ZFS_EXIT(zfsvfs);
2745 2746 return (err);
2746 2747 }
2747 2748 /*
2748 2749 * XXX - Note, we are not providing any open
2749 2750 * mode flags here (like FNDELAY), so we may
2750 2751 * block if there are locks present... this
2751 2752 * should be addressed in openat().
2752 2753 */
2753 2754 /* XXX - would it be OK to generate a log record here? */
2754 2755 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE);
2755 2756 if (err) {
2756 2757 ZFS_EXIT(zfsvfs);
2757 2758 return (err);
2758 2759 }
2759 2760
2760 2761 if (vap->va_size == 0)
2761 2762 vnevent_truncate(ZTOV(zp), ct);
2762 2763 }
2763 2764
2764 2765 if (mask & (AT_ATIME|AT_MTIME) ||
2765 2766 ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
2766 2767 XVA_ISSET_REQ(xvap, XAT_READONLY) ||
2767 2768 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
2768 2769 XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
2769 2770 XVA_ISSET_REQ(xvap, XAT_SPARSE) ||
2770 2771 XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
2771 2772 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) {
2772 2773 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0,
2773 2774 skipaclchk, cr);
2774 2775 }
2775 2776
2776 2777 if (mask & (AT_UID|AT_GID)) {
2777 2778 int idmask = (mask & (AT_UID|AT_GID));
2778 2779 int take_owner;
2779 2780 int take_group;
2780 2781
2781 2782 /*
2782 2783 * NOTE: even if a new mode is being set,
2783 2784 * we may clear S_ISUID/S_ISGID bits.
2784 2785 */
2785 2786
2786 2787 if (!(mask & AT_MODE))
2787 2788 vap->va_mode = zp->z_mode;
2788 2789
2789 2790 /*
2790 2791 * Take ownership or chgrp to group we are a member of
2791 2792 */
2792 2793
2793 2794 take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr));
2794 2795 take_group = (mask & AT_GID) &&
2795 2796 zfs_groupmember(zfsvfs, vap->va_gid, cr);
2796 2797
2797 2798 /*
2798 2799 * If both AT_UID and AT_GID are set then take_owner and
2799 2800 * take_group must both be set in order to allow taking
2800 2801 * ownership.
2801 2802 *
2802 2803 * Otherwise, send the check through secpolicy_vnode_setattr()
2803 2804 *
2804 2805 */
2805 2806
2806 2807 if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) ||
2807 2808 ((idmask == AT_UID) && take_owner) ||
2808 2809 ((idmask == AT_GID) && take_group)) {
2809 2810 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0,
2810 2811 skipaclchk, cr) == 0) {
2811 2812 /*
2812 2813 * Remove setuid/setgid for non-privileged users
2813 2814 */
2814 2815 secpolicy_setid_clear(vap, cr);
2815 2816 trim_mask = (mask & (AT_UID|AT_GID));
2816 2817 } else {
2817 2818 need_policy = TRUE;
2818 2819 }
2819 2820 } else {
2820 2821 need_policy = TRUE;
2821 2822 }
2822 2823 }
2823 2824
2824 2825 mutex_enter(&zp->z_lock);
2825 2826 oldva.va_mode = zp->z_mode;
2826 2827 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid);
2827 2828 if (mask & AT_XVATTR) {
2828 2829 /*
2829 2830 * Update xvattr mask to include only those attributes
2830 2831 * that are actually changing.
2831 2832 *
2832 2833 * the bits will be restored prior to actually setting
2833 2834 * the attributes so the caller thinks they were set.
2834 2835 */
2835 2836 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) {
2836 2837 if (xoap->xoa_appendonly !=
2837 2838 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) {
2838 2839 need_policy = TRUE;
2839 2840 } else {
2840 2841 XVA_CLR_REQ(xvap, XAT_APPENDONLY);
2841 2842 XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY);
2842 2843 }
2843 2844 }
2844 2845
2845 2846 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) {
2846 2847 if (xoap->xoa_nounlink !=
2847 2848 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) {
2848 2849 need_policy = TRUE;
2849 2850 } else {
2850 2851 XVA_CLR_REQ(xvap, XAT_NOUNLINK);
2851 2852 XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK);
2852 2853 }
2853 2854 }
2854 2855
2855 2856 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) {
2856 2857 if (xoap->xoa_immutable !=
2857 2858 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) {
2858 2859 need_policy = TRUE;
2859 2860 } else {
2860 2861 XVA_CLR_REQ(xvap, XAT_IMMUTABLE);
2861 2862 XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE);
2862 2863 }
2863 2864 }
2864 2865
2865 2866 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) {
2866 2867 if (xoap->xoa_nodump !=
2867 2868 ((zp->z_pflags & ZFS_NODUMP) != 0)) {
2868 2869 need_policy = TRUE;
2869 2870 } else {
2870 2871 XVA_CLR_REQ(xvap, XAT_NODUMP);
2871 2872 XVA_SET_REQ(&tmpxvattr, XAT_NODUMP);
2872 2873 }
2873 2874 }
2874 2875
2875 2876 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) {
2876 2877 if (xoap->xoa_av_modified !=
2877 2878 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) {
2878 2879 need_policy = TRUE;
2879 2880 } else {
2880 2881 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED);
2881 2882 XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED);
2882 2883 }
2883 2884 }
2884 2885
2885 2886 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
2886 2887 if ((vp->v_type != VREG &&
2887 2888 xoap->xoa_av_quarantined) ||
2888 2889 xoap->xoa_av_quarantined !=
2889 2890 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) {
2890 2891 need_policy = TRUE;
2891 2892 } else {
2892 2893 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED);
2893 2894 XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED);
2894 2895 }
2895 2896 }
2896 2897
2897 2898 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) {
2898 2899 mutex_exit(&zp->z_lock);
2899 2900 ZFS_EXIT(zfsvfs);
2900 2901 return (SET_ERROR(EPERM));
2901 2902 }
2902 2903
2903 2904 if (need_policy == FALSE &&
2904 2905 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) ||
2905 2906 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) {
2906 2907 need_policy = TRUE;
2907 2908 }
2908 2909 }
2909 2910
2910 2911 mutex_exit(&zp->z_lock);
2911 2912
2912 2913 if (mask & AT_MODE) {
2913 2914 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) {
2914 2915 err = secpolicy_setid_setsticky_clear(vp, vap,
2915 2916 &oldva, cr);
2916 2917 if (err) {
2917 2918 ZFS_EXIT(zfsvfs);
2918 2919 return (err);
2919 2920 }
2920 2921 trim_mask |= AT_MODE;
2921 2922 } else {
2922 2923 need_policy = TRUE;
2923 2924 }
2924 2925 }
2925 2926
2926 2927 if (need_policy) {
2927 2928 /*
2928 2929 * If trim_mask is set then take ownership
2929 2930 * has been granted or write_acl is present and user
2930 2931 * has the ability to modify mode. In that case remove
2931 2932 * UID|GID and or MODE from mask so that
2932 2933 * secpolicy_vnode_setattr() doesn't revoke it.
2933 2934 */
2934 2935
2935 2936 if (trim_mask) {
2936 2937 saved_mask = vap->va_mask;
2937 2938 vap->va_mask &= ~trim_mask;
2938 2939 }
2939 2940 err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags,
2940 2941 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp);
2941 2942 if (err) {
2942 2943 ZFS_EXIT(zfsvfs);
2943 2944 return (err);
2944 2945 }
2945 2946
2946 2947 if (trim_mask)
2947 2948 vap->va_mask |= saved_mask;
2948 2949 }
2949 2950
2950 2951 /*
2951 2952 * secpolicy_vnode_setattr, or take ownership may have
2952 2953 * changed va_mask
2953 2954 */
2954 2955 mask = vap->va_mask;
2955 2956
2956 2957 if ((mask & (AT_UID | AT_GID))) {
2957 2958 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs),
2958 2959 &xattr_obj, sizeof (xattr_obj));
2959 2960
2960 2961 if (err == 0 && xattr_obj) {
2961 2962 err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp);
2962 2963 if (err)
2963 2964 goto out2;
2964 2965 }
2965 2966 if (mask & AT_UID) {
2966 2967 new_uid = zfs_fuid_create(zfsvfs,
2967 2968 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp);
2968 2969 if (new_uid != zp->z_uid &&
2969 2970 zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) {
2970 2971 if (attrzp)
2971 2972 VN_RELE(ZTOV(attrzp));
2972 2973 err = SET_ERROR(EDQUOT);
2973 2974 goto out2;
2974 2975 }
2975 2976 }
2976 2977
2977 2978 if (mask & AT_GID) {
2978 2979 new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid,
2979 2980 cr, ZFS_GROUP, &fuidp);
2980 2981 if (new_gid != zp->z_gid &&
2981 2982 zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) {
2982 2983 if (attrzp)
2983 2984 VN_RELE(ZTOV(attrzp));
2984 2985 err = SET_ERROR(EDQUOT);
2985 2986 goto out2;
2986 2987 }
2987 2988 }
2988 2989 }
2989 2990 tx = dmu_tx_create(zfsvfs->z_os);
2990 2991
2991 2992 if (mask & AT_MODE) {
2992 2993 uint64_t pmode = zp->z_mode;
2993 2994 uint64_t acl_obj;
2994 2995 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT);
2995 2996
2996 2997 if (zp->z_zfsvfs->z_acl_mode == ZFS_ACL_RESTRICTED &&
2997 2998 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) {
2998 2999 err = SET_ERROR(EPERM);
2999 3000 goto out;
3000 3001 }
3001 3002
3002 3003 if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))
3003 3004 goto out;
3004 3005
3005 3006 mutex_enter(&zp->z_lock);
3006 3007 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) {
3007 3008 /*
3008 3009 * Are we upgrading ACL from old V0 format
3009 3010 * to V1 format?
3010 3011 */
3011 3012 if (zfsvfs->z_version >= ZPL_VERSION_FUID &&
3012 3013 zfs_znode_acl_version(zp) ==
3013 3014 ZFS_ACL_VERSION_INITIAL) {
3014 3015 dmu_tx_hold_free(tx, acl_obj, 0,
3015 3016 DMU_OBJECT_END);
3016 3017 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3017 3018 0, aclp->z_acl_bytes);
3018 3019 } else {
3019 3020 dmu_tx_hold_write(tx, acl_obj, 0,
3020 3021 aclp->z_acl_bytes);
3021 3022 }
3022 3023 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3023 3024 dmu_tx_hold_write(tx, DMU_NEW_OBJECT,
3024 3025 0, aclp->z_acl_bytes);
3025 3026 }
3026 3027 mutex_exit(&zp->z_lock);
3027 3028 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3028 3029 } else {
3029 3030 if ((mask & AT_XVATTR) &&
3030 3031 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3031 3032 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE);
3032 3033 else
3033 3034 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
3034 3035 }
3035 3036
↓ open down ↓ |
2236 lines elided |
↑ open up ↑ |
3036 3037 if (attrzp) {
3037 3038 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE);
3038 3039 }
3039 3040
3040 3041 fuid_dirtied = zfsvfs->z_fuid_dirty;
3041 3042 if (fuid_dirtied)
3042 3043 zfs_fuid_txhold(zfsvfs, tx);
3043 3044
3044 3045 zfs_sa_upgrade_txholds(tx, zp);
3045 3046
3046 - err = dmu_tx_assign(tx, TXG_NOWAIT);
3047 - if (err) {
3048 - if (err == ERESTART)
3049 - dmu_tx_wait(tx);
3047 + err = dmu_tx_assign(tx, TXG_WAIT);
3048 + if (err)
3050 3049 goto out;
3051 - }
3052 3050
3053 3051 count = 0;
3054 3052 /*
3055 3053 * Set each attribute requested.
3056 3054 * We group settings according to the locks they need to acquire.
3057 3055 *
3058 3056 * Note: you cannot set ctime directly, although it will be
3059 3057 * updated as a side-effect of calling this function.
3060 3058 */
3061 3059
3062 3060
3063 3061 if (mask & (AT_UID|AT_GID|AT_MODE))
3064 3062 mutex_enter(&zp->z_acl_lock);
3065 3063 mutex_enter(&zp->z_lock);
3066 3064
3067 3065 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
3068 3066 &zp->z_pflags, sizeof (zp->z_pflags));
3069 3067
3070 3068 if (attrzp) {
3071 3069 if (mask & (AT_UID|AT_GID|AT_MODE))
3072 3070 mutex_enter(&attrzp->z_acl_lock);
3073 3071 mutex_enter(&attrzp->z_lock);
3074 3072 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3075 3073 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags,
3076 3074 sizeof (attrzp->z_pflags));
3077 3075 }
3078 3076
3079 3077 if (mask & (AT_UID|AT_GID)) {
3080 3078
3081 3079 if (mask & AT_UID) {
3082 3080 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL,
3083 3081 &new_uid, sizeof (new_uid));
3084 3082 zp->z_uid = new_uid;
3085 3083 if (attrzp) {
3086 3084 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3087 3085 SA_ZPL_UID(zfsvfs), NULL, &new_uid,
3088 3086 sizeof (new_uid));
3089 3087 attrzp->z_uid = new_uid;
3090 3088 }
3091 3089 }
3092 3090
3093 3091 if (mask & AT_GID) {
3094 3092 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs),
3095 3093 NULL, &new_gid, sizeof (new_gid));
3096 3094 zp->z_gid = new_gid;
3097 3095 if (attrzp) {
3098 3096 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3099 3097 SA_ZPL_GID(zfsvfs), NULL, &new_gid,
3100 3098 sizeof (new_gid));
3101 3099 attrzp->z_gid = new_gid;
3102 3100 }
3103 3101 }
3104 3102 if (!(mask & AT_MODE)) {
3105 3103 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs),
3106 3104 NULL, &new_mode, sizeof (new_mode));
3107 3105 new_mode = zp->z_mode;
3108 3106 }
3109 3107 err = zfs_acl_chown_setattr(zp);
3110 3108 ASSERT(err == 0);
3111 3109 if (attrzp) {
3112 3110 err = zfs_acl_chown_setattr(attrzp);
3113 3111 ASSERT(err == 0);
3114 3112 }
3115 3113 }
3116 3114
3117 3115 if (mask & AT_MODE) {
3118 3116 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL,
3119 3117 &new_mode, sizeof (new_mode));
3120 3118 zp->z_mode = new_mode;
3121 3119 ASSERT3U((uintptr_t)aclp, !=, NULL);
3122 3120 err = zfs_aclset_common(zp, aclp, cr, tx);
3123 3121 ASSERT0(err);
3124 3122 if (zp->z_acl_cached)
3125 3123 zfs_acl_free(zp->z_acl_cached);
3126 3124 zp->z_acl_cached = aclp;
3127 3125 aclp = NULL;
3128 3126 }
3129 3127
3130 3128
3131 3129 if (mask & AT_ATIME) {
3132 3130 ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime);
3133 3131 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL,
3134 3132 &zp->z_atime, sizeof (zp->z_atime));
3135 3133 }
3136 3134
3137 3135 if (mask & AT_MTIME) {
3138 3136 ZFS_TIME_ENCODE(&vap->va_mtime, mtime);
3139 3137 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
3140 3138 mtime, sizeof (mtime));
3141 3139 }
3142 3140
3143 3141 /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */
3144 3142 if (mask & AT_SIZE && !(mask & AT_MTIME)) {
3145 3143 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs),
3146 3144 NULL, mtime, sizeof (mtime));
3147 3145 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3148 3146 &ctime, sizeof (ctime));
3149 3147 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
3150 3148 B_TRUE);
3151 3149 } else if (mask != 0) {
3152 3150 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
3153 3151 &ctime, sizeof (ctime));
3154 3152 zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime,
3155 3153 B_TRUE);
3156 3154 if (attrzp) {
3157 3155 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count,
3158 3156 SA_ZPL_CTIME(zfsvfs), NULL,
3159 3157 &ctime, sizeof (ctime));
3160 3158 zfs_tstamp_update_setup(attrzp, STATE_CHANGED,
3161 3159 mtime, ctime, B_TRUE);
3162 3160 }
3163 3161 }
3164 3162 /*
3165 3163 * Do this after setting timestamps to prevent timestamp
3166 3164 * update from toggling bit
3167 3165 */
3168 3166
3169 3167 if (xoap && (mask & AT_XVATTR)) {
3170 3168
3171 3169 /*
3172 3170 * restore trimmed off masks
3173 3171 * so that return masks can be set for caller.
3174 3172 */
3175 3173
3176 3174 if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) {
3177 3175 XVA_SET_REQ(xvap, XAT_APPENDONLY);
3178 3176 }
3179 3177 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) {
3180 3178 XVA_SET_REQ(xvap, XAT_NOUNLINK);
3181 3179 }
3182 3180 if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) {
3183 3181 XVA_SET_REQ(xvap, XAT_IMMUTABLE);
3184 3182 }
3185 3183 if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) {
3186 3184 XVA_SET_REQ(xvap, XAT_NODUMP);
3187 3185 }
3188 3186 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) {
3189 3187 XVA_SET_REQ(xvap, XAT_AV_MODIFIED);
3190 3188 }
3191 3189 if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) {
3192 3190 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED);
3193 3191 }
3194 3192
3195 3193 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP))
3196 3194 ASSERT(vp->v_type == VREG);
3197 3195
3198 3196 zfs_xvattr_set(zp, xvap, tx);
3199 3197 }
3200 3198
3201 3199 if (fuid_dirtied)
3202 3200 zfs_fuid_sync(zfsvfs, tx);
3203 3201
3204 3202 if (mask != 0)
3205 3203 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp);
3206 3204
3207 3205 mutex_exit(&zp->z_lock);
3208 3206 if (mask & (AT_UID|AT_GID|AT_MODE))
3209 3207 mutex_exit(&zp->z_acl_lock);
3210 3208
3211 3209 if (attrzp) {
3212 3210 if (mask & (AT_UID|AT_GID|AT_MODE))
3213 3211 mutex_exit(&attrzp->z_acl_lock);
3214 3212 mutex_exit(&attrzp->z_lock);
3215 3213 }
3216 3214 out:
3217 3215 if (err == 0 && attrzp) {
3218 3216 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk,
3219 3217 xattr_count, tx);
3220 3218 ASSERT(err2 == 0);
3221 3219 }
3222 3220
3223 3221 if (attrzp)
3224 3222 VN_RELE(ZTOV(attrzp));
3225 3223
3226 3224 if (aclp)
3227 3225 zfs_acl_free(aclp);
3228 3226
3229 3227 if (fuidp) {
3230 3228 zfs_fuid_info_free(fuidp);
3231 3229 fuidp = NULL;
3232 3230 }
3233 3231
3234 3232 if (err) {
3235 3233 dmu_tx_abort(tx);
3236 3234 if (err == ERESTART)
3237 3235 goto top;
3238 3236 } else {
3239 3237 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx);
3240 3238 dmu_tx_commit(tx);
3241 3239 }
3242 3240
3243 3241 out2:
3244 3242 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3245 3243 zil_commit(zilog, 0);
3246 3244
3247 3245 ZFS_EXIT(zfsvfs);
3248 3246 return (err);
3249 3247 }
3250 3248
3251 3249 typedef struct zfs_zlock {
3252 3250 krwlock_t *zl_rwlock; /* lock we acquired */
3253 3251 znode_t *zl_znode; /* znode we held */
3254 3252 struct zfs_zlock *zl_next; /* next in list */
3255 3253 } zfs_zlock_t;
3256 3254
3257 3255 /*
3258 3256 * Drop locks and release vnodes that were held by zfs_rename_lock().
3259 3257 */
3260 3258 static void
3261 3259 zfs_rename_unlock(zfs_zlock_t **zlpp)
3262 3260 {
3263 3261 zfs_zlock_t *zl;
3264 3262
3265 3263 while ((zl = *zlpp) != NULL) {
3266 3264 if (zl->zl_znode != NULL)
3267 3265 VN_RELE(ZTOV(zl->zl_znode));
3268 3266 rw_exit(zl->zl_rwlock);
3269 3267 *zlpp = zl->zl_next;
3270 3268 kmem_free(zl, sizeof (*zl));
3271 3269 }
3272 3270 }
3273 3271
3274 3272 /*
3275 3273 * Search back through the directory tree, using the ".." entries.
3276 3274 * Lock each directory in the chain to prevent concurrent renames.
3277 3275 * Fail any attempt to move a directory into one of its own descendants.
3278 3276 * XXX - z_parent_lock can overlap with map or grow locks
3279 3277 */
3280 3278 static int
3281 3279 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp)
3282 3280 {
3283 3281 zfs_zlock_t *zl;
3284 3282 znode_t *zp = tdzp;
3285 3283 uint64_t rootid = zp->z_zfsvfs->z_root;
3286 3284 uint64_t oidp = zp->z_id;
3287 3285 krwlock_t *rwlp = &szp->z_parent_lock;
3288 3286 krw_t rw = RW_WRITER;
3289 3287
3290 3288 /*
3291 3289 * First pass write-locks szp and compares to zp->z_id.
3292 3290 * Later passes read-lock zp and compare to zp->z_parent.
3293 3291 */
3294 3292 do {
3295 3293 if (!rw_tryenter(rwlp, rw)) {
3296 3294 /*
3297 3295 * Another thread is renaming in this path.
3298 3296 * Note that if we are a WRITER, we don't have any
3299 3297 * parent_locks held yet.
3300 3298 */
3301 3299 if (rw == RW_READER && zp->z_id > szp->z_id) {
3302 3300 /*
3303 3301 * Drop our locks and restart
3304 3302 */
3305 3303 zfs_rename_unlock(&zl);
3306 3304 *zlpp = NULL;
3307 3305 zp = tdzp;
3308 3306 oidp = zp->z_id;
3309 3307 rwlp = &szp->z_parent_lock;
3310 3308 rw = RW_WRITER;
3311 3309 continue;
3312 3310 } else {
3313 3311 /*
3314 3312 * Wait for other thread to drop its locks
3315 3313 */
3316 3314 rw_enter(rwlp, rw);
3317 3315 }
3318 3316 }
3319 3317
3320 3318 zl = kmem_alloc(sizeof (*zl), KM_SLEEP);
3321 3319 zl->zl_rwlock = rwlp;
3322 3320 zl->zl_znode = NULL;
3323 3321 zl->zl_next = *zlpp;
3324 3322 *zlpp = zl;
3325 3323
3326 3324 if (oidp == szp->z_id) /* We're a descendant of szp */
3327 3325 return (SET_ERROR(EINVAL));
3328 3326
3329 3327 if (oidp == rootid) /* We've hit the top */
3330 3328 return (0);
3331 3329
3332 3330 if (rw == RW_READER) { /* i.e. not the first pass */
3333 3331 int error = zfs_zget(zp->z_zfsvfs, oidp, &zp);
3334 3332 if (error)
3335 3333 return (error);
3336 3334 zl->zl_znode = zp;
3337 3335 }
3338 3336 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs),
3339 3337 &oidp, sizeof (oidp));
3340 3338 rwlp = &zp->z_parent_lock;
3341 3339 rw = RW_READER;
3342 3340
3343 3341 } while (zp->z_id != sdzp->z_id);
3344 3342
3345 3343 return (0);
3346 3344 }
3347 3345
3348 3346 /*
3349 3347 * Move an entry from the provided source directory to the target
3350 3348 * directory. Change the entry name as indicated.
3351 3349 *
3352 3350 * IN: sdvp - Source directory containing the "old entry".
3353 3351 * snm - Old entry name.
3354 3352 * tdvp - Target directory to contain the "new entry".
3355 3353 * tnm - New entry name.
3356 3354 * cr - credentials of caller.
3357 3355 * ct - caller context
3358 3356 * flags - case flags
3359 3357 *
3360 3358 * RETURN: 0 on success, error code on failure.
3361 3359 *
3362 3360 * Timestamps:
3363 3361 * sdvp,tdvp - ctime|mtime updated
3364 3362 */
3365 3363 /*ARGSUSED*/
3366 3364 static int
3367 3365 zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr,
3368 3366 caller_context_t *ct, int flags)
3369 3367 {
3370 3368 znode_t *tdzp, *szp, *tzp;
3371 3369 znode_t *sdzp = VTOZ(sdvp);
3372 3370 zfsvfs_t *zfsvfs = sdzp->z_zfsvfs;
3373 3371 zilog_t *zilog;
3374 3372 vnode_t *realvp;
3375 3373 zfs_dirlock_t *sdl, *tdl;
3376 3374 dmu_tx_t *tx;
3377 3375 zfs_zlock_t *zl;
3378 3376 int cmp, serr, terr;
3379 3377 int error = 0;
3380 3378 int zflg = 0;
3381 3379 boolean_t waited = B_FALSE;
3382 3380
3383 3381 ZFS_ENTER(zfsvfs);
3384 3382 ZFS_VERIFY_ZP(sdzp);
3385 3383 zilog = zfsvfs->z_log;
3386 3384
3387 3385 /*
3388 3386 * Make sure we have the real vp for the target directory.
3389 3387 */
3390 3388 if (VOP_REALVP(tdvp, &realvp, ct) == 0)
3391 3389 tdvp = realvp;
3392 3390
3393 3391 tdzp = VTOZ(tdvp);
3394 3392 ZFS_VERIFY_ZP(tdzp);
3395 3393
3396 3394 /*
3397 3395 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3398 3396 * ctldir appear to have the same v_vfsp.
3399 3397 */
3400 3398 if (tdzp->z_zfsvfs != zfsvfs || zfsctl_is_node(tdvp)) {
3401 3399 ZFS_EXIT(zfsvfs);
3402 3400 return (SET_ERROR(EXDEV));
3403 3401 }
3404 3402
3405 3403 if (zfsvfs->z_utf8 && u8_validate(tnm,
3406 3404 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3407 3405 ZFS_EXIT(zfsvfs);
3408 3406 return (SET_ERROR(EILSEQ));
3409 3407 }
3410 3408
3411 3409 if (flags & FIGNORECASE)
3412 3410 zflg |= ZCILOOK;
3413 3411
3414 3412 top:
3415 3413 szp = NULL;
3416 3414 tzp = NULL;
3417 3415 zl = NULL;
3418 3416
3419 3417 /*
3420 3418 * This is to prevent the creation of links into attribute space
3421 3419 * by renaming a linked file into/outof an attribute directory.
3422 3420 * See the comment in zfs_link() for why this is considered bad.
3423 3421 */
3424 3422 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) {
3425 3423 ZFS_EXIT(zfsvfs);
3426 3424 return (SET_ERROR(EINVAL));
3427 3425 }
3428 3426
3429 3427 /*
3430 3428 * Lock source and target directory entries. To prevent deadlock,
3431 3429 * a lock ordering must be defined. We lock the directory with
3432 3430 * the smallest object id first, or if it's a tie, the one with
3433 3431 * the lexically first name.
3434 3432 */
3435 3433 if (sdzp->z_id < tdzp->z_id) {
3436 3434 cmp = -1;
3437 3435 } else if (sdzp->z_id > tdzp->z_id) {
3438 3436 cmp = 1;
3439 3437 } else {
3440 3438 /*
3441 3439 * First compare the two name arguments without
3442 3440 * considering any case folding.
3443 3441 */
3444 3442 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER);
3445 3443
3446 3444 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error);
3447 3445 ASSERT(error == 0 || !zfsvfs->z_utf8);
3448 3446 if (cmp == 0) {
3449 3447 /*
3450 3448 * POSIX: "If the old argument and the new argument
3451 3449 * both refer to links to the same existing file,
3452 3450 * the rename() function shall return successfully
3453 3451 * and perform no other action."
3454 3452 */
3455 3453 ZFS_EXIT(zfsvfs);
3456 3454 return (0);
3457 3455 }
3458 3456 /*
3459 3457 * If the file system is case-folding, then we may
3460 3458 * have some more checking to do. A case-folding file
3461 3459 * system is either supporting mixed case sensitivity
3462 3460 * access or is completely case-insensitive. Note
3463 3461 * that the file system is always case preserving.
3464 3462 *
3465 3463 * In mixed sensitivity mode case sensitive behavior
3466 3464 * is the default. FIGNORECASE must be used to
3467 3465 * explicitly request case insensitive behavior.
3468 3466 *
3469 3467 * If the source and target names provided differ only
3470 3468 * by case (e.g., a request to rename 'tim' to 'Tim'),
3471 3469 * we will treat this as a special case in the
3472 3470 * case-insensitive mode: as long as the source name
3473 3471 * is an exact match, we will allow this to proceed as
3474 3472 * a name-change request.
3475 3473 */
3476 3474 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE ||
3477 3475 (zfsvfs->z_case == ZFS_CASE_MIXED &&
3478 3476 flags & FIGNORECASE)) &&
3479 3477 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST,
3480 3478 &error) == 0) {
3481 3479 /*
3482 3480 * case preserving rename request, require exact
3483 3481 * name matches
3484 3482 */
3485 3483 zflg |= ZCIEXACT;
3486 3484 zflg &= ~ZCILOOK;
3487 3485 }
3488 3486 }
3489 3487
3490 3488 /*
3491 3489 * If the source and destination directories are the same, we should
3492 3490 * grab the z_name_lock of that directory only once.
3493 3491 */
3494 3492 if (sdzp == tdzp) {
3495 3493 zflg |= ZHAVELOCK;
3496 3494 rw_enter(&sdzp->z_name_lock, RW_READER);
3497 3495 }
3498 3496
3499 3497 if (cmp < 0) {
3500 3498 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp,
3501 3499 ZEXISTS | zflg, NULL, NULL);
3502 3500 terr = zfs_dirent_lock(&tdl,
3503 3501 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL);
3504 3502 } else {
3505 3503 terr = zfs_dirent_lock(&tdl,
3506 3504 tdzp, tnm, &tzp, zflg, NULL, NULL);
3507 3505 serr = zfs_dirent_lock(&sdl,
3508 3506 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg,
3509 3507 NULL, NULL);
3510 3508 }
3511 3509
3512 3510 if (serr) {
3513 3511 /*
3514 3512 * Source entry invalid or not there.
3515 3513 */
3516 3514 if (!terr) {
3517 3515 zfs_dirent_unlock(tdl);
3518 3516 if (tzp)
3519 3517 VN_RELE(ZTOV(tzp));
3520 3518 }
3521 3519
3522 3520 if (sdzp == tdzp)
3523 3521 rw_exit(&sdzp->z_name_lock);
3524 3522
3525 3523 if (strcmp(snm, "..") == 0)
3526 3524 serr = SET_ERROR(EINVAL);
3527 3525 ZFS_EXIT(zfsvfs);
3528 3526 return (serr);
3529 3527 }
3530 3528 if (terr) {
3531 3529 zfs_dirent_unlock(sdl);
3532 3530 VN_RELE(ZTOV(szp));
3533 3531
3534 3532 if (sdzp == tdzp)
3535 3533 rw_exit(&sdzp->z_name_lock);
3536 3534
3537 3535 if (strcmp(tnm, "..") == 0)
3538 3536 terr = SET_ERROR(EINVAL);
3539 3537 ZFS_EXIT(zfsvfs);
3540 3538 return (terr);
3541 3539 }
3542 3540
3543 3541 /*
3544 3542 * Must have write access at the source to remove the old entry
3545 3543 * and write access at the target to create the new entry.
3546 3544 * Note that if target and source are the same, this can be
3547 3545 * done in a single check.
3548 3546 */
3549 3547
3550 3548 if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))
3551 3549 goto out;
3552 3550
3553 3551 if (ZTOV(szp)->v_type == VDIR) {
3554 3552 /*
3555 3553 * Check to make sure rename is valid.
3556 3554 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d
3557 3555 */
3558 3556 if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl))
3559 3557 goto out;
3560 3558 }
3561 3559
3562 3560 /*
3563 3561 * Does target exist?
3564 3562 */
3565 3563 if (tzp) {
3566 3564 /*
3567 3565 * Source and target must be the same type.
3568 3566 */
3569 3567 if (ZTOV(szp)->v_type == VDIR) {
3570 3568 if (ZTOV(tzp)->v_type != VDIR) {
3571 3569 error = SET_ERROR(ENOTDIR);
3572 3570 goto out;
3573 3571 }
3574 3572 } else {
3575 3573 if (ZTOV(tzp)->v_type == VDIR) {
3576 3574 error = SET_ERROR(EISDIR);
3577 3575 goto out;
3578 3576 }
3579 3577 }
3580 3578 /*
3581 3579 * POSIX dictates that when the source and target
3582 3580 * entries refer to the same file object, rename
3583 3581 * must do nothing and exit without error.
3584 3582 */
3585 3583 if (szp->z_id == tzp->z_id) {
3586 3584 error = 0;
3587 3585 goto out;
3588 3586 }
3589 3587 }
3590 3588
3591 3589 vnevent_rename_src(ZTOV(szp), sdvp, snm, ct);
3592 3590 if (tzp)
3593 3591 vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct);
3594 3592
3595 3593 /*
3596 3594 * notify the target directory if it is not the same
3597 3595 * as source directory.
3598 3596 */
3599 3597 if (tdvp != sdvp) {
3600 3598 vnevent_rename_dest_dir(tdvp, ct);
3601 3599 }
3602 3600
3603 3601 tx = dmu_tx_create(zfsvfs->z_os);
3604 3602 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
3605 3603 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE);
3606 3604 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm);
3607 3605 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm);
3608 3606 if (sdzp != tdzp) {
3609 3607 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE);
3610 3608 zfs_sa_upgrade_txholds(tx, tdzp);
3611 3609 }
3612 3610 if (tzp) {
3613 3611 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE);
3614 3612 zfs_sa_upgrade_txholds(tx, tzp);
3615 3613 }
3616 3614
3617 3615 zfs_sa_upgrade_txholds(tx, szp);
3618 3616 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL);
3619 3617 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3620 3618 if (error) {
3621 3619 if (zl != NULL)
3622 3620 zfs_rename_unlock(&zl);
3623 3621 zfs_dirent_unlock(sdl);
3624 3622 zfs_dirent_unlock(tdl);
3625 3623
3626 3624 if (sdzp == tdzp)
3627 3625 rw_exit(&sdzp->z_name_lock);
3628 3626
3629 3627 VN_RELE(ZTOV(szp));
3630 3628 if (tzp)
3631 3629 VN_RELE(ZTOV(tzp));
3632 3630 if (error == ERESTART) {
3633 3631 waited = B_TRUE;
3634 3632 dmu_tx_wait(tx);
3635 3633 dmu_tx_abort(tx);
3636 3634 goto top;
3637 3635 }
3638 3636 dmu_tx_abort(tx);
3639 3637 ZFS_EXIT(zfsvfs);
3640 3638 return (error);
3641 3639 }
3642 3640
3643 3641 if (tzp) /* Attempt to remove the existing target */
3644 3642 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL);
3645 3643
3646 3644 if (error == 0) {
3647 3645 error = zfs_link_create(tdl, szp, tx, ZRENAMING);
3648 3646 if (error == 0) {
3649 3647 szp->z_pflags |= ZFS_AV_MODIFIED;
3650 3648
3651 3649 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs),
3652 3650 (void *)&szp->z_pflags, sizeof (uint64_t), tx);
3653 3651 ASSERT0(error);
3654 3652
3655 3653 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL);
3656 3654 if (error == 0) {
3657 3655 zfs_log_rename(zilog, tx, TX_RENAME |
3658 3656 (flags & FIGNORECASE ? TX_CI : 0), sdzp,
3659 3657 sdl->dl_name, tdzp, tdl->dl_name, szp);
3660 3658
3661 3659 /*
3662 3660 * Update path information for the target vnode
3663 3661 */
3664 3662 vn_renamepath(tdvp, ZTOV(szp), tnm,
3665 3663 strlen(tnm));
3666 3664 } else {
3667 3665 /*
3668 3666 * At this point, we have successfully created
3669 3667 * the target name, but have failed to remove
3670 3668 * the source name. Since the create was done
3671 3669 * with the ZRENAMING flag, there are
3672 3670 * complications; for one, the link count is
3673 3671 * wrong. The easiest way to deal with this
3674 3672 * is to remove the newly created target, and
3675 3673 * return the original error. This must
3676 3674 * succeed; fortunately, it is very unlikely to
3677 3675 * fail, since we just created it.
3678 3676 */
3679 3677 VERIFY3U(zfs_link_destroy(tdl, szp, tx,
3680 3678 ZRENAMING, NULL), ==, 0);
3681 3679 }
3682 3680 }
3683 3681 }
3684 3682
3685 3683 dmu_tx_commit(tx);
3686 3684 out:
3687 3685 if (zl != NULL)
3688 3686 zfs_rename_unlock(&zl);
3689 3687
3690 3688 zfs_dirent_unlock(sdl);
3691 3689 zfs_dirent_unlock(tdl);
3692 3690
3693 3691 if (sdzp == tdzp)
3694 3692 rw_exit(&sdzp->z_name_lock);
3695 3693
3696 3694
3697 3695 VN_RELE(ZTOV(szp));
3698 3696 if (tzp)
3699 3697 VN_RELE(ZTOV(tzp));
3700 3698
3701 3699 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3702 3700 zil_commit(zilog, 0);
3703 3701
3704 3702 ZFS_EXIT(zfsvfs);
3705 3703 return (error);
3706 3704 }
3707 3705
3708 3706 /*
3709 3707 * Insert the indicated symbolic reference entry into the directory.
3710 3708 *
3711 3709 * IN: dvp - Directory to contain new symbolic link.
3712 3710 * link - Name for new symlink entry.
3713 3711 * vap - Attributes of new entry.
3714 3712 * cr - credentials of caller.
3715 3713 * ct - caller context
3716 3714 * flags - case flags
3717 3715 *
3718 3716 * RETURN: 0 on success, error code on failure.
3719 3717 *
3720 3718 * Timestamps:
3721 3719 * dvp - ctime|mtime updated
3722 3720 */
3723 3721 /*ARGSUSED*/
3724 3722 static int
3725 3723 zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr,
3726 3724 caller_context_t *ct, int flags)
3727 3725 {
3728 3726 znode_t *zp, *dzp = VTOZ(dvp);
3729 3727 zfs_dirlock_t *dl;
3730 3728 dmu_tx_t *tx;
3731 3729 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3732 3730 zilog_t *zilog;
3733 3731 uint64_t len = strlen(link);
3734 3732 int error;
3735 3733 int zflg = ZNEW;
3736 3734 zfs_acl_ids_t acl_ids;
3737 3735 boolean_t fuid_dirtied;
3738 3736 uint64_t txtype = TX_SYMLINK;
3739 3737 boolean_t waited = B_FALSE;
3740 3738
3741 3739 ASSERT(vap->va_type == VLNK);
3742 3740
3743 3741 ZFS_ENTER(zfsvfs);
3744 3742 ZFS_VERIFY_ZP(dzp);
3745 3743 zilog = zfsvfs->z_log;
3746 3744
3747 3745 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name),
3748 3746 NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3749 3747 ZFS_EXIT(zfsvfs);
3750 3748 return (SET_ERROR(EILSEQ));
3751 3749 }
3752 3750 if (flags & FIGNORECASE)
3753 3751 zflg |= ZCILOOK;
3754 3752
3755 3753 if (len > MAXPATHLEN) {
3756 3754 ZFS_EXIT(zfsvfs);
3757 3755 return (SET_ERROR(ENAMETOOLONG));
3758 3756 }
3759 3757
3760 3758 if ((error = zfs_acl_ids_create(dzp, 0,
3761 3759 vap, cr, NULL, &acl_ids)) != 0) {
3762 3760 ZFS_EXIT(zfsvfs);
3763 3761 return (error);
3764 3762 }
3765 3763 top:
3766 3764 /*
3767 3765 * Attempt to lock directory; fail if entry already exists.
3768 3766 */
3769 3767 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL);
3770 3768 if (error) {
3771 3769 zfs_acl_ids_free(&acl_ids);
3772 3770 ZFS_EXIT(zfsvfs);
3773 3771 return (error);
3774 3772 }
3775 3773
3776 3774 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
3777 3775 zfs_acl_ids_free(&acl_ids);
3778 3776 zfs_dirent_unlock(dl);
3779 3777 ZFS_EXIT(zfsvfs);
3780 3778 return (error);
3781 3779 }
3782 3780
3783 3781 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) {
3784 3782 zfs_acl_ids_free(&acl_ids);
3785 3783 zfs_dirent_unlock(dl);
3786 3784 ZFS_EXIT(zfsvfs);
3787 3785 return (SET_ERROR(EDQUOT));
3788 3786 }
3789 3787 tx = dmu_tx_create(zfsvfs->z_os);
3790 3788 fuid_dirtied = zfsvfs->z_fuid_dirty;
3791 3789 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len));
3792 3790 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
3793 3791 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes +
3794 3792 ZFS_SA_BASE_ATTR_SIZE + len);
3795 3793 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE);
3796 3794 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) {
3797 3795 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0,
3798 3796 acl_ids.z_aclp->z_acl_bytes);
3799 3797 }
3800 3798 if (fuid_dirtied)
3801 3799 zfs_fuid_txhold(zfsvfs, tx);
3802 3800 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
3803 3801 if (error) {
3804 3802 zfs_dirent_unlock(dl);
3805 3803 if (error == ERESTART) {
3806 3804 waited = B_TRUE;
3807 3805 dmu_tx_wait(tx);
3808 3806 dmu_tx_abort(tx);
3809 3807 goto top;
3810 3808 }
3811 3809 zfs_acl_ids_free(&acl_ids);
3812 3810 dmu_tx_abort(tx);
3813 3811 ZFS_EXIT(zfsvfs);
3814 3812 return (error);
3815 3813 }
3816 3814
3817 3815 /*
3818 3816 * Create a new object for the symlink.
3819 3817 * for version 4 ZPL datsets the symlink will be an SA attribute
3820 3818 */
3821 3819 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids);
3822 3820
3823 3821 if (fuid_dirtied)
3824 3822 zfs_fuid_sync(zfsvfs, tx);
3825 3823
3826 3824 mutex_enter(&zp->z_lock);
3827 3825 if (zp->z_is_sa)
3828 3826 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs),
3829 3827 link, len, tx);
3830 3828 else
3831 3829 zfs_sa_symlink(zp, link, len, tx);
3832 3830 mutex_exit(&zp->z_lock);
3833 3831
3834 3832 zp->z_size = len;
3835 3833 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs),
3836 3834 &zp->z_size, sizeof (zp->z_size), tx);
3837 3835 /*
3838 3836 * Insert the new object into the directory.
3839 3837 */
3840 3838 (void) zfs_link_create(dl, zp, tx, ZNEW);
3841 3839
3842 3840 if (flags & FIGNORECASE)
3843 3841 txtype |= TX_CI;
3844 3842 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link);
3845 3843
3846 3844 zfs_acl_ids_free(&acl_ids);
3847 3845
3848 3846 dmu_tx_commit(tx);
3849 3847
3850 3848 zfs_dirent_unlock(dl);
3851 3849
3852 3850 VN_RELE(ZTOV(zp));
3853 3851
3854 3852 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
3855 3853 zil_commit(zilog, 0);
3856 3854
3857 3855 ZFS_EXIT(zfsvfs);
3858 3856 return (error);
3859 3857 }
3860 3858
3861 3859 /*
3862 3860 * Return, in the buffer contained in the provided uio structure,
3863 3861 * the symbolic path referred to by vp.
3864 3862 *
3865 3863 * IN: vp - vnode of symbolic link.
3866 3864 * uio - structure to contain the link path.
3867 3865 * cr - credentials of caller.
3868 3866 * ct - caller context
3869 3867 *
3870 3868 * OUT: uio - structure containing the link path.
3871 3869 *
3872 3870 * RETURN: 0 on success, error code on failure.
3873 3871 *
3874 3872 * Timestamps:
3875 3873 * vp - atime updated
3876 3874 */
3877 3875 /* ARGSUSED */
3878 3876 static int
3879 3877 zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct)
3880 3878 {
3881 3879 znode_t *zp = VTOZ(vp);
3882 3880 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
3883 3881 int error;
3884 3882
3885 3883 ZFS_ENTER(zfsvfs);
3886 3884 ZFS_VERIFY_ZP(zp);
3887 3885
3888 3886 mutex_enter(&zp->z_lock);
3889 3887 if (zp->z_is_sa)
3890 3888 error = sa_lookup_uio(zp->z_sa_hdl,
3891 3889 SA_ZPL_SYMLINK(zfsvfs), uio);
3892 3890 else
3893 3891 error = zfs_sa_readlink(zp, uio);
3894 3892 mutex_exit(&zp->z_lock);
3895 3893
3896 3894 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
3897 3895
3898 3896 ZFS_EXIT(zfsvfs);
3899 3897 return (error);
3900 3898 }
3901 3899
3902 3900 /*
3903 3901 * Insert a new entry into directory tdvp referencing svp.
3904 3902 *
3905 3903 * IN: tdvp - Directory to contain new entry.
3906 3904 * svp - vnode of new entry.
3907 3905 * name - name of new entry.
3908 3906 * cr - credentials of caller.
3909 3907 * ct - caller context
3910 3908 *
3911 3909 * RETURN: 0 on success, error code on failure.
3912 3910 *
3913 3911 * Timestamps:
3914 3912 * tdvp - ctime|mtime updated
3915 3913 * svp - ctime updated
3916 3914 */
3917 3915 /* ARGSUSED */
3918 3916 static int
3919 3917 zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr,
3920 3918 caller_context_t *ct, int flags)
3921 3919 {
3922 3920 znode_t *dzp = VTOZ(tdvp);
3923 3921 znode_t *tzp, *szp;
3924 3922 zfsvfs_t *zfsvfs = dzp->z_zfsvfs;
3925 3923 zilog_t *zilog;
3926 3924 zfs_dirlock_t *dl;
3927 3925 dmu_tx_t *tx;
3928 3926 vnode_t *realvp;
3929 3927 int error;
3930 3928 int zf = ZNEW;
3931 3929 uint64_t parent;
3932 3930 uid_t owner;
3933 3931 boolean_t waited = B_FALSE;
3934 3932
3935 3933 ASSERT(tdvp->v_type == VDIR);
3936 3934
3937 3935 ZFS_ENTER(zfsvfs);
3938 3936 ZFS_VERIFY_ZP(dzp);
3939 3937 zilog = zfsvfs->z_log;
3940 3938
3941 3939 if (VOP_REALVP(svp, &realvp, ct) == 0)
3942 3940 svp = realvp;
3943 3941
3944 3942 /*
3945 3943 * POSIX dictates that we return EPERM here.
3946 3944 * Better choices include ENOTSUP or EISDIR.
3947 3945 */
3948 3946 if (svp->v_type == VDIR) {
3949 3947 ZFS_EXIT(zfsvfs);
3950 3948 return (SET_ERROR(EPERM));
3951 3949 }
3952 3950
3953 3951 szp = VTOZ(svp);
3954 3952 ZFS_VERIFY_ZP(szp);
3955 3953
3956 3954 /*
3957 3955 * We check z_zfsvfs rather than v_vfsp here, because snapshots and the
3958 3956 * ctldir appear to have the same v_vfsp.
3959 3957 */
3960 3958 if (szp->z_zfsvfs != zfsvfs || zfsctl_is_node(svp)) {
3961 3959 ZFS_EXIT(zfsvfs);
3962 3960 return (SET_ERROR(EXDEV));
3963 3961 }
3964 3962
3965 3963 /* Prevent links to .zfs/shares files */
3966 3964
3967 3965 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs),
3968 3966 &parent, sizeof (uint64_t))) != 0) {
3969 3967 ZFS_EXIT(zfsvfs);
3970 3968 return (error);
3971 3969 }
3972 3970 if (parent == zfsvfs->z_shares_dir) {
3973 3971 ZFS_EXIT(zfsvfs);
3974 3972 return (SET_ERROR(EPERM));
3975 3973 }
3976 3974
3977 3975 if (zfsvfs->z_utf8 && u8_validate(name,
3978 3976 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) {
3979 3977 ZFS_EXIT(zfsvfs);
3980 3978 return (SET_ERROR(EILSEQ));
3981 3979 }
3982 3980 if (flags & FIGNORECASE)
3983 3981 zf |= ZCILOOK;
3984 3982
3985 3983 /*
3986 3984 * We do not support links between attributes and non-attributes
3987 3985 * because of the potential security risk of creating links
3988 3986 * into "normal" file space in order to circumvent restrictions
3989 3987 * imposed in attribute space.
3990 3988 */
3991 3989 if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) {
3992 3990 ZFS_EXIT(zfsvfs);
3993 3991 return (SET_ERROR(EINVAL));
3994 3992 }
3995 3993
3996 3994
3997 3995 owner = zfs_fuid_map_id(zfsvfs, szp->z_uid, cr, ZFS_OWNER);
3998 3996 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) {
3999 3997 ZFS_EXIT(zfsvfs);
4000 3998 return (SET_ERROR(EPERM));
4001 3999 }
4002 4000
4003 4001 if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) {
4004 4002 ZFS_EXIT(zfsvfs);
4005 4003 return (error);
4006 4004 }
4007 4005
4008 4006 top:
4009 4007 /*
4010 4008 * Attempt to lock directory; fail if entry already exists.
4011 4009 */
4012 4010 error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL);
4013 4011 if (error) {
4014 4012 ZFS_EXIT(zfsvfs);
4015 4013 return (error);
4016 4014 }
4017 4015
4018 4016 tx = dmu_tx_create(zfsvfs->z_os);
4019 4017 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE);
4020 4018 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name);
4021 4019 zfs_sa_upgrade_txholds(tx, szp);
4022 4020 zfs_sa_upgrade_txholds(tx, dzp);
4023 4021 error = dmu_tx_assign(tx, waited ? TXG_WAITED : TXG_NOWAIT);
4024 4022 if (error) {
4025 4023 zfs_dirent_unlock(dl);
4026 4024 if (error == ERESTART) {
4027 4025 waited = B_TRUE;
4028 4026 dmu_tx_wait(tx);
4029 4027 dmu_tx_abort(tx);
4030 4028 goto top;
4031 4029 }
4032 4030 dmu_tx_abort(tx);
4033 4031 ZFS_EXIT(zfsvfs);
4034 4032 return (error);
4035 4033 }
4036 4034
4037 4035 error = zfs_link_create(dl, szp, tx, 0);
4038 4036
4039 4037 if (error == 0) {
4040 4038 uint64_t txtype = TX_LINK;
4041 4039 if (flags & FIGNORECASE)
4042 4040 txtype |= TX_CI;
4043 4041 zfs_log_link(zilog, tx, txtype, dzp, szp, name);
4044 4042 }
4045 4043
4046 4044 dmu_tx_commit(tx);
4047 4045
4048 4046 zfs_dirent_unlock(dl);
4049 4047
4050 4048 if (error == 0) {
4051 4049 vnevent_link(svp, ct);
4052 4050 }
4053 4051
4054 4052 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4055 4053 zil_commit(zilog, 0);
4056 4054
4057 4055 ZFS_EXIT(zfsvfs);
4058 4056 return (error);
4059 4057 }
4060 4058
4061 4059 /*
4062 4060 * zfs_null_putapage() is used when the file system has been force
4063 4061 * unmounted. It just drops the pages.
4064 4062 */
4065 4063 /* ARGSUSED */
4066 4064 static int
4067 4065 zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4068 4066 size_t *lenp, int flags, cred_t *cr)
4069 4067 {
4070 4068 pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR);
4071 4069 return (0);
4072 4070 }
4073 4071
4074 4072 /*
4075 4073 * Push a page out to disk, klustering if possible.
4076 4074 *
4077 4075 * IN: vp - file to push page to.
4078 4076 * pp - page to push.
4079 4077 * flags - additional flags.
4080 4078 * cr - credentials of caller.
4081 4079 *
4082 4080 * OUT: offp - start of range pushed.
4083 4081 * lenp - len of range pushed.
4084 4082 *
4085 4083 * RETURN: 0 on success, error code on failure.
4086 4084 *
4087 4085 * NOTE: callers must have locked the page to be pushed. On
4088 4086 * exit, the page (and all other pages in the kluster) must be
4089 4087 * unlocked.
4090 4088 */
4091 4089 /* ARGSUSED */
4092 4090 static int
4093 4091 zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
4094 4092 size_t *lenp, int flags, cred_t *cr)
4095 4093 {
4096 4094 znode_t *zp = VTOZ(vp);
4097 4095 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4098 4096 dmu_tx_t *tx;
4099 4097 u_offset_t off, koff;
4100 4098 size_t len, klen;
4101 4099 int err;
4102 4100
4103 4101 off = pp->p_offset;
4104 4102 len = PAGESIZE;
4105 4103 /*
4106 4104 * If our blocksize is bigger than the page size, try to kluster
4107 4105 * multiple pages so that we write a full block (thus avoiding
4108 4106 * a read-modify-write).
4109 4107 */
4110 4108 if (off < zp->z_size && zp->z_blksz > PAGESIZE) {
4111 4109 klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE);
4112 4110 koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0;
4113 4111 ASSERT(koff <= zp->z_size);
4114 4112 if (koff + klen > zp->z_size)
4115 4113 klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE);
4116 4114 pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags);
4117 4115 }
4118 4116 ASSERT3U(btop(len), ==, btopr(len));
4119 4117
4120 4118 /*
4121 4119 * Can't push pages past end-of-file.
4122 4120 */
4123 4121 if (off >= zp->z_size) {
4124 4122 /* ignore all pages */
4125 4123 err = 0;
4126 4124 goto out;
4127 4125 } else if (off + len > zp->z_size) {
4128 4126 int npages = btopr(zp->z_size - off);
4129 4127 page_t *trunc;
4130 4128
4131 4129 page_list_break(&pp, &trunc, npages);
4132 4130 /* ignore pages past end of file */
↓ open down ↓ |
1071 lines elided |
↑ open up ↑ |
4133 4131 if (trunc)
4134 4132 pvn_write_done(trunc, flags);
4135 4133 len = zp->z_size - off;
4136 4134 }
4137 4135
4138 4136 if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) ||
4139 4137 zfs_owner_overquota(zfsvfs, zp, B_TRUE)) {
4140 4138 err = SET_ERROR(EDQUOT);
4141 4139 goto out;
4142 4140 }
4143 -top:
4144 4141 tx = dmu_tx_create(zfsvfs->z_os);
4145 4142 dmu_tx_hold_write(tx, zp->z_id, off, len);
4146 4143
4147 4144 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4148 4145 zfs_sa_upgrade_txholds(tx, zp);
4149 - err = dmu_tx_assign(tx, TXG_NOWAIT);
4146 + err = dmu_tx_assign(tx, TXG_WAIT);
4150 4147 if (err != 0) {
4151 - if (err == ERESTART) {
4152 - dmu_tx_wait(tx);
4153 - dmu_tx_abort(tx);
4154 - goto top;
4155 - }
4156 4148 dmu_tx_abort(tx);
4157 4149 goto out;
4158 4150 }
4159 4151
4160 4152 if (zp->z_blksz <= PAGESIZE) {
4161 4153 caddr_t va = zfs_map_page(pp, S_READ);
4162 4154 ASSERT3U(len, <=, PAGESIZE);
4163 4155 dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx);
4164 4156 zfs_unmap_page(pp, va);
4165 4157 } else {
4166 4158 err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx);
4167 4159 }
4168 4160
4169 4161 if (err == 0) {
4170 4162 uint64_t mtime[2], ctime[2];
4171 4163 sa_bulk_attr_t bulk[3];
4172 4164 int count = 0;
4173 4165
4174 4166 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL,
4175 4167 &mtime, 16);
4176 4168 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL,
4177 4169 &ctime, 16);
4178 4170 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL,
4179 4171 &zp->z_pflags, 8);
4180 4172 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime,
4181 4173 B_TRUE);
4182 4174 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0);
4183 4175 }
4184 4176 dmu_tx_commit(tx);
4185 4177
4186 4178 out:
4187 4179 pvn_write_done(pp, (err ? B_ERROR : 0) | flags);
4188 4180 if (offp)
4189 4181 *offp = off;
4190 4182 if (lenp)
4191 4183 *lenp = len;
4192 4184
4193 4185 return (err);
4194 4186 }
4195 4187
4196 4188 /*
4197 4189 * Copy the portion of the file indicated from pages into the file.
4198 4190 * The pages are stored in a page list attached to the files vnode.
4199 4191 *
4200 4192 * IN: vp - vnode of file to push page data to.
4201 4193 * off - position in file to put data.
4202 4194 * len - amount of data to write.
4203 4195 * flags - flags to control the operation.
4204 4196 * cr - credentials of caller.
4205 4197 * ct - caller context.
4206 4198 *
4207 4199 * RETURN: 0 on success, error code on failure.
4208 4200 *
4209 4201 * Timestamps:
4210 4202 * vp - ctime|mtime updated
4211 4203 */
4212 4204 /*ARGSUSED*/
4213 4205 static int
4214 4206 zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr,
4215 4207 caller_context_t *ct)
4216 4208 {
4217 4209 znode_t *zp = VTOZ(vp);
4218 4210 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4219 4211 page_t *pp;
4220 4212 size_t io_len;
4221 4213 u_offset_t io_off;
4222 4214 uint_t blksz;
4223 4215 rl_t *rl;
4224 4216 int error = 0;
4225 4217
4226 4218 ZFS_ENTER(zfsvfs);
4227 4219 ZFS_VERIFY_ZP(zp);
4228 4220
4229 4221 /*
4230 4222 * There's nothing to do if no data is cached.
4231 4223 */
4232 4224 if (!vn_has_cached_data(vp)) {
4233 4225 ZFS_EXIT(zfsvfs);
4234 4226 return (0);
4235 4227 }
4236 4228
4237 4229 /*
4238 4230 * Align this request to the file block size in case we kluster.
4239 4231 * XXX - this can result in pretty aggresive locking, which can
4240 4232 * impact simultanious read/write access. One option might be
4241 4233 * to break up long requests (len == 0) into block-by-block
4242 4234 * operations to get narrower locking.
4243 4235 */
4244 4236 blksz = zp->z_blksz;
4245 4237 if (ISP2(blksz))
4246 4238 io_off = P2ALIGN_TYPED(off, blksz, u_offset_t);
4247 4239 else
4248 4240 io_off = 0;
4249 4241 if (len > 0 && ISP2(blksz))
4250 4242 io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t);
4251 4243 else
4252 4244 io_len = 0;
4253 4245
4254 4246 if (io_len == 0) {
4255 4247 /*
4256 4248 * Search the entire vp list for pages >= io_off.
4257 4249 */
4258 4250 rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER);
4259 4251 error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr);
4260 4252 goto out;
4261 4253 }
4262 4254 rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER);
4263 4255
4264 4256 if (off > zp->z_size) {
4265 4257 /* past end of file */
4266 4258 zfs_range_unlock(rl);
4267 4259 ZFS_EXIT(zfsvfs);
4268 4260 return (0);
4269 4261 }
4270 4262
4271 4263 len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off);
4272 4264
4273 4265 for (off = io_off; io_off < off + len; io_off += io_len) {
4274 4266 if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
4275 4267 pp = page_lookup(vp, io_off,
4276 4268 (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED);
4277 4269 } else {
4278 4270 pp = page_lookup_nowait(vp, io_off,
4279 4271 (flags & B_FREE) ? SE_EXCL : SE_SHARED);
4280 4272 }
4281 4273
4282 4274 if (pp != NULL && pvn_getdirty(pp, flags)) {
4283 4275 int err;
4284 4276
4285 4277 /*
4286 4278 * Found a dirty page to push
4287 4279 */
4288 4280 err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr);
4289 4281 if (err)
4290 4282 error = err;
4291 4283 } else {
4292 4284 io_len = PAGESIZE;
4293 4285 }
4294 4286 }
4295 4287 out:
4296 4288 zfs_range_unlock(rl);
4297 4289 if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4298 4290 zil_commit(zfsvfs->z_log, zp->z_id);
4299 4291 ZFS_EXIT(zfsvfs);
4300 4292 return (error);
4301 4293 }
4302 4294
4303 4295 /*ARGSUSED*/
4304 4296 void
4305 4297 zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct)
4306 4298 {
4307 4299 znode_t *zp = VTOZ(vp);
4308 4300 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4309 4301 int error;
4310 4302
4311 4303 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER);
4312 4304 if (zp->z_sa_hdl == NULL) {
4313 4305 /*
4314 4306 * The fs has been unmounted, or we did a
4315 4307 * suspend/resume and this file no longer exists.
4316 4308 */
4317 4309 if (vn_has_cached_data(vp)) {
4318 4310 (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage,
4319 4311 B_INVAL, cr);
4320 4312 }
4321 4313
4322 4314 mutex_enter(&zp->z_lock);
4323 4315 mutex_enter(&vp->v_lock);
4324 4316 ASSERT(vp->v_count == 1);
4325 4317 vp->v_count = 0;
4326 4318 mutex_exit(&vp->v_lock);
4327 4319 mutex_exit(&zp->z_lock);
4328 4320 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4329 4321 zfs_znode_free(zp);
4330 4322 return;
4331 4323 }
4332 4324
4333 4325 /*
4334 4326 * Attempt to push any data in the page cache. If this fails
4335 4327 * we will get kicked out later in zfs_zinactive().
4336 4328 */
4337 4329 if (vn_has_cached_data(vp)) {
4338 4330 (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC,
4339 4331 cr);
4340 4332 }
4341 4333
4342 4334 if (zp->z_atime_dirty && zp->z_unlinked == 0) {
4343 4335 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os);
4344 4336
4345 4337 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE);
4346 4338 zfs_sa_upgrade_txholds(tx, zp);
4347 4339 error = dmu_tx_assign(tx, TXG_WAIT);
4348 4340 if (error) {
4349 4341 dmu_tx_abort(tx);
4350 4342 } else {
4351 4343 mutex_enter(&zp->z_lock);
4352 4344 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs),
4353 4345 (void *)&zp->z_atime, sizeof (zp->z_atime), tx);
4354 4346 zp->z_atime_dirty = 0;
4355 4347 mutex_exit(&zp->z_lock);
4356 4348 dmu_tx_commit(tx);
4357 4349 }
4358 4350 }
4359 4351
4360 4352 zfs_zinactive(zp);
4361 4353 rw_exit(&zfsvfs->z_teardown_inactive_lock);
4362 4354 }
4363 4355
4364 4356 /*
4365 4357 * Bounds-check the seek operation.
4366 4358 *
4367 4359 * IN: vp - vnode seeking within
4368 4360 * ooff - old file offset
4369 4361 * noffp - pointer to new file offset
4370 4362 * ct - caller context
4371 4363 *
4372 4364 * RETURN: 0 on success, EINVAL if new offset invalid.
4373 4365 */
4374 4366 /* ARGSUSED */
4375 4367 static int
4376 4368 zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp,
4377 4369 caller_context_t *ct)
4378 4370 {
4379 4371 if (vp->v_type == VDIR)
4380 4372 return (0);
4381 4373 return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
4382 4374 }
4383 4375
4384 4376 /*
4385 4377 * Pre-filter the generic locking function to trap attempts to place
4386 4378 * a mandatory lock on a memory mapped file.
4387 4379 */
4388 4380 static int
4389 4381 zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset,
4390 4382 flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct)
4391 4383 {
4392 4384 znode_t *zp = VTOZ(vp);
4393 4385 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4394 4386
4395 4387 ZFS_ENTER(zfsvfs);
4396 4388 ZFS_VERIFY_ZP(zp);
4397 4389
4398 4390 /*
4399 4391 * We are following the UFS semantics with respect to mapcnt
4400 4392 * here: If we see that the file is mapped already, then we will
4401 4393 * return an error, but we don't worry about races between this
4402 4394 * function and zfs_map().
4403 4395 */
4404 4396 if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) {
4405 4397 ZFS_EXIT(zfsvfs);
4406 4398 return (SET_ERROR(EAGAIN));
4407 4399 }
4408 4400 ZFS_EXIT(zfsvfs);
4409 4401 return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct));
4410 4402 }
4411 4403
4412 4404 /*
4413 4405 * If we can't find a page in the cache, we will create a new page
4414 4406 * and fill it with file data. For efficiency, we may try to fill
4415 4407 * multiple pages at once (klustering) to fill up the supplied page
4416 4408 * list. Note that the pages to be filled are held with an exclusive
4417 4409 * lock to prevent access by other threads while they are being filled.
4418 4410 */
4419 4411 static int
4420 4412 zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg,
4421 4413 caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw)
4422 4414 {
4423 4415 znode_t *zp = VTOZ(vp);
4424 4416 page_t *pp, *cur_pp;
4425 4417 objset_t *os = zp->z_zfsvfs->z_os;
4426 4418 u_offset_t io_off, total;
4427 4419 size_t io_len;
4428 4420 int err;
4429 4421
4430 4422 if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) {
4431 4423 /*
4432 4424 * We only have a single page, don't bother klustering
4433 4425 */
4434 4426 io_off = off;
4435 4427 io_len = PAGESIZE;
4436 4428 pp = page_create_va(vp, io_off, io_len,
4437 4429 PG_EXCL | PG_WAIT, seg, addr);
4438 4430 } else {
4439 4431 /*
4440 4432 * Try to find enough pages to fill the page list
4441 4433 */
4442 4434 pp = pvn_read_kluster(vp, off, seg, addr, &io_off,
4443 4435 &io_len, off, plsz, 0);
4444 4436 }
4445 4437 if (pp == NULL) {
4446 4438 /*
4447 4439 * The page already exists, nothing to do here.
4448 4440 */
4449 4441 *pl = NULL;
4450 4442 return (0);
4451 4443 }
4452 4444
4453 4445 /*
4454 4446 * Fill the pages in the kluster.
4455 4447 */
4456 4448 cur_pp = pp;
4457 4449 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) {
4458 4450 caddr_t va;
4459 4451
4460 4452 ASSERT3U(io_off, ==, cur_pp->p_offset);
4461 4453 va = zfs_map_page(cur_pp, S_WRITE);
4462 4454 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va,
4463 4455 DMU_READ_PREFETCH);
4464 4456 zfs_unmap_page(cur_pp, va);
4465 4457 if (err) {
4466 4458 /* On error, toss the entire kluster */
4467 4459 pvn_read_done(pp, B_ERROR);
4468 4460 /* convert checksum errors into IO errors */
4469 4461 if (err == ECKSUM)
4470 4462 err = SET_ERROR(EIO);
4471 4463 return (err);
4472 4464 }
4473 4465 cur_pp = cur_pp->p_next;
4474 4466 }
4475 4467
4476 4468 /*
4477 4469 * Fill in the page list array from the kluster starting
4478 4470 * from the desired offset `off'.
4479 4471 * NOTE: the page list will always be null terminated.
4480 4472 */
4481 4473 pvn_plist_init(pp, pl, plsz, off, io_len, rw);
4482 4474 ASSERT(pl == NULL || (*pl)->p_offset == off);
4483 4475
4484 4476 return (0);
4485 4477 }
4486 4478
4487 4479 /*
4488 4480 * Return pointers to the pages for the file region [off, off + len]
4489 4481 * in the pl array. If plsz is greater than len, this function may
4490 4482 * also return page pointers from after the specified region
4491 4483 * (i.e. the region [off, off + plsz]). These additional pages are
4492 4484 * only returned if they are already in the cache, or were created as
4493 4485 * part of a klustered read.
4494 4486 *
4495 4487 * IN: vp - vnode of file to get data from.
4496 4488 * off - position in file to get data from.
4497 4489 * len - amount of data to retrieve.
4498 4490 * plsz - length of provided page list.
4499 4491 * seg - segment to obtain pages for.
4500 4492 * addr - virtual address of fault.
4501 4493 * rw - mode of created pages.
4502 4494 * cr - credentials of caller.
4503 4495 * ct - caller context.
4504 4496 *
4505 4497 * OUT: protp - protection mode of created pages.
4506 4498 * pl - list of pages created.
4507 4499 *
4508 4500 * RETURN: 0 on success, error code on failure.
4509 4501 *
4510 4502 * Timestamps:
4511 4503 * vp - atime updated
4512 4504 */
4513 4505 /* ARGSUSED */
4514 4506 static int
4515 4507 zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp,
4516 4508 page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
4517 4509 enum seg_rw rw, cred_t *cr, caller_context_t *ct)
4518 4510 {
4519 4511 znode_t *zp = VTOZ(vp);
4520 4512 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4521 4513 page_t **pl0 = pl;
4522 4514 int err = 0;
4523 4515
4524 4516 /* we do our own caching, faultahead is unnecessary */
4525 4517 if (pl == NULL)
4526 4518 return (0);
4527 4519 else if (len > plsz)
4528 4520 len = plsz;
4529 4521 else
4530 4522 len = P2ROUNDUP(len, PAGESIZE);
4531 4523 ASSERT(plsz >= len);
4532 4524
4533 4525 ZFS_ENTER(zfsvfs);
4534 4526 ZFS_VERIFY_ZP(zp);
4535 4527
4536 4528 if (protp)
4537 4529 *protp = PROT_ALL;
4538 4530
4539 4531 /*
4540 4532 * Loop through the requested range [off, off + len) looking
4541 4533 * for pages. If we don't find a page, we will need to create
4542 4534 * a new page and fill it with data from the file.
4543 4535 */
4544 4536 while (len > 0) {
4545 4537 if (*pl = page_lookup(vp, off, SE_SHARED))
4546 4538 *(pl+1) = NULL;
4547 4539 else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw))
4548 4540 goto out;
4549 4541 while (*pl) {
4550 4542 ASSERT3U((*pl)->p_offset, ==, off);
4551 4543 off += PAGESIZE;
4552 4544 addr += PAGESIZE;
4553 4545 if (len > 0) {
4554 4546 ASSERT3U(len, >=, PAGESIZE);
4555 4547 len -= PAGESIZE;
4556 4548 }
4557 4549 ASSERT3U(plsz, >=, PAGESIZE);
4558 4550 plsz -= PAGESIZE;
4559 4551 pl++;
4560 4552 }
4561 4553 }
4562 4554
4563 4555 /*
4564 4556 * Fill out the page array with any pages already in the cache.
4565 4557 */
4566 4558 while (plsz > 0 &&
4567 4559 (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) {
4568 4560 off += PAGESIZE;
4569 4561 plsz -= PAGESIZE;
4570 4562 }
4571 4563 out:
4572 4564 if (err) {
4573 4565 /*
4574 4566 * Release any pages we have previously locked.
4575 4567 */
4576 4568 while (pl > pl0)
4577 4569 page_unlock(*--pl);
4578 4570 } else {
4579 4571 ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
4580 4572 }
4581 4573
4582 4574 *pl = NULL;
4583 4575
4584 4576 ZFS_EXIT(zfsvfs);
4585 4577 return (err);
4586 4578 }
4587 4579
4588 4580 /*
4589 4581 * Request a memory map for a section of a file. This code interacts
4590 4582 * with common code and the VM system as follows:
4591 4583 *
4592 4584 * - common code calls mmap(), which ends up in smmap_common()
4593 4585 * - this calls VOP_MAP(), which takes you into (say) zfs
4594 4586 * - zfs_map() calls as_map(), passing segvn_create() as the callback
4595 4587 * - segvn_create() creates the new segment and calls VOP_ADDMAP()
4596 4588 * - zfs_addmap() updates z_mapcnt
4597 4589 */
4598 4590 /*ARGSUSED*/
4599 4591 static int
4600 4592 zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp,
4601 4593 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4602 4594 caller_context_t *ct)
4603 4595 {
4604 4596 znode_t *zp = VTOZ(vp);
4605 4597 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4606 4598 segvn_crargs_t vn_a;
4607 4599 int error;
4608 4600
4609 4601 ZFS_ENTER(zfsvfs);
4610 4602 ZFS_VERIFY_ZP(zp);
4611 4603
4612 4604 if ((prot & PROT_WRITE) && (zp->z_pflags &
4613 4605 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) {
4614 4606 ZFS_EXIT(zfsvfs);
4615 4607 return (SET_ERROR(EPERM));
4616 4608 }
4617 4609
4618 4610 if ((prot & (PROT_READ | PROT_EXEC)) &&
4619 4611 (zp->z_pflags & ZFS_AV_QUARANTINED)) {
4620 4612 ZFS_EXIT(zfsvfs);
4621 4613 return (SET_ERROR(EACCES));
4622 4614 }
4623 4615
4624 4616 if (vp->v_flag & VNOMAP) {
4625 4617 ZFS_EXIT(zfsvfs);
4626 4618 return (SET_ERROR(ENOSYS));
4627 4619 }
4628 4620
4629 4621 if (off < 0 || len > MAXOFFSET_T - off) {
4630 4622 ZFS_EXIT(zfsvfs);
4631 4623 return (SET_ERROR(ENXIO));
4632 4624 }
4633 4625
4634 4626 if (vp->v_type != VREG) {
4635 4627 ZFS_EXIT(zfsvfs);
4636 4628 return (SET_ERROR(ENODEV));
4637 4629 }
4638 4630
4639 4631 /*
4640 4632 * If file is locked, disallow mapping.
4641 4633 */
4642 4634 if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) {
4643 4635 ZFS_EXIT(zfsvfs);
4644 4636 return (SET_ERROR(EAGAIN));
4645 4637 }
4646 4638
4647 4639 as_rangelock(as);
4648 4640 error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
4649 4641 if (error != 0) {
4650 4642 as_rangeunlock(as);
4651 4643 ZFS_EXIT(zfsvfs);
4652 4644 return (error);
4653 4645 }
4654 4646
4655 4647 vn_a.vp = vp;
4656 4648 vn_a.offset = (u_offset_t)off;
4657 4649 vn_a.type = flags & MAP_TYPE;
4658 4650 vn_a.prot = prot;
4659 4651 vn_a.maxprot = maxprot;
4660 4652 vn_a.cred = cr;
4661 4653 vn_a.amp = NULL;
4662 4654 vn_a.flags = flags & ~MAP_TYPE;
4663 4655 vn_a.szc = 0;
4664 4656 vn_a.lgrp_mem_policy_flags = 0;
4665 4657
4666 4658 error = as_map(as, *addrp, len, segvn_create, &vn_a);
4667 4659
4668 4660 as_rangeunlock(as);
4669 4661 ZFS_EXIT(zfsvfs);
4670 4662 return (error);
4671 4663 }
4672 4664
4673 4665 /* ARGSUSED */
4674 4666 static int
4675 4667 zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4676 4668 size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr,
4677 4669 caller_context_t *ct)
4678 4670 {
4679 4671 uint64_t pages = btopr(len);
4680 4672
4681 4673 atomic_add_64(&VTOZ(vp)->z_mapcnt, pages);
4682 4674 return (0);
4683 4675 }
4684 4676
4685 4677 /*
4686 4678 * The reason we push dirty pages as part of zfs_delmap() is so that we get a
4687 4679 * more accurate mtime for the associated file. Since we don't have a way of
4688 4680 * detecting when the data was actually modified, we have to resort to
4689 4681 * heuristics. If an explicit msync() is done, then we mark the mtime when the
4690 4682 * last page is pushed. The problem occurs when the msync() call is omitted,
4691 4683 * which by far the most common case:
4692 4684 *
4693 4685 * open()
4694 4686 * mmap()
4695 4687 * <modify memory>
4696 4688 * munmap()
4697 4689 * close()
4698 4690 * <time lapse>
4699 4691 * putpage() via fsflush
4700 4692 *
4701 4693 * If we wait until fsflush to come along, we can have a modification time that
4702 4694 * is some arbitrary point in the future. In order to prevent this in the
4703 4695 * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is
4704 4696 * torn down.
4705 4697 */
4706 4698 /* ARGSUSED */
4707 4699 static int
4708 4700 zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr,
4709 4701 size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr,
4710 4702 caller_context_t *ct)
4711 4703 {
4712 4704 uint64_t pages = btopr(len);
4713 4705
4714 4706 ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages);
4715 4707 atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages);
4716 4708
4717 4709 if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
4718 4710 vn_has_cached_data(vp))
4719 4711 (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);
4720 4712
4721 4713 return (0);
4722 4714 }
4723 4715
4724 4716 /*
4725 4717 * Free or allocate space in a file. Currently, this function only
4726 4718 * supports the `F_FREESP' command. However, this command is somewhat
4727 4719 * misnamed, as its functionality includes the ability to allocate as
4728 4720 * well as free space.
4729 4721 *
4730 4722 * IN: vp - vnode of file to free data in.
4731 4723 * cmd - action to take (only F_FREESP supported).
4732 4724 * bfp - section of file to free/alloc.
4733 4725 * flag - current file open mode flags.
4734 4726 * offset - current file offset.
4735 4727 * cr - credentials of caller [UNUSED].
4736 4728 * ct - caller context.
4737 4729 *
4738 4730 * RETURN: 0 on success, error code on failure.
4739 4731 *
4740 4732 * Timestamps:
4741 4733 * vp - ctime|mtime updated
4742 4734 */
4743 4735 /* ARGSUSED */
4744 4736 static int
4745 4737 zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag,
4746 4738 offset_t offset, cred_t *cr, caller_context_t *ct)
4747 4739 {
4748 4740 znode_t *zp = VTOZ(vp);
4749 4741 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4750 4742 uint64_t off, len;
4751 4743 int error;
4752 4744
4753 4745 ZFS_ENTER(zfsvfs);
4754 4746 ZFS_VERIFY_ZP(zp);
4755 4747
4756 4748 if (cmd != F_FREESP) {
4757 4749 ZFS_EXIT(zfsvfs);
4758 4750 return (SET_ERROR(EINVAL));
4759 4751 }
4760 4752
4761 4753 if (error = convoff(vp, bfp, 0, offset)) {
4762 4754 ZFS_EXIT(zfsvfs);
4763 4755 return (error);
4764 4756 }
4765 4757
4766 4758 if (bfp->l_len < 0) {
4767 4759 ZFS_EXIT(zfsvfs);
4768 4760 return (SET_ERROR(EINVAL));
4769 4761 }
4770 4762
4771 4763 off = bfp->l_start;
4772 4764 len = bfp->l_len; /* 0 means from off to end of file */
4773 4765
4774 4766 error = zfs_freesp(zp, off, len, flag, TRUE);
4775 4767
4776 4768 if (error == 0 && off == 0 && len == 0)
4777 4769 vnevent_truncate(ZTOV(zp), ct);
4778 4770
4779 4771 ZFS_EXIT(zfsvfs);
4780 4772 return (error);
4781 4773 }
4782 4774
4783 4775 /*ARGSUSED*/
4784 4776 static int
4785 4777 zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct)
4786 4778 {
4787 4779 znode_t *zp = VTOZ(vp);
4788 4780 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4789 4781 uint32_t gen;
4790 4782 uint64_t gen64;
4791 4783 uint64_t object = zp->z_id;
4792 4784 zfid_short_t *zfid;
4793 4785 int size, i, error;
4794 4786
4795 4787 ZFS_ENTER(zfsvfs);
4796 4788 ZFS_VERIFY_ZP(zp);
4797 4789
4798 4790 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs),
4799 4791 &gen64, sizeof (uint64_t))) != 0) {
4800 4792 ZFS_EXIT(zfsvfs);
4801 4793 return (error);
4802 4794 }
4803 4795
4804 4796 gen = (uint32_t)gen64;
4805 4797
4806 4798 size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN;
4807 4799 if (fidp->fid_len < size) {
4808 4800 fidp->fid_len = size;
4809 4801 ZFS_EXIT(zfsvfs);
4810 4802 return (SET_ERROR(ENOSPC));
4811 4803 }
4812 4804
4813 4805 zfid = (zfid_short_t *)fidp;
4814 4806
4815 4807 zfid->zf_len = size;
4816 4808
4817 4809 for (i = 0; i < sizeof (zfid->zf_object); i++)
4818 4810 zfid->zf_object[i] = (uint8_t)(object >> (8 * i));
4819 4811
4820 4812 /* Must have a non-zero generation number to distinguish from .zfs */
4821 4813 if (gen == 0)
4822 4814 gen = 1;
4823 4815 for (i = 0; i < sizeof (zfid->zf_gen); i++)
4824 4816 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i));
4825 4817
4826 4818 if (size == LONG_FID_LEN) {
4827 4819 uint64_t objsetid = dmu_objset_id(zfsvfs->z_os);
4828 4820 zfid_long_t *zlfid;
4829 4821
4830 4822 zlfid = (zfid_long_t *)fidp;
4831 4823
4832 4824 for (i = 0; i < sizeof (zlfid->zf_setid); i++)
4833 4825 zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i));
4834 4826
4835 4827 /* XXX - this should be the generation number for the objset */
4836 4828 for (i = 0; i < sizeof (zlfid->zf_setgen); i++)
4837 4829 zlfid->zf_setgen[i] = 0;
4838 4830 }
4839 4831
4840 4832 ZFS_EXIT(zfsvfs);
4841 4833 return (0);
4842 4834 }
4843 4835
4844 4836 static int
4845 4837 zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
4846 4838 caller_context_t *ct)
4847 4839 {
4848 4840 znode_t *zp, *xzp;
4849 4841 zfsvfs_t *zfsvfs;
4850 4842 zfs_dirlock_t *dl;
4851 4843 int error;
4852 4844
4853 4845 switch (cmd) {
4854 4846 case _PC_LINK_MAX:
4855 4847 *valp = ULONG_MAX;
4856 4848 return (0);
4857 4849
4858 4850 case _PC_FILESIZEBITS:
4859 4851 *valp = 64;
4860 4852 return (0);
4861 4853
4862 4854 case _PC_XATTR_EXISTS:
4863 4855 zp = VTOZ(vp);
4864 4856 zfsvfs = zp->z_zfsvfs;
4865 4857 ZFS_ENTER(zfsvfs);
4866 4858 ZFS_VERIFY_ZP(zp);
4867 4859 *valp = 0;
4868 4860 error = zfs_dirent_lock(&dl, zp, "", &xzp,
4869 4861 ZXATTR | ZEXISTS | ZSHARED, NULL, NULL);
4870 4862 if (error == 0) {
4871 4863 zfs_dirent_unlock(dl);
4872 4864 if (!zfs_dirempty(xzp))
4873 4865 *valp = 1;
4874 4866 VN_RELE(ZTOV(xzp));
4875 4867 } else if (error == ENOENT) {
4876 4868 /*
4877 4869 * If there aren't extended attributes, it's the
4878 4870 * same as having zero of them.
4879 4871 */
4880 4872 error = 0;
4881 4873 }
4882 4874 ZFS_EXIT(zfsvfs);
4883 4875 return (error);
4884 4876
4885 4877 case _PC_SATTR_ENABLED:
4886 4878 case _PC_SATTR_EXISTS:
4887 4879 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) &&
4888 4880 (vp->v_type == VREG || vp->v_type == VDIR);
4889 4881 return (0);
4890 4882
4891 4883 case _PC_ACCESS_FILTERING:
4892 4884 *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) &&
4893 4885 vp->v_type == VDIR;
4894 4886 return (0);
4895 4887
4896 4888 case _PC_ACL_ENABLED:
4897 4889 *valp = _ACL_ACE_ENABLED;
4898 4890 return (0);
4899 4891
4900 4892 case _PC_MIN_HOLE_SIZE:
4901 4893 *valp = (ulong_t)SPA_MINBLOCKSIZE;
4902 4894 return (0);
4903 4895
4904 4896 case _PC_TIMESTAMP_RESOLUTION:
4905 4897 /* nanosecond timestamp resolution */
4906 4898 *valp = 1L;
4907 4899 return (0);
4908 4900
4909 4901 default:
4910 4902 return (fs_pathconf(vp, cmd, valp, cr, ct));
4911 4903 }
4912 4904 }
4913 4905
4914 4906 /*ARGSUSED*/
4915 4907 static int
4916 4908 zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4917 4909 caller_context_t *ct)
4918 4910 {
4919 4911 znode_t *zp = VTOZ(vp);
4920 4912 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4921 4913 int error;
4922 4914 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4923 4915
4924 4916 ZFS_ENTER(zfsvfs);
4925 4917 ZFS_VERIFY_ZP(zp);
4926 4918 error = zfs_getacl(zp, vsecp, skipaclchk, cr);
4927 4919 ZFS_EXIT(zfsvfs);
4928 4920
4929 4921 return (error);
4930 4922 }
4931 4923
4932 4924 /*ARGSUSED*/
4933 4925 static int
4934 4926 zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr,
4935 4927 caller_context_t *ct)
4936 4928 {
4937 4929 znode_t *zp = VTOZ(vp);
4938 4930 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4939 4931 int error;
4940 4932 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
4941 4933 zilog_t *zilog = zfsvfs->z_log;
4942 4934
4943 4935 ZFS_ENTER(zfsvfs);
4944 4936 ZFS_VERIFY_ZP(zp);
4945 4937
4946 4938 error = zfs_setacl(zp, vsecp, skipaclchk, cr);
4947 4939
4948 4940 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
4949 4941 zil_commit(zilog, 0);
4950 4942
4951 4943 ZFS_EXIT(zfsvfs);
4952 4944 return (error);
4953 4945 }
4954 4946
4955 4947 /*
4956 4948 * The smallest read we may consider to loan out an arcbuf.
4957 4949 * This must be a power of 2.
4958 4950 */
4959 4951 int zcr_blksz_min = (1 << 10); /* 1K */
4960 4952 /*
4961 4953 * If set to less than the file block size, allow loaning out of an
4962 4954 * arcbuf for a partial block read. This must be a power of 2.
4963 4955 */
4964 4956 int zcr_blksz_max = (1 << 17); /* 128K */
4965 4957
4966 4958 /*ARGSUSED*/
4967 4959 static int
4968 4960 zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr,
4969 4961 caller_context_t *ct)
4970 4962 {
4971 4963 znode_t *zp = VTOZ(vp);
4972 4964 zfsvfs_t *zfsvfs = zp->z_zfsvfs;
4973 4965 int max_blksz = zfsvfs->z_max_blksz;
4974 4966 uio_t *uio = &xuio->xu_uio;
4975 4967 ssize_t size = uio->uio_resid;
4976 4968 offset_t offset = uio->uio_loffset;
4977 4969 int blksz;
4978 4970 int fullblk, i;
4979 4971 arc_buf_t *abuf;
4980 4972 ssize_t maxsize;
4981 4973 int preamble, postamble;
4982 4974
4983 4975 if (xuio->xu_type != UIOTYPE_ZEROCOPY)
4984 4976 return (SET_ERROR(EINVAL));
4985 4977
4986 4978 ZFS_ENTER(zfsvfs);
4987 4979 ZFS_VERIFY_ZP(zp);
4988 4980 switch (ioflag) {
4989 4981 case UIO_WRITE:
4990 4982 /*
4991 4983 * Loan out an arc_buf for write if write size is bigger than
4992 4984 * max_blksz, and the file's block size is also max_blksz.
4993 4985 */
4994 4986 blksz = max_blksz;
4995 4987 if (size < blksz || zp->z_blksz != blksz) {
4996 4988 ZFS_EXIT(zfsvfs);
4997 4989 return (SET_ERROR(EINVAL));
4998 4990 }
4999 4991 /*
5000 4992 * Caller requests buffers for write before knowing where the
5001 4993 * write offset might be (e.g. NFS TCP write).
5002 4994 */
5003 4995 if (offset == -1) {
5004 4996 preamble = 0;
5005 4997 } else {
5006 4998 preamble = P2PHASE(offset, blksz);
5007 4999 if (preamble) {
5008 5000 preamble = blksz - preamble;
5009 5001 size -= preamble;
5010 5002 }
5011 5003 }
5012 5004
5013 5005 postamble = P2PHASE(size, blksz);
5014 5006 size -= postamble;
5015 5007
5016 5008 fullblk = size / blksz;
5017 5009 (void) dmu_xuio_init(xuio,
5018 5010 (preamble != 0) + fullblk + (postamble != 0));
5019 5011 DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble,
5020 5012 int, postamble, int,
5021 5013 (preamble != 0) + fullblk + (postamble != 0));
5022 5014
5023 5015 /*
5024 5016 * Have to fix iov base/len for partial buffers. They
5025 5017 * currently represent full arc_buf's.
5026 5018 */
5027 5019 if (preamble) {
5028 5020 /* data begins in the middle of the arc_buf */
5029 5021 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5030 5022 blksz);
5031 5023 ASSERT(abuf);
5032 5024 (void) dmu_xuio_add(xuio, abuf,
5033 5025 blksz - preamble, preamble);
5034 5026 }
5035 5027
5036 5028 for (i = 0; i < fullblk; i++) {
5037 5029 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5038 5030 blksz);
5039 5031 ASSERT(abuf);
5040 5032 (void) dmu_xuio_add(xuio, abuf, 0, blksz);
5041 5033 }
5042 5034
5043 5035 if (postamble) {
5044 5036 /* data ends in the middle of the arc_buf */
5045 5037 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl),
5046 5038 blksz);
5047 5039 ASSERT(abuf);
5048 5040 (void) dmu_xuio_add(xuio, abuf, 0, postamble);
5049 5041 }
5050 5042 break;
5051 5043 case UIO_READ:
5052 5044 /*
5053 5045 * Loan out an arc_buf for read if the read size is larger than
5054 5046 * the current file block size. Block alignment is not
5055 5047 * considered. Partial arc_buf will be loaned out for read.
5056 5048 */
5057 5049 blksz = zp->z_blksz;
5058 5050 if (blksz < zcr_blksz_min)
5059 5051 blksz = zcr_blksz_min;
5060 5052 if (blksz > zcr_blksz_max)
5061 5053 blksz = zcr_blksz_max;
5062 5054 /* avoid potential complexity of dealing with it */
5063 5055 if (blksz > max_blksz) {
5064 5056 ZFS_EXIT(zfsvfs);
5065 5057 return (SET_ERROR(EINVAL));
5066 5058 }
5067 5059
5068 5060 maxsize = zp->z_size - uio->uio_loffset;
5069 5061 if (size > maxsize)
5070 5062 size = maxsize;
5071 5063
5072 5064 if (size < blksz || vn_has_cached_data(vp)) {
5073 5065 ZFS_EXIT(zfsvfs);
5074 5066 return (SET_ERROR(EINVAL));
5075 5067 }
5076 5068 break;
5077 5069 default:
5078 5070 ZFS_EXIT(zfsvfs);
5079 5071 return (SET_ERROR(EINVAL));
5080 5072 }
5081 5073
5082 5074 uio->uio_extflg = UIO_XUIO;
5083 5075 XUIO_XUZC_RW(xuio) = ioflag;
5084 5076 ZFS_EXIT(zfsvfs);
5085 5077 return (0);
5086 5078 }
5087 5079
5088 5080 /*ARGSUSED*/
5089 5081 static int
5090 5082 zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct)
5091 5083 {
5092 5084 int i;
5093 5085 arc_buf_t *abuf;
5094 5086 int ioflag = XUIO_XUZC_RW(xuio);
5095 5087
5096 5088 ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY);
5097 5089
5098 5090 i = dmu_xuio_cnt(xuio);
5099 5091 while (i-- > 0) {
5100 5092 abuf = dmu_xuio_arcbuf(xuio, i);
5101 5093 /*
5102 5094 * if abuf == NULL, it must be a write buffer
5103 5095 * that has been returned in zfs_write().
5104 5096 */
5105 5097 if (abuf)
5106 5098 dmu_return_arcbuf(abuf);
5107 5099 ASSERT(abuf || ioflag == UIO_WRITE);
5108 5100 }
5109 5101
5110 5102 dmu_xuio_fini(xuio);
5111 5103 return (0);
5112 5104 }
5113 5105
5114 5106 /*
5115 5107 * Predeclare these here so that the compiler assumes that
5116 5108 * this is an "old style" function declaration that does
5117 5109 * not include arguments => we won't get type mismatch errors
5118 5110 * in the initializations that follow.
5119 5111 */
5120 5112 static int zfs_inval();
5121 5113 static int zfs_isdir();
5122 5114
5123 5115 static int
5124 5116 zfs_inval()
5125 5117 {
5126 5118 return (SET_ERROR(EINVAL));
5127 5119 }
5128 5120
5129 5121 static int
5130 5122 zfs_isdir()
5131 5123 {
5132 5124 return (SET_ERROR(EISDIR));
5133 5125 }
5134 5126 /*
5135 5127 * Directory vnode operations template
5136 5128 */
5137 5129 vnodeops_t *zfs_dvnodeops;
5138 5130 const fs_operation_def_t zfs_dvnodeops_template[] = {
5139 5131 VOPNAME_OPEN, { .vop_open = zfs_open },
5140 5132 VOPNAME_CLOSE, { .vop_close = zfs_close },
5141 5133 VOPNAME_READ, { .error = zfs_isdir },
5142 5134 VOPNAME_WRITE, { .error = zfs_isdir },
5143 5135 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5144 5136 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5145 5137 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5146 5138 VOPNAME_ACCESS, { .vop_access = zfs_access },
5147 5139 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5148 5140 VOPNAME_CREATE, { .vop_create = zfs_create },
5149 5141 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5150 5142 VOPNAME_LINK, { .vop_link = zfs_link },
5151 5143 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5152 5144 VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir },
5153 5145 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5154 5146 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5155 5147 VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink },
5156 5148 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5157 5149 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5158 5150 VOPNAME_FID, { .vop_fid = zfs_fid },
5159 5151 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5160 5152 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5161 5153 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5162 5154 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5163 5155 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5164 5156 NULL, NULL
5165 5157 };
5166 5158
5167 5159 /*
5168 5160 * Regular file vnode operations template
5169 5161 */
5170 5162 vnodeops_t *zfs_fvnodeops;
5171 5163 const fs_operation_def_t zfs_fvnodeops_template[] = {
5172 5164 VOPNAME_OPEN, { .vop_open = zfs_open },
5173 5165 VOPNAME_CLOSE, { .vop_close = zfs_close },
5174 5166 VOPNAME_READ, { .vop_read = zfs_read },
5175 5167 VOPNAME_WRITE, { .vop_write = zfs_write },
5176 5168 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5177 5169 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5178 5170 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5179 5171 VOPNAME_ACCESS, { .vop_access = zfs_access },
5180 5172 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5181 5173 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5182 5174 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5183 5175 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5184 5176 VOPNAME_FID, { .vop_fid = zfs_fid },
5185 5177 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5186 5178 VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock },
5187 5179 VOPNAME_SPACE, { .vop_space = zfs_space },
5188 5180 VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage },
5189 5181 VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage },
5190 5182 VOPNAME_MAP, { .vop_map = zfs_map },
5191 5183 VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap },
5192 5184 VOPNAME_DELMAP, { .vop_delmap = zfs_delmap },
5193 5185 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5194 5186 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5195 5187 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5196 5188 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5197 5189 VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf },
5198 5190 VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf },
5199 5191 NULL, NULL
5200 5192 };
5201 5193
5202 5194 /*
5203 5195 * Symbolic link vnode operations template
5204 5196 */
5205 5197 vnodeops_t *zfs_symvnodeops;
5206 5198 const fs_operation_def_t zfs_symvnodeops_template[] = {
5207 5199 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5208 5200 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5209 5201 VOPNAME_ACCESS, { .vop_access = zfs_access },
5210 5202 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5211 5203 VOPNAME_READLINK, { .vop_readlink = zfs_readlink },
5212 5204 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5213 5205 VOPNAME_FID, { .vop_fid = zfs_fid },
5214 5206 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5215 5207 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5216 5208 NULL, NULL
5217 5209 };
5218 5210
5219 5211 /*
5220 5212 * special share hidden files vnode operations template
5221 5213 */
5222 5214 vnodeops_t *zfs_sharevnodeops;
5223 5215 const fs_operation_def_t zfs_sharevnodeops_template[] = {
5224 5216 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5225 5217 VOPNAME_ACCESS, { .vop_access = zfs_access },
5226 5218 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5227 5219 VOPNAME_FID, { .vop_fid = zfs_fid },
5228 5220 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5229 5221 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5230 5222 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5231 5223 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5232 5224 NULL, NULL
5233 5225 };
5234 5226
5235 5227 /*
5236 5228 * Extended attribute directory vnode operations template
5237 5229 *
5238 5230 * This template is identical to the directory vnodes
5239 5231 * operation template except for restricted operations:
5240 5232 * VOP_MKDIR()
5241 5233 * VOP_SYMLINK()
5242 5234 *
5243 5235 * Note that there are other restrictions embedded in:
5244 5236 * zfs_create() - restrict type to VREG
5245 5237 * zfs_link() - no links into/out of attribute space
5246 5238 * zfs_rename() - no moves into/out of attribute space
5247 5239 */
5248 5240 vnodeops_t *zfs_xdvnodeops;
5249 5241 const fs_operation_def_t zfs_xdvnodeops_template[] = {
5250 5242 VOPNAME_OPEN, { .vop_open = zfs_open },
5251 5243 VOPNAME_CLOSE, { .vop_close = zfs_close },
5252 5244 VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl },
5253 5245 VOPNAME_GETATTR, { .vop_getattr = zfs_getattr },
5254 5246 VOPNAME_SETATTR, { .vop_setattr = zfs_setattr },
5255 5247 VOPNAME_ACCESS, { .vop_access = zfs_access },
5256 5248 VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup },
5257 5249 VOPNAME_CREATE, { .vop_create = zfs_create },
5258 5250 VOPNAME_REMOVE, { .vop_remove = zfs_remove },
5259 5251 VOPNAME_LINK, { .vop_link = zfs_link },
5260 5252 VOPNAME_RENAME, { .vop_rename = zfs_rename },
5261 5253 VOPNAME_MKDIR, { .error = zfs_inval },
5262 5254 VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir },
5263 5255 VOPNAME_READDIR, { .vop_readdir = zfs_readdir },
5264 5256 VOPNAME_SYMLINK, { .error = zfs_inval },
5265 5257 VOPNAME_FSYNC, { .vop_fsync = zfs_fsync },
5266 5258 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5267 5259 VOPNAME_FID, { .vop_fid = zfs_fid },
5268 5260 VOPNAME_SEEK, { .vop_seek = zfs_seek },
5269 5261 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5270 5262 VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr },
5271 5263 VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr },
5272 5264 VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support },
5273 5265 NULL, NULL
5274 5266 };
5275 5267
5276 5268 /*
5277 5269 * Error vnode operations template
5278 5270 */
5279 5271 vnodeops_t *zfs_evnodeops;
5280 5272 const fs_operation_def_t zfs_evnodeops_template[] = {
5281 5273 VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive },
5282 5274 VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf },
5283 5275 NULL, NULL
5284 5276 };
↓ open down ↓ |
1119 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX