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