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