]>
Commit | Line | Data |
---|---|---|
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 | 174 | static void |
c0d35759 BB |
175 | update_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 | */ | |
223 | static int | |
3558fd73 | 224 | mappedread(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 | |
268 | offset_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 | 289 | int |
3558fd73 | 290 | zfs_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 |
412 | out: |
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 | 420 | EXPORT_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 | 442 | int |
3558fd73 | 443 | zfs_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 | 584 | again: |
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 | 795 | EXPORT_SYMBOL(zfs_write); |
34dc7c2f | 796 | |
3558fd73 BB |
797 | static void |
798 | iput_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 | 807 | void |
428870ff | 808 | zfs_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 |
831 | static int zil_fault_io = 0; | |
832 | #endif | |
833 | ||
34dc7c2f BB |
834 | /* |
835 | * Get data to generate a TX_WRITE intent log record. | |
836 | */ | |
837 | int | |
838 | zfs_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 |
954 | int |
955 | zfs_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 | 972 | EXPORT_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 | 994 | int |
3558fd73 BB |
995 | zfs_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 | 1110 | EXPORT_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 | 1137 | int |
3558fd73 BB |
1138 | zfs_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 | ||
5484965a | 1177 | if (vap->va_mask & ATTR_XVATTR) { |
34dc7c2f | 1178 | if ((error = secpolicy_xvattr((xvattr_t *)vap, |
3558fd73 BB |
1179 | crgetuid(cr), cr, vap->va_mode)) != 0) { |
1180 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
1181 | return (error); |
1182 | } | |
1183 | } | |
34dc7c2f | 1184 | |
3558fd73 BB |
1185 | top: |
1186 | *ipp = NULL; | |
34dc7c2f BB |
1187 | if (*name == '\0') { |
1188 | /* | |
1189 | * Null component name refers to the directory itself. | |
1190 | */ | |
3558fd73 | 1191 | igrab(dip); |
34dc7c2f BB |
1192 | zp = dzp; |
1193 | dl = NULL; | |
1194 | error = 0; | |
1195 | } else { | |
3558fd73 | 1196 | /* possible igrab(zp) */ |
34dc7c2f BB |
1197 | int zflg = 0; |
1198 | ||
1199 | if (flag & FIGNORECASE) | |
1200 | zflg |= ZCILOOK; | |
1201 | ||
1202 | error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, | |
1203 | NULL, NULL); | |
1204 | if (error) { | |
572e2857 BB |
1205 | if (have_acl) |
1206 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1207 | if (strcmp(name, "..") == 0) |
1208 | error = EISDIR; | |
3558fd73 | 1209 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1210 | return (error); |
1211 | } | |
1212 | } | |
428870ff | 1213 | |
34dc7c2f BB |
1214 | if (zp == NULL) { |
1215 | uint64_t txtype; | |
1216 | ||
1217 | /* | |
1218 | * Create a new file object and update the directory | |
1219 | * to reference it. | |
1220 | */ | |
149e873a | 1221 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
572e2857 BB |
1222 | if (have_acl) |
1223 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1224 | goto out; |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | * We only support the creation of regular files in | |
1229 | * extended attribute directories. | |
1230 | */ | |
428870ff | 1231 | |
3558fd73 | 1232 | if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) { |
572e2857 BB |
1233 | if (have_acl) |
1234 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1235 | error = EINVAL; |
1236 | goto out; | |
1237 | } | |
1238 | ||
428870ff BB |
1239 | if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, |
1240 | cr, vsecp, &acl_ids)) != 0) | |
9babb374 | 1241 | goto out; |
428870ff BB |
1242 | have_acl = B_TRUE; |
1243 | ||
3558fd73 | 1244 | if (zfs_acl_ids_overquota(zsb, &acl_ids)) { |
45d1cae3 | 1245 | zfs_acl_ids_free(&acl_ids); |
9babb374 BB |
1246 | error = EDQUOT; |
1247 | goto out; | |
1248 | } | |
1249 | ||
34dc7c2f | 1250 | tx = dmu_tx_create(os); |
428870ff BB |
1251 | |
1252 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + | |
1253 | ZFS_SA_BASE_ATTR_SIZE); | |
1254 | ||
3558fd73 | 1255 | fuid_dirtied = zsb->z_fuid_dirty; |
9babb374 | 1256 | if (fuid_dirtied) |
3558fd73 | 1257 | zfs_fuid_txhold(zsb, tx); |
34dc7c2f | 1258 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
428870ff | 1259 | dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); |
3558fd73 | 1260 | if (!zsb->z_use_sa && |
428870ff | 1261 | acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
34dc7c2f | 1262 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, |
428870ff | 1263 | 0, acl_ids.z_aclp->z_acl_bytes); |
34dc7c2f | 1264 | } |
fb5f0bc8 | 1265 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
1266 | if (error) { |
1267 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 1268 | if (error == ERESTART) { |
34dc7c2f BB |
1269 | dmu_tx_wait(tx); |
1270 | dmu_tx_abort(tx); | |
1271 | goto top; | |
1272 | } | |
428870ff | 1273 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 1274 | dmu_tx_abort(tx); |
3558fd73 | 1275 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1276 | return (error); |
1277 | } | |
428870ff | 1278 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
9babb374 BB |
1279 | |
1280 | if (fuid_dirtied) | |
3558fd73 | 1281 | zfs_fuid_sync(zsb, tx); |
9babb374 | 1282 | |
34dc7c2f BB |
1283 | (void) zfs_link_create(dl, zp, tx, ZNEW); |
1284 | txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); | |
1285 | if (flag & FIGNORECASE) | |
1286 | txtype |= TX_CI; | |
1287 | zfs_log_create(zilog, tx, txtype, dzp, zp, name, | |
9babb374 BB |
1288 | vsecp, acl_ids.z_fuidp, vap); |
1289 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1290 | dmu_tx_commit(tx); |
1291 | } else { | |
1292 | int aflags = (flag & FAPPEND) ? V_APPEND : 0; | |
1293 | ||
572e2857 BB |
1294 | if (have_acl) |
1295 | zfs_acl_ids_free(&acl_ids); | |
1296 | have_acl = B_FALSE; | |
1297 | ||
34dc7c2f BB |
1298 | /* |
1299 | * A directory entry already exists for this name. | |
1300 | */ | |
1301 | /* | |
1302 | * Can't truncate an existing file if in exclusive mode. | |
1303 | */ | |
3558fd73 | 1304 | if (excl) { |
34dc7c2f BB |
1305 | error = EEXIST; |
1306 | goto out; | |
1307 | } | |
1308 | /* | |
1309 | * Can't open a directory for writing. | |
1310 | */ | |
3558fd73 | 1311 | if (S_ISDIR(ZTOI(zp)->i_mode)) { |
34dc7c2f BB |
1312 | error = EISDIR; |
1313 | goto out; | |
1314 | } | |
1315 | /* | |
1316 | * Verify requested access to file. | |
1317 | */ | |
1318 | if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { | |
1319 | goto out; | |
1320 | } | |
1321 | ||
1322 | mutex_enter(&dzp->z_lock); | |
1323 | dzp->z_seq++; | |
1324 | mutex_exit(&dzp->z_lock); | |
1325 | ||
1326 | /* | |
1327 | * Truncate regular files if requested. | |
1328 | */ | |
3558fd73 BB |
1329 | if (S_ISREG(ZTOI(zp)->i_mode) && |
1330 | (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) { | |
b128c09f BB |
1331 | /* we can't hold any locks when calling zfs_freesp() */ |
1332 | zfs_dirent_unlock(dl); | |
1333 | dl = NULL; | |
34dc7c2f | 1334 | error = zfs_freesp(zp, 0, 0, mode, TRUE); |
34dc7c2f BB |
1335 | } |
1336 | } | |
1337 | out: | |
1338 | ||
1339 | if (dl) | |
1340 | zfs_dirent_unlock(dl); | |
1341 | ||
1342 | if (error) { | |
1343 | if (zp) | |
3558fd73 | 1344 | iput(ZTOI(zp)); |
34dc7c2f | 1345 | } else { |
960e08fe BB |
1346 | zfs_inode_update(dzp); |
1347 | zfs_inode_update(zp); | |
3558fd73 | 1348 | *ipp = ZTOI(zp); |
34dc7c2f | 1349 | } |
34dc7c2f | 1350 | |
3558fd73 | 1351 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1352 | zil_commit(zilog, 0); |
428870ff | 1353 | |
3558fd73 | 1354 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1355 | return (error); |
1356 | } | |
e5c39b95 | 1357 | EXPORT_SYMBOL(zfs_create); |
34dc7c2f BB |
1358 | |
1359 | /* | |
1360 | * Remove an entry from a directory. | |
1361 | * | |
3558fd73 | 1362 | * IN: dip - inode of directory to remove entry from. |
34dc7c2f BB |
1363 | * name - name of entry to remove. |
1364 | * cr - credentials of caller. | |
34dc7c2f BB |
1365 | * |
1366 | * RETURN: 0 if success | |
1367 | * error code if failure | |
1368 | * | |
1369 | * Timestamps: | |
3558fd73 BB |
1370 | * dip - ctime|mtime |
1371 | * ip - ctime (if nlink > 0) | |
34dc7c2f | 1372 | */ |
428870ff BB |
1373 | |
1374 | uint64_t null_xattr = 0; | |
1375 | ||
34dc7c2f | 1376 | /*ARGSUSED*/ |
e5c39b95 | 1377 | int |
3558fd73 | 1378 | zfs_remove(struct inode *dip, char *name, cred_t *cr) |
34dc7c2f | 1379 | { |
3558fd73 | 1380 | znode_t *zp, *dzp = ITOZ(dip); |
572e2857 | 1381 | znode_t *xzp; |
3558fd73 BB |
1382 | struct inode *ip; |
1383 | zfs_sb_t *zsb = ITOZSB(dip); | |
34dc7c2f | 1384 | zilog_t *zilog; |
3558fd73 BB |
1385 | uint64_t xattr_obj; |
1386 | uint64_t xattr_obj_unlinked = 0; | |
572e2857 | 1387 | uint64_t obj = 0; |
34dc7c2f BB |
1388 | zfs_dirlock_t *dl; |
1389 | dmu_tx_t *tx; | |
3558fd73 | 1390 | boolean_t unlinked; |
34dc7c2f BB |
1391 | uint64_t txtype; |
1392 | pathname_t *realnmp = NULL; | |
3558fd73 | 1393 | #ifdef HAVE_PN_UTILS |
34dc7c2f | 1394 | pathname_t realnm; |
3558fd73 | 1395 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f BB |
1396 | int error; |
1397 | int zflg = ZEXISTS; | |
1398 | ||
3558fd73 | 1399 | ZFS_ENTER(zsb); |
34dc7c2f | 1400 | ZFS_VERIFY_ZP(dzp); |
3558fd73 | 1401 | zilog = zsb->z_log; |
34dc7c2f | 1402 | |
3558fd73 | 1403 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
1404 | if (flags & FIGNORECASE) { |
1405 | zflg |= ZCILOOK; | |
1406 | pn_alloc(&realnm); | |
1407 | realnmp = &realnm; | |
1408 | } | |
3558fd73 | 1409 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f BB |
1410 | |
1411 | top: | |
572e2857 BB |
1412 | xattr_obj = 0; |
1413 | xzp = NULL; | |
34dc7c2f BB |
1414 | /* |
1415 | * Attempt to lock directory; fail if entry doesn't exist. | |
1416 | */ | |
149e873a BB |
1417 | if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, |
1418 | NULL, realnmp))) { | |
3558fd73 | 1419 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
1420 | if (realnmp) |
1421 | pn_free(realnmp); | |
3558fd73 BB |
1422 | #endif /* HAVE_PN_UTILS */ |
1423 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
1424 | return (error); |
1425 | } | |
1426 | ||
3558fd73 | 1427 | ip = ZTOI(zp); |
34dc7c2f | 1428 | |
149e873a | 1429 | if ((error = zfs_zaccess_delete(dzp, zp, cr))) { |
34dc7c2f BB |
1430 | goto out; |
1431 | } | |
1432 | ||
1433 | /* | |
1434 | * Need to use rmdir for removing directories. | |
1435 | */ | |
3558fd73 | 1436 | if (S_ISDIR(ip->i_mode)) { |
34dc7c2f BB |
1437 | error = EPERM; |
1438 | goto out; | |
1439 | } | |
1440 | ||
3558fd73 | 1441 | #ifdef HAVE_DNLC |
34dc7c2f BB |
1442 | if (realnmp) |
1443 | dnlc_remove(dvp, realnmp->pn_buf); | |
1444 | else | |
1445 | dnlc_remove(dvp, name); | |
3558fd73 | 1446 | #endif /* HAVE_DNLC */ |
34dc7c2f BB |
1447 | |
1448 | /* | |
3558fd73 BB |
1449 | * We never delete the znode and always place it in the unlinked |
1450 | * set. The dentry cache will always hold the last reference and | |
1451 | * is responsible for safely freeing the znode. | |
34dc7c2f | 1452 | */ |
572e2857 | 1453 | obj = zp->z_id; |
3558fd73 | 1454 | tx = dmu_tx_create(zsb->z_os); |
34dc7c2f | 1455 | dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); |
428870ff BB |
1456 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1457 | zfs_sa_upgrade_txholds(tx, zp); | |
1458 | zfs_sa_upgrade_txholds(tx, dzp); | |
34dc7c2f BB |
1459 | |
1460 | /* are there any extended attributes? */ | |
3558fd73 | 1461 | error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb), |
428870ff | 1462 | &xattr_obj, sizeof (xattr_obj)); |
572e2857 | 1463 | if (error == 0 && xattr_obj) { |
3558fd73 | 1464 | error = zfs_zget(zsb, xattr_obj, &xzp); |
428870ff BB |
1465 | ASSERT3U(error, ==, 0); |
1466 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); | |
1467 | dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
1468 | } |
1469 | ||
34dc7c2f | 1470 | /* charge as an update -- would be nice not to charge at all */ |
3558fd73 | 1471 | dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL); |
34dc7c2f | 1472 | |
fb5f0bc8 | 1473 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
1474 | if (error) { |
1475 | zfs_dirent_unlock(dl); | |
3558fd73 | 1476 | iput(ip); |
572e2857 | 1477 | if (xzp) |
3558fd73 | 1478 | iput(ZTOI(xzp)); |
fb5f0bc8 | 1479 | if (error == ERESTART) { |
34dc7c2f BB |
1480 | dmu_tx_wait(tx); |
1481 | dmu_tx_abort(tx); | |
1482 | goto top; | |
1483 | } | |
3558fd73 | 1484 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
1485 | if (realnmp) |
1486 | pn_free(realnmp); | |
3558fd73 | 1487 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f | 1488 | dmu_tx_abort(tx); |
3558fd73 | 1489 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1490 | return (error); |
1491 | } | |
1492 | ||
1493 | /* | |
1494 | * Remove the directory entry. | |
1495 | */ | |
1496 | error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); | |
1497 | ||
1498 | if (error) { | |
1499 | dmu_tx_commit(tx); | |
1500 | goto out; | |
1501 | } | |
1502 | ||
1503 | if (unlinked) { | |
572e2857 BB |
1504 | /* |
1505 | * Hold z_lock so that we can make sure that the ACL obj | |
1506 | * hasn't changed. Could have been deleted due to | |
1507 | * zfs_sa_upgrade(). | |
1508 | */ | |
1509 | mutex_enter(&zp->z_lock); | |
3558fd73 | 1510 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb), |
428870ff | 1511 | &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); |
572e2857 | 1512 | mutex_exit(&zp->z_lock); |
34dc7c2f BB |
1513 | zfs_unlinked_add(zp, tx); |
1514 | } | |
1515 | ||
1516 | txtype = TX_REMOVE; | |
3558fd73 | 1517 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
1518 | if (flags & FIGNORECASE) |
1519 | txtype |= TX_CI; | |
3558fd73 | 1520 | #endif /* HAVE_PN_UTILS */ |
572e2857 | 1521 | zfs_log_remove(zilog, tx, txtype, dzp, name, obj); |
34dc7c2f BB |
1522 | |
1523 | dmu_tx_commit(tx); | |
1524 | out: | |
3558fd73 | 1525 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
1526 | if (realnmp) |
1527 | pn_free(realnmp); | |
3558fd73 | 1528 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f BB |
1529 | |
1530 | zfs_dirent_unlock(dl); | |
960e08fe BB |
1531 | zfs_inode_update(dzp); |
1532 | zfs_inode_update(zp); | |
1533 | if (xzp) | |
1534 | zfs_inode_update(xzp); | |
34dc7c2f | 1535 | |
3558fd73 | 1536 | iput(ip); |
428870ff | 1537 | if (xzp) |
3558fd73 | 1538 | iput(ZTOI(xzp)); |
428870ff | 1539 | |
3558fd73 | 1540 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1541 | zil_commit(zilog, 0); |
34dc7c2f | 1542 | |
3558fd73 | 1543 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1544 | return (error); |
1545 | } | |
e5c39b95 | 1546 | EXPORT_SYMBOL(zfs_remove); |
34dc7c2f BB |
1547 | |
1548 | /* | |
3558fd73 | 1549 | * Create a new directory and insert it into dip using the name |
34dc7c2f BB |
1550 | * provided. Return a pointer to the inserted directory. |
1551 | * | |
3558fd73 | 1552 | * IN: dip - inode of directory to add subdir to. |
34dc7c2f BB |
1553 | * dirname - name of new directory. |
1554 | * vap - attributes of new directory. | |
1555 | * cr - credentials of caller. | |
34dc7c2f BB |
1556 | * vsecp - ACL to be set |
1557 | * | |
3558fd73 | 1558 | * OUT: ipp - inode of created directory. |
34dc7c2f BB |
1559 | * |
1560 | * RETURN: 0 if success | |
1561 | * error code if failure | |
1562 | * | |
1563 | * Timestamps: | |
3558fd73 BB |
1564 | * dip - ctime|mtime updated |
1565 | * ipp - ctime|mtime|atime updated | |
34dc7c2f BB |
1566 | */ |
1567 | /*ARGSUSED*/ | |
e5c39b95 | 1568 | int |
3558fd73 BB |
1569 | zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp, |
1570 | cred_t *cr, int flags, vsecattr_t *vsecp) | |
34dc7c2f | 1571 | { |
3558fd73 BB |
1572 | znode_t *zp, *dzp = ITOZ(dip); |
1573 | zfs_sb_t *zsb = ITOZSB(dip); | |
34dc7c2f BB |
1574 | zilog_t *zilog; |
1575 | zfs_dirlock_t *dl; | |
1576 | uint64_t txtype; | |
1577 | dmu_tx_t *tx; | |
1578 | int error; | |
34dc7c2f | 1579 | int zf = ZNEW; |
b128c09f BB |
1580 | uid_t uid; |
1581 | gid_t gid = crgetgid(cr); | |
428870ff | 1582 | zfs_acl_ids_t acl_ids; |
9babb374 | 1583 | boolean_t fuid_dirtied; |
34dc7c2f | 1584 | |
3558fd73 | 1585 | ASSERT(S_ISDIR(vap->va_mode)); |
34dc7c2f BB |
1586 | |
1587 | /* | |
1588 | * If we have an ephemeral id, ACL, or XVATTR then | |
1589 | * make sure file system is at proper version | |
1590 | */ | |
1591 | ||
3558fd73 BB |
1592 | uid = crgetuid(cr); |
1593 | if (zsb->z_use_fuids == B_FALSE && | |
1594 | (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) | |
34dc7c2f BB |
1595 | return (EINVAL); |
1596 | ||
3558fd73 | 1597 | ZFS_ENTER(zsb); |
34dc7c2f | 1598 | ZFS_VERIFY_ZP(dzp); |
3558fd73 | 1599 | zilog = zsb->z_log; |
34dc7c2f | 1600 | |
428870ff | 1601 | if (dzp->z_pflags & ZFS_XATTR) { |
3558fd73 | 1602 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1603 | return (EINVAL); |
1604 | } | |
1605 | ||
3558fd73 | 1606 | if (zsb->z_utf8 && u8_validate(dirname, |
34dc7c2f | 1607 | strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
3558fd73 | 1608 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1609 | return (EILSEQ); |
1610 | } | |
1611 | if (flags & FIGNORECASE) | |
1612 | zf |= ZCILOOK; | |
1613 | ||
5484965a | 1614 | if (vap->va_mask & ATTR_XVATTR) { |
34dc7c2f | 1615 | if ((error = secpolicy_xvattr((xvattr_t *)vap, |
3558fd73 BB |
1616 | crgetuid(cr), cr, vap->va_mode)) != 0) { |
1617 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
1618 | return (error); |
1619 | } | |
428870ff | 1620 | } |
34dc7c2f | 1621 | |
428870ff BB |
1622 | if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, |
1623 | vsecp, &acl_ids)) != 0) { | |
3558fd73 | 1624 | ZFS_EXIT(zsb); |
428870ff BB |
1625 | return (error); |
1626 | } | |
34dc7c2f BB |
1627 | /* |
1628 | * First make sure the new directory doesn't exist. | |
428870ff BB |
1629 | * |
1630 | * Existence is checked first to make sure we don't return | |
1631 | * EACCES instead of EEXIST which can cause some applications | |
1632 | * to fail. | |
34dc7c2f BB |
1633 | */ |
1634 | top: | |
3558fd73 | 1635 | *ipp = NULL; |
34dc7c2f | 1636 | |
149e873a BB |
1637 | if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, |
1638 | NULL, NULL))) { | |
428870ff | 1639 | zfs_acl_ids_free(&acl_ids); |
3558fd73 | 1640 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1641 | return (error); |
1642 | } | |
1643 | ||
149e873a | 1644 | if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) { |
428870ff | 1645 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 1646 | zfs_dirent_unlock(dl); |
3558fd73 | 1647 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1648 | return (error); |
1649 | } | |
1650 | ||
3558fd73 | 1651 | if (zfs_acl_ids_overquota(zsb, &acl_ids)) { |
45d1cae3 | 1652 | zfs_acl_ids_free(&acl_ids); |
9babb374 | 1653 | zfs_dirent_unlock(dl); |
3558fd73 | 1654 | ZFS_EXIT(zsb); |
9babb374 BB |
1655 | return (EDQUOT); |
1656 | } | |
1657 | ||
34dc7c2f BB |
1658 | /* |
1659 | * Add a new entry to the directory. | |
1660 | */ | |
3558fd73 | 1661 | tx = dmu_tx_create(zsb->z_os); |
34dc7c2f BB |
1662 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); |
1663 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); | |
3558fd73 | 1664 | fuid_dirtied = zsb->z_fuid_dirty; |
9babb374 | 1665 | if (fuid_dirtied) |
3558fd73 BB |
1666 | zfs_fuid_txhold(zsb, tx); |
1667 | if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { | |
428870ff BB |
1668 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, |
1669 | acl_ids.z_aclp->z_acl_bytes); | |
1670 | } | |
1671 | ||
1672 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + | |
1673 | ZFS_SA_BASE_ATTR_SIZE); | |
1674 | ||
fb5f0bc8 | 1675 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
1676 | if (error) { |
1677 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 1678 | if (error == ERESTART) { |
34dc7c2f BB |
1679 | dmu_tx_wait(tx); |
1680 | dmu_tx_abort(tx); | |
1681 | goto top; | |
1682 | } | |
428870ff | 1683 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 1684 | dmu_tx_abort(tx); |
3558fd73 | 1685 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1686 | return (error); |
1687 | } | |
1688 | ||
1689 | /* | |
1690 | * Create new node. | |
1691 | */ | |
428870ff | 1692 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
34dc7c2f | 1693 | |
9babb374 | 1694 | if (fuid_dirtied) |
3558fd73 | 1695 | zfs_fuid_sync(zsb, tx); |
428870ff | 1696 | |
34dc7c2f BB |
1697 | /* |
1698 | * Now put new name in parent dir. | |
1699 | */ | |
1700 | (void) zfs_link_create(dl, zp, tx, ZNEW); | |
1701 | ||
3558fd73 | 1702 | *ipp = ZTOI(zp); |
34dc7c2f BB |
1703 | |
1704 | txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); | |
1705 | if (flags & FIGNORECASE) | |
1706 | txtype |= TX_CI; | |
9babb374 BB |
1707 | zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, |
1708 | acl_ids.z_fuidp, vap); | |
34dc7c2f | 1709 | |
9babb374 | 1710 | zfs_acl_ids_free(&acl_ids); |
428870ff | 1711 | |
34dc7c2f BB |
1712 | dmu_tx_commit(tx); |
1713 | ||
1714 | zfs_dirent_unlock(dl); | |
1715 | ||
3558fd73 | 1716 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1717 | zil_commit(zilog, 0); |
428870ff | 1718 | |
960e08fe BB |
1719 | zfs_inode_update(dzp); |
1720 | zfs_inode_update(zp); | |
3558fd73 | 1721 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1722 | return (0); |
1723 | } | |
e5c39b95 | 1724 | EXPORT_SYMBOL(zfs_mkdir); |
34dc7c2f BB |
1725 | |
1726 | /* | |
1727 | * Remove a directory subdir entry. If the current working | |
1728 | * directory is the same as the subdir to be removed, the | |
1729 | * remove will fail. | |
1730 | * | |
3558fd73 | 1731 | * IN: dip - inode of directory to remove from. |
34dc7c2f | 1732 | * name - name of directory to be removed. |
3558fd73 | 1733 | * cwd - inode of current working directory. |
34dc7c2f | 1734 | * cr - credentials of caller. |
34dc7c2f BB |
1735 | * flags - case flags |
1736 | * | |
1737 | * RETURN: 0 if success | |
1738 | * error code if failure | |
1739 | * | |
1740 | * Timestamps: | |
3558fd73 | 1741 | * dip - ctime|mtime updated |
34dc7c2f BB |
1742 | */ |
1743 | /*ARGSUSED*/ | |
e5c39b95 | 1744 | int |
3558fd73 BB |
1745 | zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr, |
1746 | int flags) | |
34dc7c2f | 1747 | { |
3558fd73 | 1748 | znode_t *dzp = ITOZ(dip); |
34dc7c2f | 1749 | znode_t *zp; |
3558fd73 BB |
1750 | struct inode *ip; |
1751 | zfs_sb_t *zsb = ITOZSB(dip); | |
34dc7c2f BB |
1752 | zilog_t *zilog; |
1753 | zfs_dirlock_t *dl; | |
1754 | dmu_tx_t *tx; | |
1755 | int error; | |
1756 | int zflg = ZEXISTS; | |
1757 | ||
3558fd73 | 1758 | ZFS_ENTER(zsb); |
34dc7c2f | 1759 | ZFS_VERIFY_ZP(dzp); |
3558fd73 | 1760 | zilog = zsb->z_log; |
34dc7c2f BB |
1761 | |
1762 | if (flags & FIGNORECASE) | |
1763 | zflg |= ZCILOOK; | |
1764 | top: | |
1765 | zp = NULL; | |
1766 | ||
1767 | /* | |
1768 | * Attempt to lock directory; fail if entry doesn't exist. | |
1769 | */ | |
149e873a BB |
1770 | if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, |
1771 | NULL, NULL))) { | |
3558fd73 | 1772 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1773 | return (error); |
1774 | } | |
1775 | ||
3558fd73 | 1776 | ip = ZTOI(zp); |
34dc7c2f | 1777 | |
149e873a | 1778 | if ((error = zfs_zaccess_delete(dzp, zp, cr))) { |
34dc7c2f BB |
1779 | goto out; |
1780 | } | |
1781 | ||
3558fd73 | 1782 | if (!S_ISDIR(ip->i_mode)) { |
34dc7c2f BB |
1783 | error = ENOTDIR; |
1784 | goto out; | |
1785 | } | |
1786 | ||
3558fd73 | 1787 | if (ip == cwd) { |
34dc7c2f BB |
1788 | error = EINVAL; |
1789 | goto out; | |
1790 | } | |
1791 | ||
34dc7c2f BB |
1792 | /* |
1793 | * Grab a lock on the directory to make sure that noone is | |
1794 | * trying to add (or lookup) entries while we are removing it. | |
1795 | */ | |
1796 | rw_enter(&zp->z_name_lock, RW_WRITER); | |
1797 | ||
1798 | /* | |
1799 | * Grab a lock on the parent pointer to make sure we play well | |
1800 | * with the treewalk and directory rename code. | |
1801 | */ | |
1802 | rw_enter(&zp->z_parent_lock, RW_WRITER); | |
1803 | ||
3558fd73 | 1804 | tx = dmu_tx_create(zsb->z_os); |
34dc7c2f | 1805 | dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); |
428870ff | 1806 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
3558fd73 | 1807 | dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL); |
428870ff BB |
1808 | zfs_sa_upgrade_txholds(tx, zp); |
1809 | zfs_sa_upgrade_txholds(tx, dzp); | |
fb5f0bc8 | 1810 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
1811 | if (error) { |
1812 | rw_exit(&zp->z_parent_lock); | |
1813 | rw_exit(&zp->z_name_lock); | |
1814 | zfs_dirent_unlock(dl); | |
3558fd73 | 1815 | iput(ip); |
fb5f0bc8 | 1816 | if (error == ERESTART) { |
34dc7c2f BB |
1817 | dmu_tx_wait(tx); |
1818 | dmu_tx_abort(tx); | |
1819 | goto top; | |
1820 | } | |
1821 | dmu_tx_abort(tx); | |
3558fd73 | 1822 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1823 | return (error); |
1824 | } | |
1825 | ||
1826 | error = zfs_link_destroy(dl, zp, tx, zflg, NULL); | |
1827 | ||
1828 | if (error == 0) { | |
1829 | uint64_t txtype = TX_RMDIR; | |
1830 | if (flags & FIGNORECASE) | |
1831 | txtype |= TX_CI; | |
572e2857 | 1832 | zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); |
34dc7c2f BB |
1833 | } |
1834 | ||
1835 | dmu_tx_commit(tx); | |
1836 | ||
1837 | rw_exit(&zp->z_parent_lock); | |
1838 | rw_exit(&zp->z_name_lock); | |
1839 | out: | |
1840 | zfs_dirent_unlock(dl); | |
1841 | ||
3558fd73 | 1842 | iput(ip); |
34dc7c2f | 1843 | |
3558fd73 | 1844 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1845 | zil_commit(zilog, 0); |
428870ff | 1846 | |
960e08fe BB |
1847 | zfs_inode_update(dzp); |
1848 | zfs_inode_update(zp); | |
3558fd73 | 1849 | ZFS_EXIT(zsb); |
34dc7c2f BB |
1850 | return (error); |
1851 | } | |
e5c39b95 | 1852 | EXPORT_SYMBOL(zfs_rmdir); |
34dc7c2f BB |
1853 | |
1854 | /* | |
1855 | * Read as many directory entries as will fit into the provided | |
3558fd73 | 1856 | * dirent buffer from the given directory cursor position. |
34dc7c2f | 1857 | * |
3558fd73 BB |
1858 | * IN: ip - inode of directory to read. |
1859 | * dirent - buffer for directory entries. | |
34dc7c2f | 1860 | * |
3558fd73 | 1861 | * OUT: dirent - filler buffer of directory entries. |
34dc7c2f BB |
1862 | * |
1863 | * RETURN: 0 if success | |
1864 | * error code if failure | |
1865 | * | |
1866 | * Timestamps: | |
3558fd73 | 1867 | * ip - atime updated |
34dc7c2f BB |
1868 | * |
1869 | * Note that the low 4 bits of the cookie returned by zap is always zero. | |
1870 | * This allows us to use the low range for "special" directory entries: | |
1871 | * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, | |
1872 | * we use the offset 2 for the '.zfs' directory. | |
1873 | */ | |
1874 | /* ARGSUSED */ | |
3558fd73 BB |
1875 | int |
1876 | zfs_readdir(struct inode *ip, void *dirent, filldir_t filldir, | |
1877 | loff_t *pos, cred_t *cr) | |
34dc7c2f | 1878 | { |
3558fd73 BB |
1879 | znode_t *zp = ITOZ(ip); |
1880 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f | 1881 | objset_t *os; |
34dc7c2f BB |
1882 | zap_cursor_t zc; |
1883 | zap_attribute_t zap; | |
34dc7c2f BB |
1884 | int outcount; |
1885 | int error; | |
1886 | uint8_t prefetch; | |
3558fd73 BB |
1887 | int done = 0; |
1888 | uint64_t parent; | |
34dc7c2f | 1889 | |
3558fd73 | 1890 | ZFS_ENTER(zsb); |
34dc7c2f BB |
1891 | ZFS_VERIFY_ZP(zp); |
1892 | ||
3558fd73 BB |
1893 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zsb), |
1894 | &parent, sizeof (parent))) != 0) | |
1895 | goto out; | |
34dc7c2f BB |
1896 | |
1897 | /* | |
1898 | * Quit if directory has been removed (posix) | |
1899 | */ | |
34dc7c2f | 1900 | error = 0; |
3558fd73 BB |
1901 | if (zp->z_unlinked) |
1902 | goto out; | |
1903 | ||
1904 | os = zsb->z_os; | |
34dc7c2f BB |
1905 | prefetch = zp->z_zn_prefetch; |
1906 | ||
1907 | /* | |
1908 | * Initialize the iterator cursor. | |
1909 | */ | |
3558fd73 | 1910 | if (*pos <= 3) { |
34dc7c2f BB |
1911 | /* |
1912 | * Start iteration from the beginning of the directory. | |
1913 | */ | |
1914 | zap_cursor_init(&zc, os, zp->z_id); | |
1915 | } else { | |
1916 | /* | |
1917 | * The offset is a serialized cursor. | |
1918 | */ | |
3558fd73 | 1919 | zap_cursor_init_serialized(&zc, os, zp->z_id, *pos); |
34dc7c2f BB |
1920 | } |
1921 | ||
34dc7c2f BB |
1922 | /* |
1923 | * Transform to file-system independent format | |
1924 | */ | |
1925 | outcount = 0; | |
34dc7c2f | 1926 | |
3558fd73 BB |
1927 | while (!done) { |
1928 | uint64_t objnum; | |
34dc7c2f BB |
1929 | /* |
1930 | * Special case `.', `..', and `.zfs'. | |
1931 | */ | |
3558fd73 | 1932 | if (*pos == 0) { |
34dc7c2f BB |
1933 | (void) strcpy(zap.za_name, "."); |
1934 | zap.za_normalization_conflict = 0; | |
1935 | objnum = zp->z_id; | |
3558fd73 | 1936 | } else if (*pos == 1) { |
34dc7c2f BB |
1937 | (void) strcpy(zap.za_name, ".."); |
1938 | zap.za_normalization_conflict = 0; | |
428870ff | 1939 | objnum = parent; |
3558fd73 | 1940 | } else if (*pos == 2 && zfs_show_ctldir(zp)) { |
34dc7c2f BB |
1941 | (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); |
1942 | zap.za_normalization_conflict = 0; | |
1943 | objnum = ZFSCTL_INO_ROOT; | |
1944 | } else { | |
1945 | /* | |
1946 | * Grab next entry. | |
1947 | */ | |
3558fd73 BB |
1948 | if ((error = zap_cursor_retrieve(&zc, &zap))) { |
1949 | if (error == ENOENT) | |
34dc7c2f BB |
1950 | break; |
1951 | else | |
1952 | goto update; | |
1953 | } | |
1954 | ||
1955 | if (zap.za_integer_length != 8 || | |
1956 | zap.za_num_integers != 1) { | |
1957 | cmn_err(CE_WARN, "zap_readdir: bad directory " | |
1958 | "entry, obj = %lld, offset = %lld\n", | |
1959 | (u_longlong_t)zp->z_id, | |
3558fd73 | 1960 | (u_longlong_t)*pos); |
34dc7c2f BB |
1961 | error = ENXIO; |
1962 | goto update; | |
1963 | } | |
1964 | ||
1965 | objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); | |
34dc7c2f | 1966 | } |
3558fd73 BB |
1967 | done = filldir(dirent, zap.za_name, strlen(zap.za_name), |
1968 | zap_cursor_serialize(&zc), objnum, 0); | |
1969 | if (done) { | |
34dc7c2f BB |
1970 | break; |
1971 | } | |
34dc7c2f BB |
1972 | |
1973 | /* Prefetch znode */ | |
3558fd73 | 1974 | if (prefetch) { |
34dc7c2f | 1975 | dmu_prefetch(os, objnum, 0, 0); |
3558fd73 | 1976 | } |
34dc7c2f | 1977 | |
3558fd73 | 1978 | if (*pos >= 2) { |
34dc7c2f | 1979 | zap_cursor_advance(&zc); |
3558fd73 | 1980 | *pos = zap_cursor_serialize(&zc); |
34dc7c2f | 1981 | } else { |
3558fd73 | 1982 | (*pos)++; |
34dc7c2f | 1983 | } |
34dc7c2f BB |
1984 | } |
1985 | zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ | |
1986 | ||
34dc7c2f BB |
1987 | update: |
1988 | zap_cursor_fini(&zc); | |
34dc7c2f BB |
1989 | if (error == ENOENT) |
1990 | error = 0; | |
1991 | ||
3558fd73 BB |
1992 | ZFS_ACCESSTIME_STAMP(zsb, zp); |
1993 | zfs_inode_update(zp); | |
1994 | ||
1995 | out: | |
1996 | ZFS_EXIT(zsb); | |
34dc7c2f | 1997 | |
34dc7c2f BB |
1998 | return (error); |
1999 | } | |
3558fd73 | 2000 | EXPORT_SYMBOL(zfs_readdir); |
34dc7c2f BB |
2001 | |
2002 | ulong_t zfs_fsync_sync_cnt = 4; | |
2003 | ||
e5c39b95 | 2004 | int |
3558fd73 | 2005 | zfs_fsync(struct inode *ip, int syncflag, cred_t *cr) |
34dc7c2f | 2006 | { |
3558fd73 BB |
2007 | znode_t *zp = ITOZ(ip); |
2008 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
2009 | |
2010 | (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); | |
2011 | ||
3558fd73 BB |
2012 | if (zsb->z_os->os_sync != ZFS_SYNC_DISABLED) { |
2013 | ZFS_ENTER(zsb); | |
428870ff | 2014 | ZFS_VERIFY_ZP(zp); |
3558fd73 BB |
2015 | zil_commit(zsb->z_log, zp->z_id); |
2016 | ZFS_EXIT(zsb); | |
428870ff | 2017 | } |
34dc7c2f BB |
2018 | return (0); |
2019 | } | |
e5c39b95 | 2020 | EXPORT_SYMBOL(zfs_fsync); |
34dc7c2f BB |
2021 | |
2022 | ||
2023 | /* | |
2024 | * Get the requested file attributes and place them in the provided | |
2025 | * vattr structure. | |
2026 | * | |
3558fd73 | 2027 | * IN: ip - inode of file. |
5484965a BB |
2028 | * vap - va_mask identifies requested attributes. |
2029 | * If ATTR_XVATTR set, then optional attrs are requested | |
34dc7c2f BB |
2030 | * flags - ATTR_NOACLCHECK (CIFS server context) |
2031 | * cr - credentials of caller. | |
34dc7c2f | 2032 | * |
5484965a BB |
2033 | * OUT: vap - attribute values. |
2034 | * | |
2035 | * RETURN: 0 (always succeeds) | |
34dc7c2f BB |
2036 | */ |
2037 | /* ARGSUSED */ | |
e5c39b95 | 2038 | int |
5484965a | 2039 | zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr) |
34dc7c2f | 2040 | { |
3558fd73 BB |
2041 | znode_t *zp = ITOZ(ip); |
2042 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
2043 | int error = 0; |
2044 | uint64_t links; | |
428870ff | 2045 | uint64_t mtime[2], ctime[2]; |
5484965a BB |
2046 | xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ |
2047 | xoptattr_t *xoap = NULL; | |
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 | |
5484965a | 2055 | zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_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) && |
5484965a | 2071 | (vap->va_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); |
5484965a BB |
2085 | vap->va_type = vn_mode_to_vtype(zp->z_mode); |
2086 | vap->va_mode = zp->z_mode; | |
2087 | vap->va_fsid = 0; | |
2088 | vap->va_nodeid = zp->z_id; | |
3558fd73 | 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; |
5484965a BB |
2093 | vap->va_nlink = MIN(links, ZFS_LINK_MAX); |
2094 | vap->va_size = i_size_read(ip); | |
2095 | vap->va_rdev = ip->i_rdev; | |
2096 | vap->va_seq = ip->i_generation; | |
2097 | ||
2098 | /* | |
2099 | * Add in any requested optional attributes and the create time. | |
2100 | * Also set the corresponding bits in the returned attribute bitmap. | |
2101 | */ | |
2102 | if ((xoap = xva_getxoptattr(xvap)) != NULL && zsb->z_use_fuids) { | |
2103 | if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { | |
2104 | xoap->xoa_archive = | |
2105 | ((zp->z_pflags & ZFS_ARCHIVE) != 0); | |
2106 | XVA_SET_RTN(xvap, XAT_ARCHIVE); | |
2107 | } | |
2108 | ||
2109 | if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { | |
2110 | xoap->xoa_readonly = | |
2111 | ((zp->z_pflags & ZFS_READONLY) != 0); | |
2112 | XVA_SET_RTN(xvap, XAT_READONLY); | |
2113 | } | |
2114 | ||
2115 | if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { | |
2116 | xoap->xoa_system = | |
2117 | ((zp->z_pflags & ZFS_SYSTEM) != 0); | |
2118 | XVA_SET_RTN(xvap, XAT_SYSTEM); | |
2119 | } | |
2120 | ||
2121 | if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { | |
2122 | xoap->xoa_hidden = | |
2123 | ((zp->z_pflags & ZFS_HIDDEN) != 0); | |
2124 | XVA_SET_RTN(xvap, XAT_HIDDEN); | |
2125 | } | |
2126 | ||
2127 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { | |
2128 | xoap->xoa_nounlink = | |
2129 | ((zp->z_pflags & ZFS_NOUNLINK) != 0); | |
2130 | XVA_SET_RTN(xvap, XAT_NOUNLINK); | |
2131 | } | |
2132 | ||
2133 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
2134 | xoap->xoa_immutable = | |
2135 | ((zp->z_pflags & ZFS_IMMUTABLE) != 0); | |
2136 | XVA_SET_RTN(xvap, XAT_IMMUTABLE); | |
2137 | } | |
2138 | ||
2139 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
2140 | xoap->xoa_appendonly = | |
2141 | ((zp->z_pflags & ZFS_APPENDONLY) != 0); | |
2142 | XVA_SET_RTN(xvap, XAT_APPENDONLY); | |
2143 | } | |
2144 | ||
2145 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
2146 | xoap->xoa_nodump = | |
2147 | ((zp->z_pflags & ZFS_NODUMP) != 0); | |
2148 | XVA_SET_RTN(xvap, XAT_NODUMP); | |
2149 | } | |
2150 | ||
2151 | if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { | |
2152 | xoap->xoa_opaque = | |
2153 | ((zp->z_pflags & ZFS_OPAQUE) != 0); | |
2154 | XVA_SET_RTN(xvap, XAT_OPAQUE); | |
2155 | } | |
2156 | ||
2157 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
2158 | xoap->xoa_av_quarantined = | |
2159 | ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); | |
2160 | XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); | |
2161 | } | |
2162 | ||
2163 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
2164 | xoap->xoa_av_modified = | |
2165 | ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); | |
2166 | XVA_SET_RTN(xvap, XAT_AV_MODIFIED); | |
2167 | } | |
2168 | ||
2169 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && | |
2170 | S_ISREG(ip->i_mode)) { | |
2171 | zfs_sa_get_scanstamp(zp, xvap); | |
2172 | } | |
34dc7c2f | 2173 | |
5484965a BB |
2174 | if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { |
2175 | uint64_t times[2]; | |
2176 | ||
2177 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zsb), | |
2178 | times, sizeof (times)); | |
2179 | ZFS_TIME_DECODE(&xoap->xoa_createtime, times); | |
2180 | XVA_SET_RTN(xvap, XAT_CREATETIME); | |
2181 | } | |
2182 | ||
2183 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
2184 | xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); | |
2185 | XVA_SET_RTN(xvap, XAT_REPARSE); | |
2186 | } | |
2187 | if (XVA_ISSET_REQ(xvap, XAT_GEN)) { | |
2188 | xoap->xoa_generation = zp->z_gen; | |
2189 | XVA_SET_RTN(xvap, XAT_GEN); | |
2190 | } | |
2191 | ||
2192 | if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { | |
2193 | xoap->xoa_offline = | |
2194 | ((zp->z_pflags & ZFS_OFFLINE) != 0); | |
2195 | XVA_SET_RTN(xvap, XAT_OFFLINE); | |
2196 | } | |
2197 | ||
2198 | if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { | |
2199 | xoap->xoa_sparse = | |
2200 | ((zp->z_pflags & ZFS_SPARSE) != 0); | |
2201 | XVA_SET_RTN(xvap, XAT_SPARSE); | |
2202 | } | |
2203 | } | |
2204 | ||
2205 | ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); | |
2206 | ZFS_TIME_DECODE(&vap->va_mtime, mtime); | |
2207 | ZFS_TIME_DECODE(&vap->va_ctime, ctime); | |
34dc7c2f BB |
2208 | |
2209 | mutex_exit(&zp->z_lock); | |
2210 | ||
5484965a | 2211 | sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); |
34dc7c2f BB |
2212 | |
2213 | if (zp->z_blksz == 0) { | |
2214 | /* | |
2215 | * Block size hasn't been set; suggest maximal I/O transfers. | |
2216 | */ | |
5484965a | 2217 | vap->va_blksize = zsb->z_max_blksz; |
34dc7c2f BB |
2218 | } |
2219 | ||
3558fd73 | 2220 | ZFS_EXIT(zsb); |
34dc7c2f BB |
2221 | return (0); |
2222 | } | |
e5c39b95 | 2223 | EXPORT_SYMBOL(zfs_getattr); |
34dc7c2f BB |
2224 | |
2225 | /* | |
2226 | * Set the file attributes to the values contained in the | |
2227 | * vattr structure. | |
2228 | * | |
3558fd73 | 2229 | * IN: ip - inode of file to be modified. |
34dc7c2f | 2230 | * vap - new attribute values. |
5484965a | 2231 | * If ATTR_XVATTR set, then optional attrs are being set |
34dc7c2f BB |
2232 | * flags - ATTR_UTIME set if non-default time values provided. |
2233 | * - ATTR_NOACLCHECK (CIFS context only). | |
2234 | * cr - credentials of caller. | |
34dc7c2f BB |
2235 | * |
2236 | * RETURN: 0 if success | |
2237 | * error code if failure | |
2238 | * | |
2239 | * Timestamps: | |
3558fd73 | 2240 | * ip - ctime updated, mtime updated if size changed. |
34dc7c2f BB |
2241 | */ |
2242 | /* ARGSUSED */ | |
e5c39b95 | 2243 | int |
5484965a | 2244 | zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr) |
34dc7c2f | 2245 | { |
3558fd73 BB |
2246 | znode_t *zp = ITOZ(ip); |
2247 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
2248 | zilog_t *zilog; |
2249 | dmu_tx_t *tx; | |
2250 | vattr_t oldva; | |
f4ea75d4 | 2251 | xvattr_t *tmpxvattr; |
5484965a | 2252 | uint_t mask = vap->va_mask; |
34dc7c2f BB |
2253 | uint_t saved_mask; |
2254 | int trim_mask = 0; | |
2255 | uint64_t new_mode; | |
9babb374 | 2256 | uint64_t new_uid, new_gid; |
572e2857 | 2257 | uint64_t xattr_obj; |
428870ff | 2258 | uint64_t mtime[2], ctime[2]; |
34dc7c2f BB |
2259 | znode_t *attrzp; |
2260 | int need_policy = FALSE; | |
428870ff | 2261 | int err, err2; |
34dc7c2f | 2262 | zfs_fuid_info_t *fuidp = NULL; |
5484965a BB |
2263 | xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ |
2264 | xoptattr_t *xoap; | |
2265 | zfs_acl_t *aclp; | |
34dc7c2f | 2266 | boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; |
428870ff BB |
2267 | boolean_t fuid_dirtied = B_FALSE; |
2268 | sa_bulk_attr_t bulk[7], xattr_bulk[7]; | |
2269 | int count = 0, xattr_count = 0; | |
34dc7c2f BB |
2270 | |
2271 | if (mask == 0) | |
2272 | return (0); | |
2273 | ||
3558fd73 | 2274 | ZFS_ENTER(zsb); |
34dc7c2f BB |
2275 | ZFS_VERIFY_ZP(zp); |
2276 | ||
3558fd73 | 2277 | zilog = zsb->z_log; |
34dc7c2f BB |
2278 | |
2279 | /* | |
2280 | * Make sure that if we have ephemeral uid/gid or xvattr specified | |
2281 | * that file system is at proper version level | |
2282 | */ | |
5484965a | 2283 | |
3558fd73 | 2284 | if (zsb->z_use_fuids == B_FALSE && |
5484965a BB |
2285 | (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) || |
2286 | ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) || | |
2287 | (mask & ATTR_XVATTR))) { | |
3558fd73 | 2288 | ZFS_EXIT(zsb); |
34dc7c2f BB |
2289 | return (EINVAL); |
2290 | } | |
2291 | ||
3558fd73 BB |
2292 | if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) { |
2293 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
2294 | return (EISDIR); |
2295 | } | |
2296 | ||
3558fd73 BB |
2297 | if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) { |
2298 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
2299 | return (EINVAL); |
2300 | } | |
2301 | ||
5484965a BB |
2302 | /* |
2303 | * If this is an xvattr_t, then get a pointer to the structure of | |
2304 | * optional attributes. If this is NULL, then we have a vattr_t. | |
2305 | */ | |
2306 | xoap = xva_getxoptattr(xvap); | |
2307 | ||
f4ea75d4 BB |
2308 | tmpxvattr = kmem_alloc(sizeof(xvattr_t), KM_SLEEP); |
2309 | xva_init(tmpxvattr); | |
5484965a BB |
2310 | |
2311 | /* | |
2312 | * Immutable files can only alter immutable bit and atime | |
2313 | */ | |
2314 | if ((zp->z_pflags & ZFS_IMMUTABLE) && | |
2315 | ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) || | |
2316 | ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { | |
f4ea75d4 BB |
2317 | err = EPERM; |
2318 | goto out3; | |
5484965a BB |
2319 | } |
2320 | ||
3558fd73 | 2321 | if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) { |
f4ea75d4 BB |
2322 | err = EPERM; |
2323 | goto out3; | |
34dc7c2f BB |
2324 | } |
2325 | ||
5484965a BB |
2326 | /* |
2327 | * Verify timestamps doesn't overflow 32 bits. | |
2328 | * ZFS can handle large timestamps, but 32bit syscalls can't | |
2329 | * handle times greater than 2039. This check should be removed | |
2330 | * once large timestamps are fully supported. | |
2331 | */ | |
2332 | if (mask & (ATTR_ATIME | ATTR_MTIME)) { | |
2333 | if (((mask & ATTR_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || | |
2334 | ((mask & ATTR_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { | |
f4ea75d4 BB |
2335 | err = EOVERFLOW; |
2336 | goto out3; | |
5484965a BB |
2337 | } |
2338 | } | |
2339 | ||
34dc7c2f BB |
2340 | top: |
2341 | attrzp = NULL; | |
572e2857 | 2342 | aclp = NULL; |
34dc7c2f | 2343 | |
45d1cae3 | 2344 | /* Can this be moved to before the top label? */ |
3558fd73 | 2345 | if (zsb->z_vfs->mnt_flags & MNT_READONLY) { |
f4ea75d4 BB |
2346 | err = EROFS; |
2347 | goto out3; | |
34dc7c2f BB |
2348 | } |
2349 | ||
2350 | /* | |
2351 | * First validate permissions | |
2352 | */ | |
2353 | ||
3558fd73 | 2354 | if (mask & ATTR_SIZE) { |
34dc7c2f | 2355 | err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); |
f4ea75d4 BB |
2356 | if (err) |
2357 | goto out3; | |
2358 | ||
34dc7c2f BB |
2359 | /* |
2360 | * XXX - Note, we are not providing any open | |
2361 | * mode flags here (like FNDELAY), so we may | |
2362 | * block if there are locks present... this | |
2363 | * should be addressed in openat(). | |
2364 | */ | |
b128c09f | 2365 | /* XXX - would it be OK to generate a log record here? */ |
5484965a | 2366 | err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); |
f4ea75d4 BB |
2367 | if (err) |
2368 | goto out3; | |
34dc7c2f | 2369 | |
3558fd73 | 2370 | /* Careful negative Linux return code here */ |
5484965a | 2371 | err = -vmtruncate(ip, vap->va_size); |
f4ea75d4 BB |
2372 | if (err) |
2373 | goto out3; | |
428870ff | 2374 | } |
34dc7c2f | 2375 | |
5484965a BB |
2376 | if (mask & (ATTR_ATIME|ATTR_MTIME) || |
2377 | ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || | |
2378 | XVA_ISSET_REQ(xvap, XAT_READONLY) || | |
2379 | XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || | |
2380 | XVA_ISSET_REQ(xvap, XAT_OFFLINE) || | |
2381 | XVA_ISSET_REQ(xvap, XAT_SPARSE) || | |
2382 | XVA_ISSET_REQ(xvap, XAT_CREATETIME) || | |
2383 | XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { | |
2384 | need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, | |
2385 | skipaclchk, cr); | |
2386 | } | |
2387 | ||
3558fd73 BB |
2388 | if (mask & (ATTR_UID|ATTR_GID)) { |
2389 | int idmask = (mask & (ATTR_UID|ATTR_GID)); | |
34dc7c2f BB |
2390 | int take_owner; |
2391 | int take_group; | |
2392 | ||
2393 | /* | |
2394 | * NOTE: even if a new mode is being set, | |
2395 | * we may clear S_ISUID/S_ISGID bits. | |
2396 | */ | |
2397 | ||
3558fd73 | 2398 | if (!(mask & ATTR_MODE)) |
5484965a | 2399 | vap->va_mode = zp->z_mode; |
34dc7c2f BB |
2400 | |
2401 | /* | |
2402 | * Take ownership or chgrp to group we are a member of | |
2403 | */ | |
2404 | ||
5484965a | 2405 | take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr)); |
3558fd73 | 2406 | take_group = (mask & ATTR_GID) && |
5484965a | 2407 | zfs_groupmember(zsb, vap->va_gid, cr); |
34dc7c2f BB |
2408 | |
2409 | /* | |
5484965a | 2410 | * If both ATTR_UID and ATTR_GID are set then take_owner and |
34dc7c2f BB |
2411 | * take_group must both be set in order to allow taking |
2412 | * ownership. | |
2413 | * | |
2414 | * Otherwise, send the check through secpolicy_vnode_setattr() | |
2415 | * | |
2416 | */ | |
2417 | ||
3558fd73 BB |
2418 | if (((idmask == (ATTR_UID|ATTR_GID)) && |
2419 | take_owner && take_group) || | |
2420 | ((idmask == ATTR_UID) && take_owner) || | |
2421 | ((idmask == ATTR_GID) && take_group)) { | |
34dc7c2f BB |
2422 | if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, |
2423 | skipaclchk, cr) == 0) { | |
2424 | /* | |
2425 | * Remove setuid/setgid for non-privileged users | |
2426 | */ | |
5484965a | 2427 | (void) secpolicy_setid_clear(vap, cr); |
3558fd73 | 2428 | trim_mask = (mask & (ATTR_UID|ATTR_GID)); |
34dc7c2f BB |
2429 | } else { |
2430 | need_policy = TRUE; | |
2431 | } | |
2432 | } else { | |
2433 | need_policy = TRUE; | |
2434 | } | |
2435 | } | |
2436 | ||
2437 | mutex_enter(&zp->z_lock); | |
428870ff | 2438 | oldva.va_mode = zp->z_mode; |
572e2857 | 2439 | zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); |
5484965a BB |
2440 | if (mask & ATTR_XVATTR) { |
2441 | /* | |
2442 | * Update xvattr mask to include only those attributes | |
2443 | * that are actually changing. | |
2444 | * | |
2445 | * the bits will be restored prior to actually setting | |
2446 | * the attributes so the caller thinks they were set. | |
2447 | */ | |
2448 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
2449 | if (xoap->xoa_appendonly != | |
2450 | ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { | |
2451 | need_policy = TRUE; | |
2452 | } else { | |
2453 | XVA_CLR_REQ(xvap, XAT_APPENDONLY); | |
f4ea75d4 | 2454 | XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY); |
5484965a BB |
2455 | } |
2456 | } | |
2457 | ||
2458 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { | |
2459 | if (xoap->xoa_nounlink != | |
2460 | ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { | |
2461 | need_policy = TRUE; | |
2462 | } else { | |
2463 | XVA_CLR_REQ(xvap, XAT_NOUNLINK); | |
f4ea75d4 | 2464 | XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK); |
5484965a BB |
2465 | } |
2466 | } | |
2467 | ||
2468 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
2469 | if (xoap->xoa_immutable != | |
2470 | ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { | |
2471 | need_policy = TRUE; | |
2472 | } else { | |
2473 | XVA_CLR_REQ(xvap, XAT_IMMUTABLE); | |
f4ea75d4 | 2474 | XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE); |
5484965a BB |
2475 | } |
2476 | } | |
2477 | ||
2478 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
2479 | if (xoap->xoa_nodump != | |
2480 | ((zp->z_pflags & ZFS_NODUMP) != 0)) { | |
2481 | need_policy = TRUE; | |
2482 | } else { | |
2483 | XVA_CLR_REQ(xvap, XAT_NODUMP); | |
f4ea75d4 | 2484 | XVA_SET_REQ(tmpxvattr, XAT_NODUMP); |
5484965a BB |
2485 | } |
2486 | } | |
2487 | ||
2488 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
2489 | if (xoap->xoa_av_modified != | |
2490 | ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { | |
2491 | need_policy = TRUE; | |
2492 | } else { | |
2493 | XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); | |
f4ea75d4 | 2494 | XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED); |
5484965a BB |
2495 | } |
2496 | } | |
2497 | ||
2498 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
2499 | if ((!S_ISREG(ip->i_mode) && | |
2500 | xoap->xoa_av_quarantined) || | |
2501 | xoap->xoa_av_quarantined != | |
2502 | ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { | |
2503 | need_policy = TRUE; | |
2504 | } else { | |
2505 | XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); | |
f4ea75d4 | 2506 | XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED); |
5484965a BB |
2507 | } |
2508 | } | |
2509 | ||
2510 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
2511 | mutex_exit(&zp->z_lock); | |
f4ea75d4 BB |
2512 | err = EPERM; |
2513 | goto out3; | |
5484965a BB |
2514 | } |
2515 | ||
2516 | if (need_policy == FALSE && | |
2517 | (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || | |
2518 | XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { | |
2519 | need_policy = TRUE; | |
2520 | } | |
2521 | } | |
34dc7c2f BB |
2522 | |
2523 | mutex_exit(&zp->z_lock); | |
2524 | ||
3558fd73 | 2525 | if (mask & ATTR_MODE) { |
34dc7c2f | 2526 | if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { |
5484965a | 2527 | err = secpolicy_setid_setsticky_clear(ip, vap, |
34dc7c2f | 2528 | &oldva, cr); |
f4ea75d4 BB |
2529 | if (err) |
2530 | goto out3; | |
2531 | ||
3558fd73 | 2532 | trim_mask |= ATTR_MODE; |
34dc7c2f BB |
2533 | } else { |
2534 | need_policy = TRUE; | |
2535 | } | |
2536 | } | |
2537 | ||
2538 | if (need_policy) { | |
2539 | /* | |
2540 | * If trim_mask is set then take ownership | |
2541 | * has been granted or write_acl is present and user | |
2542 | * has the ability to modify mode. In that case remove | |
2543 | * UID|GID and or MODE from mask so that | |
2544 | * secpolicy_vnode_setattr() doesn't revoke it. | |
2545 | */ | |
2546 | ||
2547 | if (trim_mask) { | |
5484965a BB |
2548 | saved_mask = vap->va_mask; |
2549 | vap->va_mask &= ~trim_mask; | |
34dc7c2f | 2550 | } |
5484965a | 2551 | err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags, |
34dc7c2f | 2552 | (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); |
f4ea75d4 BB |
2553 | if (err) |
2554 | goto out3; | |
34dc7c2f BB |
2555 | |
2556 | if (trim_mask) | |
5484965a | 2557 | vap->va_mask |= saved_mask; |
34dc7c2f BB |
2558 | } |
2559 | ||
2560 | /* | |
2561 | * secpolicy_vnode_setattr, or take ownership may have | |
2562 | * changed va_mask | |
2563 | */ | |
5484965a | 2564 | mask = vap->va_mask; |
34dc7c2f | 2565 | |
3558fd73 BB |
2566 | if ((mask & (ATTR_UID | ATTR_GID))) { |
2567 | err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zsb), | |
572e2857 | 2568 | &xattr_obj, sizeof (xattr_obj)); |
428870ff | 2569 | |
572e2857 | 2570 | if (err == 0 && xattr_obj) { |
3558fd73 | 2571 | err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp); |
428870ff BB |
2572 | if (err) |
2573 | goto out2; | |
2574 | } | |
3558fd73 BB |
2575 | if (mask & ATTR_UID) { |
2576 | new_uid = zfs_fuid_create(zsb, | |
5484965a | 2577 | (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); |
572e2857 | 2578 | if (new_uid != zp->z_uid && |
3558fd73 | 2579 | zfs_fuid_overquota(zsb, B_FALSE, new_uid)) { |
572e2857 | 2580 | if (attrzp) |
3558fd73 | 2581 | iput(ZTOI(attrzp)); |
428870ff BB |
2582 | err = EDQUOT; |
2583 | goto out2; | |
2584 | } | |
2585 | } | |
2586 | ||
3558fd73 | 2587 | if (mask & ATTR_GID) { |
5484965a | 2588 | new_gid = zfs_fuid_create(zsb, (uint64_t)vap->va_gid, |
428870ff BB |
2589 | cr, ZFS_GROUP, &fuidp); |
2590 | if (new_gid != zp->z_gid && | |
3558fd73 | 2591 | zfs_fuid_overquota(zsb, B_TRUE, new_gid)) { |
572e2857 | 2592 | if (attrzp) |
3558fd73 | 2593 | iput(ZTOI(attrzp)); |
428870ff BB |
2594 | err = EDQUOT; |
2595 | goto out2; | |
2596 | } | |
2597 | } | |
2598 | } | |
3558fd73 | 2599 | tx = dmu_tx_create(zsb->z_os); |
34dc7c2f | 2600 | |
3558fd73 | 2601 | if (mask & ATTR_MODE) { |
428870ff | 2602 | uint64_t pmode = zp->z_mode; |
572e2857 | 2603 | uint64_t acl_obj; |
5484965a | 2604 | new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); |
34dc7c2f | 2605 | |
572e2857 | 2606 | zfs_acl_chmod_setattr(zp, &aclp, new_mode); |
428870ff | 2607 | |
572e2857 BB |
2608 | mutex_enter(&zp->z_lock); |
2609 | if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { | |
428870ff BB |
2610 | /* |
2611 | * Are we upgrading ACL from old V0 format | |
2612 | * to V1 format? | |
2613 | */ | |
3558fd73 | 2614 | if (zsb->z_version >= ZPL_VERSION_FUID && |
572e2857 | 2615 | zfs_znode_acl_version(zp) == |
34dc7c2f | 2616 | ZFS_ACL_VERSION_INITIAL) { |
572e2857 | 2617 | dmu_tx_hold_free(tx, acl_obj, 0, |
34dc7c2f BB |
2618 | DMU_OBJECT_END); |
2619 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, | |
2620 | 0, aclp->z_acl_bytes); | |
2621 | } else { | |
572e2857 | 2622 | dmu_tx_hold_write(tx, acl_obj, 0, |
34dc7c2f BB |
2623 | aclp->z_acl_bytes); |
2624 | } | |
428870ff | 2625 | } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
34dc7c2f BB |
2626 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, |
2627 | 0, aclp->z_acl_bytes); | |
2628 | } | |
572e2857 | 2629 | mutex_exit(&zp->z_lock); |
428870ff BB |
2630 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); |
2631 | } else { | |
5484965a BB |
2632 | if ((mask & ATTR_XVATTR) && |
2633 | XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) | |
2634 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); | |
2635 | else | |
2636 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
2637 | } |
2638 | ||
428870ff BB |
2639 | if (attrzp) { |
2640 | dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
2641 | } |
2642 | ||
3558fd73 | 2643 | fuid_dirtied = zsb->z_fuid_dirty; |
428870ff | 2644 | if (fuid_dirtied) |
3558fd73 | 2645 | zfs_fuid_txhold(zsb, tx); |
428870ff BB |
2646 | |
2647 | zfs_sa_upgrade_txholds(tx, zp); | |
2648 | ||
fb5f0bc8 | 2649 | err = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f | 2650 | if (err) { |
9babb374 | 2651 | if (err == ERESTART) |
34dc7c2f | 2652 | dmu_tx_wait(tx); |
9babb374 | 2653 | goto out; |
34dc7c2f BB |
2654 | } |
2655 | ||
428870ff | 2656 | count = 0; |
34dc7c2f BB |
2657 | /* |
2658 | * Set each attribute requested. | |
2659 | * We group settings according to the locks they need to acquire. | |
2660 | * | |
2661 | * Note: you cannot set ctime directly, although it will be | |
2662 | * updated as a side-effect of calling this function. | |
2663 | */ | |
2664 | ||
572e2857 | 2665 | |
3558fd73 | 2666 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 2667 | mutex_enter(&zp->z_acl_lock); |
34dc7c2f BB |
2668 | mutex_enter(&zp->z_lock); |
2669 | ||
3558fd73 | 2670 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zsb), NULL, |
428870ff BB |
2671 | &zp->z_pflags, sizeof (zp->z_pflags)); |
2672 | ||
2673 | if (attrzp) { | |
3558fd73 | 2674 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 2675 | mutex_enter(&attrzp->z_acl_lock); |
428870ff BB |
2676 | mutex_enter(&attrzp->z_lock); |
2677 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
3558fd73 | 2678 | SA_ZPL_FLAGS(zsb), NULL, &attrzp->z_pflags, |
428870ff BB |
2679 | sizeof (attrzp->z_pflags)); |
2680 | } | |
2681 | ||
3558fd73 | 2682 | if (mask & (ATTR_UID|ATTR_GID)) { |
428870ff | 2683 | |
3558fd73 BB |
2684 | if (mask & ATTR_UID) { |
2685 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zsb), NULL, | |
428870ff | 2686 | &new_uid, sizeof (new_uid)); |
572e2857 | 2687 | zp->z_uid = new_uid; |
428870ff BB |
2688 | if (attrzp) { |
2689 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
3558fd73 | 2690 | SA_ZPL_UID(zsb), NULL, &new_uid, |
428870ff | 2691 | sizeof (new_uid)); |
572e2857 | 2692 | attrzp->z_uid = new_uid; |
428870ff BB |
2693 | } |
2694 | } | |
2695 | ||
3558fd73 BB |
2696 | if (mask & ATTR_GID) { |
2697 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zsb), | |
428870ff | 2698 | NULL, &new_gid, sizeof (new_gid)); |
572e2857 | 2699 | zp->z_gid = new_gid; |
428870ff BB |
2700 | if (attrzp) { |
2701 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
3558fd73 | 2702 | SA_ZPL_GID(zsb), NULL, &new_gid, |
428870ff | 2703 | sizeof (new_gid)); |
572e2857 | 2704 | attrzp->z_gid = new_gid; |
428870ff BB |
2705 | } |
2706 | } | |
3558fd73 BB |
2707 | if (!(mask & ATTR_MODE)) { |
2708 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), | |
428870ff BB |
2709 | NULL, &new_mode, sizeof (new_mode)); |
2710 | new_mode = zp->z_mode; | |
2711 | } | |
2712 | err = zfs_acl_chown_setattr(zp); | |
2713 | ASSERT(err == 0); | |
2714 | if (attrzp) { | |
2715 | err = zfs_acl_chown_setattr(attrzp); | |
2716 | ASSERT(err == 0); | |
2717 | } | |
2718 | } | |
2719 | ||
3558fd73 BB |
2720 | if (mask & ATTR_MODE) { |
2721 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zsb), NULL, | |
428870ff BB |
2722 | &new_mode, sizeof (new_mode)); |
2723 | zp->z_mode = new_mode; | |
99c564bc | 2724 | ASSERT3P(aclp, !=, NULL); |
9babb374 | 2725 | err = zfs_aclset_common(zp, aclp, cr, tx); |
34dc7c2f | 2726 | ASSERT3U(err, ==, 0); |
572e2857 BB |
2727 | if (zp->z_acl_cached) |
2728 | zfs_acl_free(zp->z_acl_cached); | |
45d1cae3 BB |
2729 | zp->z_acl_cached = aclp; |
2730 | aclp = NULL; | |
34dc7c2f BB |
2731 | } |
2732 | ||
34dc7c2f | 2733 | |
3558fd73 | 2734 | if (mask & ATTR_ATIME) { |
5484965a | 2735 | ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); |
3558fd73 | 2736 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zsb), NULL, |
428870ff | 2737 | &zp->z_atime, sizeof (zp->z_atime)); |
34dc7c2f BB |
2738 | } |
2739 | ||
3558fd73 | 2740 | if (mask & ATTR_MTIME) { |
5484965a | 2741 | ZFS_TIME_ENCODE(&vap->va_mtime, mtime); |
3558fd73 | 2742 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), NULL, |
428870ff | 2743 | mtime, sizeof (mtime)); |
34dc7c2f BB |
2744 | } |
2745 | ||
b128c09f | 2746 | /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ |
3558fd73 BB |
2747 | if (mask & ATTR_SIZE && !(mask & ATTR_MTIME)) { |
2748 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zsb), | |
428870ff | 2749 | NULL, mtime, sizeof (mtime)); |
3558fd73 | 2750 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, |
428870ff BB |
2751 | &ctime, sizeof (ctime)); |
2752 | zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, | |
2753 | B_TRUE); | |
2754 | } else if (mask != 0) { | |
3558fd73 | 2755 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zsb), NULL, |
428870ff BB |
2756 | &ctime, sizeof (ctime)); |
2757 | zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, | |
2758 | B_TRUE); | |
2759 | if (attrzp) { | |
2760 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
3558fd73 | 2761 | SA_ZPL_CTIME(zsb), NULL, |
428870ff BB |
2762 | &ctime, sizeof (ctime)); |
2763 | zfs_tstamp_update_setup(attrzp, STATE_CHANGED, | |
2764 | mtime, ctime, B_TRUE); | |
2765 | } | |
2766 | } | |
34dc7c2f BB |
2767 | /* |
2768 | * Do this after setting timestamps to prevent timestamp | |
2769 | * update from toggling bit | |
2770 | */ | |
2771 | ||
5484965a BB |
2772 | if (xoap && (mask & ATTR_XVATTR)) { |
2773 | ||
2774 | /* | |
2775 | * restore trimmed off masks | |
2776 | * so that return masks can be set for caller. | |
2777 | */ | |
2778 | ||
f4ea75d4 | 2779 | if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) { |
5484965a BB |
2780 | XVA_SET_REQ(xvap, XAT_APPENDONLY); |
2781 | } | |
f4ea75d4 | 2782 | if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) { |
5484965a BB |
2783 | XVA_SET_REQ(xvap, XAT_NOUNLINK); |
2784 | } | |
f4ea75d4 | 2785 | if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) { |
5484965a BB |
2786 | XVA_SET_REQ(xvap, XAT_IMMUTABLE); |
2787 | } | |
f4ea75d4 | 2788 | if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) { |
5484965a BB |
2789 | XVA_SET_REQ(xvap, XAT_NODUMP); |
2790 | } | |
f4ea75d4 | 2791 | if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) { |
5484965a BB |
2792 | XVA_SET_REQ(xvap, XAT_AV_MODIFIED); |
2793 | } | |
f4ea75d4 | 2794 | if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) { |
5484965a BB |
2795 | XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); |
2796 | } | |
2797 | ||
2798 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) | |
2799 | ASSERT(S_ISREG(ip->i_mode)); | |
2800 | ||
2801 | zfs_xvattr_set(zp, xvap, tx); | |
2802 | } | |
2803 | ||
9babb374 | 2804 | if (fuid_dirtied) |
3558fd73 | 2805 | zfs_fuid_sync(zsb, tx); |
9babb374 | 2806 | |
34dc7c2f | 2807 | if (mask != 0) |
5484965a | 2808 | zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); |
34dc7c2f | 2809 | |
34dc7c2f | 2810 | mutex_exit(&zp->z_lock); |
3558fd73 | 2811 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 2812 | mutex_exit(&zp->z_acl_lock); |
34dc7c2f | 2813 | |
572e2857 | 2814 | if (attrzp) { |
3558fd73 | 2815 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 BB |
2816 | mutex_exit(&attrzp->z_acl_lock); |
2817 | mutex_exit(&attrzp->z_lock); | |
2818 | } | |
9babb374 | 2819 | out: |
428870ff BB |
2820 | if (err == 0 && attrzp) { |
2821 | err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, | |
2822 | xattr_count, tx); | |
2823 | ASSERT(err2 == 0); | |
2824 | } | |
2825 | ||
34dc7c2f | 2826 | if (attrzp) |
3558fd73 | 2827 | iput(ZTOI(attrzp)); |
45d1cae3 | 2828 | if (aclp) |
9babb374 | 2829 | zfs_acl_free(aclp); |
9babb374 BB |
2830 | |
2831 | if (fuidp) { | |
2832 | zfs_fuid_info_free(fuidp); | |
2833 | fuidp = NULL; | |
2834 | } | |
2835 | ||
428870ff | 2836 | if (err) { |
9babb374 | 2837 | dmu_tx_abort(tx); |
428870ff BB |
2838 | if (err == ERESTART) |
2839 | goto top; | |
2840 | } else { | |
2841 | err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
9babb374 | 2842 | dmu_tx_commit(tx); |
037849f8 | 2843 | zfs_inode_update(zp); |
428870ff BB |
2844 | } |
2845 | ||
428870ff | 2846 | out2: |
3558fd73 | 2847 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 2848 | zil_commit(zilog, 0); |
34dc7c2f | 2849 | |
f4ea75d4 BB |
2850 | out3: |
2851 | kmem_free(tmpxvattr, sizeof(xvattr_t)); | |
3558fd73 | 2852 | ZFS_EXIT(zsb); |
34dc7c2f BB |
2853 | return (err); |
2854 | } | |
e5c39b95 | 2855 | EXPORT_SYMBOL(zfs_setattr); |
34dc7c2f BB |
2856 | |
2857 | typedef struct zfs_zlock { | |
2858 | krwlock_t *zl_rwlock; /* lock we acquired */ | |
2859 | znode_t *zl_znode; /* znode we held */ | |
2860 | struct zfs_zlock *zl_next; /* next in list */ | |
2861 | } zfs_zlock_t; | |
2862 | ||
2863 | /* | |
2864 | * Drop locks and release vnodes that were held by zfs_rename_lock(). | |
2865 | */ | |
2866 | static void | |
2867 | zfs_rename_unlock(zfs_zlock_t **zlpp) | |
2868 | { | |
2869 | zfs_zlock_t *zl; | |
2870 | ||
2871 | while ((zl = *zlpp) != NULL) { | |
2872 | if (zl->zl_znode != NULL) | |
3558fd73 | 2873 | iput(ZTOI(zl->zl_znode)); |
34dc7c2f BB |
2874 | rw_exit(zl->zl_rwlock); |
2875 | *zlpp = zl->zl_next; | |
2876 | kmem_free(zl, sizeof (*zl)); | |
2877 | } | |
2878 | } | |
2879 | ||
2880 | /* | |
2881 | * Search back through the directory tree, using the ".." entries. | |
2882 | * Lock each directory in the chain to prevent concurrent renames. | |
2883 | * Fail any attempt to move a directory into one of its own descendants. | |
2884 | * XXX - z_parent_lock can overlap with map or grow locks | |
2885 | */ | |
2886 | static int | |
2887 | zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) | |
2888 | { | |
2889 | zfs_zlock_t *zl; | |
2890 | znode_t *zp = tdzp; | |
3558fd73 | 2891 | uint64_t rootid = ZTOZSB(zp)->z_root; |
428870ff | 2892 | uint64_t oidp = zp->z_id; |
34dc7c2f BB |
2893 | krwlock_t *rwlp = &szp->z_parent_lock; |
2894 | krw_t rw = RW_WRITER; | |
2895 | ||
2896 | /* | |
2897 | * First pass write-locks szp and compares to zp->z_id. | |
2898 | * Later passes read-lock zp and compare to zp->z_parent. | |
2899 | */ | |
2900 | do { | |
2901 | if (!rw_tryenter(rwlp, rw)) { | |
2902 | /* | |
2903 | * Another thread is renaming in this path. | |
2904 | * Note that if we are a WRITER, we don't have any | |
2905 | * parent_locks held yet. | |
2906 | */ | |
2907 | if (rw == RW_READER && zp->z_id > szp->z_id) { | |
2908 | /* | |
2909 | * Drop our locks and restart | |
2910 | */ | |
2911 | zfs_rename_unlock(&zl); | |
2912 | *zlpp = NULL; | |
2913 | zp = tdzp; | |
428870ff | 2914 | oidp = zp->z_id; |
34dc7c2f BB |
2915 | rwlp = &szp->z_parent_lock; |
2916 | rw = RW_WRITER; | |
2917 | continue; | |
2918 | } else { | |
2919 | /* | |
2920 | * Wait for other thread to drop its locks | |
2921 | */ | |
2922 | rw_enter(rwlp, rw); | |
2923 | } | |
2924 | } | |
2925 | ||
2926 | zl = kmem_alloc(sizeof (*zl), KM_SLEEP); | |
2927 | zl->zl_rwlock = rwlp; | |
2928 | zl->zl_znode = NULL; | |
2929 | zl->zl_next = *zlpp; | |
2930 | *zlpp = zl; | |
2931 | ||
428870ff | 2932 | if (oidp == szp->z_id) /* We're a descendant of szp */ |
34dc7c2f BB |
2933 | return (EINVAL); |
2934 | ||
428870ff | 2935 | if (oidp == rootid) /* We've hit the top */ |
34dc7c2f BB |
2936 | return (0); |
2937 | ||
2938 | if (rw == RW_READER) { /* i.e. not the first pass */ | |
3558fd73 | 2939 | int error = zfs_zget(ZTOZSB(zp), oidp, &zp); |
34dc7c2f BB |
2940 | if (error) |
2941 | return (error); | |
2942 | zl->zl_znode = zp; | |
2943 | } | |
3558fd73 | 2944 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)), |
428870ff | 2945 | &oidp, sizeof (oidp)); |
34dc7c2f BB |
2946 | rwlp = &zp->z_parent_lock; |
2947 | rw = RW_READER; | |
2948 | ||
2949 | } while (zp->z_id != sdzp->z_id); | |
2950 | ||
2951 | return (0); | |
2952 | } | |
2953 | ||
2954 | /* | |
2955 | * Move an entry from the provided source directory to the target | |
2956 | * directory. Change the entry name as indicated. | |
2957 | * | |
3558fd73 | 2958 | * IN: sdip - Source directory containing the "old entry". |
34dc7c2f | 2959 | * snm - Old entry name. |
3558fd73 | 2960 | * tdip - Target directory to contain the "new entry". |
34dc7c2f BB |
2961 | * tnm - New entry name. |
2962 | * cr - credentials of caller. | |
34dc7c2f BB |
2963 | * flags - case flags |
2964 | * | |
2965 | * RETURN: 0 if success | |
2966 | * error code if failure | |
2967 | * | |
2968 | * Timestamps: | |
3558fd73 | 2969 | * sdip,tdip - ctime|mtime updated |
34dc7c2f BB |
2970 | */ |
2971 | /*ARGSUSED*/ | |
e5c39b95 | 2972 | int |
3558fd73 BB |
2973 | zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm, |
2974 | cred_t *cr, int flags) | |
34dc7c2f BB |
2975 | { |
2976 | znode_t *tdzp, *szp, *tzp; | |
3558fd73 BB |
2977 | znode_t *sdzp = ITOZ(sdip); |
2978 | zfs_sb_t *zsb = ITOZSB(sdip); | |
34dc7c2f | 2979 | zilog_t *zilog; |
34dc7c2f BB |
2980 | zfs_dirlock_t *sdl, *tdl; |
2981 | dmu_tx_t *tx; | |
2982 | zfs_zlock_t *zl; | |
2983 | int cmp, serr, terr; | |
2984 | int error = 0; | |
2985 | int zflg = 0; | |
2986 | ||
3558fd73 | 2987 | ZFS_ENTER(zsb); |
34dc7c2f | 2988 | ZFS_VERIFY_ZP(sdzp); |
3558fd73 | 2989 | zilog = zsb->z_log; |
34dc7c2f | 2990 | |
3558fd73 BB |
2991 | if (tdip->i_sb != sdip->i_sb) { |
2992 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
2993 | return (EXDEV); |
2994 | } | |
2995 | ||
3558fd73 | 2996 | tdzp = ITOZ(tdip); |
34dc7c2f | 2997 | ZFS_VERIFY_ZP(tdzp); |
3558fd73 | 2998 | if (zsb->z_utf8 && u8_validate(tnm, |
34dc7c2f | 2999 | strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
3558fd73 | 3000 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3001 | return (EILSEQ); |
3002 | } | |
3003 | ||
3004 | if (flags & FIGNORECASE) | |
3005 | zflg |= ZCILOOK; | |
3006 | ||
3007 | top: | |
3008 | szp = NULL; | |
3009 | tzp = NULL; | |
3010 | zl = NULL; | |
3011 | ||
3012 | /* | |
3013 | * This is to prevent the creation of links into attribute space | |
3014 | * by renaming a linked file into/outof an attribute directory. | |
3015 | * See the comment in zfs_link() for why this is considered bad. | |
3016 | */ | |
428870ff | 3017 | if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { |
3558fd73 | 3018 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3019 | return (EINVAL); |
3020 | } | |
3021 | ||
3022 | /* | |
3023 | * Lock source and target directory entries. To prevent deadlock, | |
3024 | * a lock ordering must be defined. We lock the directory with | |
3025 | * the smallest object id first, or if it's a tie, the one with | |
3026 | * the lexically first name. | |
3027 | */ | |
3028 | if (sdzp->z_id < tdzp->z_id) { | |
3029 | cmp = -1; | |
3030 | } else if (sdzp->z_id > tdzp->z_id) { | |
3031 | cmp = 1; | |
3032 | } else { | |
3033 | /* | |
3034 | * First compare the two name arguments without | |
3035 | * considering any case folding. | |
3036 | */ | |
3558fd73 | 3037 | int nofold = (zsb->z_norm & ~U8_TEXTPREP_TOUPPER); |
34dc7c2f BB |
3038 | |
3039 | cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); | |
3558fd73 | 3040 | ASSERT(error == 0 || !zsb->z_utf8); |
34dc7c2f BB |
3041 | if (cmp == 0) { |
3042 | /* | |
3043 | * POSIX: "If the old argument and the new argument | |
3044 | * both refer to links to the same existing file, | |
3045 | * the rename() function shall return successfully | |
3046 | * and perform no other action." | |
3047 | */ | |
3558fd73 | 3048 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3049 | return (0); |
3050 | } | |
3051 | /* | |
3052 | * If the file system is case-folding, then we may | |
3053 | * have some more checking to do. A case-folding file | |
3054 | * system is either supporting mixed case sensitivity | |
3055 | * access or is completely case-insensitive. Note | |
3056 | * that the file system is always case preserving. | |
3057 | * | |
3058 | * In mixed sensitivity mode case sensitive behavior | |
3059 | * is the default. FIGNORECASE must be used to | |
3060 | * explicitly request case insensitive behavior. | |
3061 | * | |
3062 | * If the source and target names provided differ only | |
3063 | * by case (e.g., a request to rename 'tim' to 'Tim'), | |
3064 | * we will treat this as a special case in the | |
3065 | * case-insensitive mode: as long as the source name | |
3066 | * is an exact match, we will allow this to proceed as | |
3067 | * a name-change request. | |
3068 | */ | |
3558fd73 BB |
3069 | if ((zsb->z_case == ZFS_CASE_INSENSITIVE || |
3070 | (zsb->z_case == ZFS_CASE_MIXED && | |
34dc7c2f | 3071 | flags & FIGNORECASE)) && |
3558fd73 | 3072 | u8_strcmp(snm, tnm, 0, zsb->z_norm, U8_UNICODE_LATEST, |
34dc7c2f BB |
3073 | &error) == 0) { |
3074 | /* | |
3075 | * case preserving rename request, require exact | |
3076 | * name matches | |
3077 | */ | |
3078 | zflg |= ZCIEXACT; | |
3079 | zflg &= ~ZCILOOK; | |
3080 | } | |
3081 | } | |
3082 | ||
428870ff BB |
3083 | /* |
3084 | * If the source and destination directories are the same, we should | |
3085 | * grab the z_name_lock of that directory only once. | |
3086 | */ | |
3087 | if (sdzp == tdzp) { | |
3088 | zflg |= ZHAVELOCK; | |
3089 | rw_enter(&sdzp->z_name_lock, RW_READER); | |
3090 | } | |
3091 | ||
34dc7c2f BB |
3092 | if (cmp < 0) { |
3093 | serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, | |
3094 | ZEXISTS | zflg, NULL, NULL); | |
3095 | terr = zfs_dirent_lock(&tdl, | |
3096 | tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); | |
3097 | } else { | |
3098 | terr = zfs_dirent_lock(&tdl, | |
3099 | tdzp, tnm, &tzp, zflg, NULL, NULL); | |
3100 | serr = zfs_dirent_lock(&sdl, | |
3101 | sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, | |
3102 | NULL, NULL); | |
3103 | } | |
3104 | ||
3105 | if (serr) { | |
3106 | /* | |
3107 | * Source entry invalid or not there. | |
3108 | */ | |
3109 | if (!terr) { | |
3110 | zfs_dirent_unlock(tdl); | |
3111 | if (tzp) | |
3558fd73 | 3112 | iput(ZTOI(tzp)); |
34dc7c2f | 3113 | } |
428870ff BB |
3114 | |
3115 | if (sdzp == tdzp) | |
3116 | rw_exit(&sdzp->z_name_lock); | |
3117 | ||
34dc7c2f BB |
3118 | if (strcmp(snm, "..") == 0) |
3119 | serr = EINVAL; | |
3558fd73 | 3120 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3121 | return (serr); |
3122 | } | |
3123 | if (terr) { | |
3124 | zfs_dirent_unlock(sdl); | |
3558fd73 | 3125 | iput(ZTOI(szp)); |
428870ff BB |
3126 | |
3127 | if (sdzp == tdzp) | |
3128 | rw_exit(&sdzp->z_name_lock); | |
3129 | ||
34dc7c2f BB |
3130 | if (strcmp(tnm, "..") == 0) |
3131 | terr = EINVAL; | |
3558fd73 | 3132 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3133 | return (terr); |
3134 | } | |
3135 | ||
3136 | /* | |
3137 | * Must have write access at the source to remove the old entry | |
3138 | * and write access at the target to create the new entry. | |
3139 | * Note that if target and source are the same, this can be | |
3140 | * done in a single check. | |
3141 | */ | |
3142 | ||
149e873a | 3143 | if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))) |
34dc7c2f BB |
3144 | goto out; |
3145 | ||
3558fd73 | 3146 | if (S_ISDIR(ZTOI(szp)->i_mode)) { |
34dc7c2f BB |
3147 | /* |
3148 | * Check to make sure rename is valid. | |
3149 | * Can't do a move like this: /usr/a/b to /usr/a/b/c/d | |
3150 | */ | |
149e873a | 3151 | if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl))) |
34dc7c2f BB |
3152 | goto out; |
3153 | } | |
3154 | ||
3155 | /* | |
3156 | * Does target exist? | |
3157 | */ | |
3158 | if (tzp) { | |
3159 | /* | |
3160 | * Source and target must be the same type. | |
3161 | */ | |
3558fd73 BB |
3162 | if (S_ISDIR(ZTOI(szp)->i_mode)) { |
3163 | if (!S_ISDIR(ZTOI(tzp)->i_mode)) { | |
34dc7c2f BB |
3164 | error = ENOTDIR; |
3165 | goto out; | |
3166 | } | |
3167 | } else { | |
3558fd73 | 3168 | if (S_ISDIR(ZTOI(tzp)->i_mode)) { |
34dc7c2f BB |
3169 | error = EISDIR; |
3170 | goto out; | |
3171 | } | |
3172 | } | |
3173 | /* | |
3174 | * POSIX dictates that when the source and target | |
3175 | * entries refer to the same file object, rename | |
3176 | * must do nothing and exit without error. | |
3177 | */ | |
3178 | if (szp->z_id == tzp->z_id) { | |
3179 | error = 0; | |
3180 | goto out; | |
3181 | } | |
3182 | } | |
3183 | ||
3558fd73 | 3184 | tx = dmu_tx_create(zsb->z_os); |
428870ff BB |
3185 | dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); |
3186 | dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
3187 | dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); |
3188 | dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); | |
428870ff BB |
3189 | if (sdzp != tdzp) { |
3190 | dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); | |
3191 | zfs_sa_upgrade_txholds(tx, tdzp); | |
3192 | } | |
3193 | if (tzp) { | |
3194 | dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); | |
3195 | zfs_sa_upgrade_txholds(tx, tzp); | |
3196 | } | |
3197 | ||
3198 | zfs_sa_upgrade_txholds(tx, szp); | |
3558fd73 | 3199 | dmu_tx_hold_zap(tx, zsb->z_unlinkedobj, FALSE, NULL); |
fb5f0bc8 | 3200 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
3201 | if (error) { |
3202 | if (zl != NULL) | |
3203 | zfs_rename_unlock(&zl); | |
3204 | zfs_dirent_unlock(sdl); | |
3205 | zfs_dirent_unlock(tdl); | |
428870ff BB |
3206 | |
3207 | if (sdzp == tdzp) | |
3208 | rw_exit(&sdzp->z_name_lock); | |
3209 | ||
3558fd73 | 3210 | iput(ZTOI(szp)); |
34dc7c2f | 3211 | if (tzp) |
3558fd73 | 3212 | iput(ZTOI(tzp)); |
fb5f0bc8 | 3213 | if (error == ERESTART) { |
34dc7c2f BB |
3214 | dmu_tx_wait(tx); |
3215 | dmu_tx_abort(tx); | |
3216 | goto top; | |
3217 | } | |
3218 | dmu_tx_abort(tx); | |
3558fd73 | 3219 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3220 | return (error); |
3221 | } | |
3222 | ||
3223 | if (tzp) /* Attempt to remove the existing target */ | |
3224 | error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); | |
3225 | ||
3226 | if (error == 0) { | |
3227 | error = zfs_link_create(tdl, szp, tx, ZRENAMING); | |
3228 | if (error == 0) { | |
428870ff | 3229 | szp->z_pflags |= ZFS_AV_MODIFIED; |
34dc7c2f | 3230 | |
3558fd73 | 3231 | error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zsb), |
428870ff BB |
3232 | (void *)&szp->z_pflags, sizeof (uint64_t), tx); |
3233 | ASSERT3U(error, ==, 0); | |
34dc7c2f | 3234 | |
428870ff BB |
3235 | error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); |
3236 | if (error == 0) { | |
3237 | zfs_log_rename(zilog, tx, TX_RENAME | | |
572e2857 BB |
3238 | (flags & FIGNORECASE ? TX_CI : 0), sdzp, |
3239 | sdl->dl_name, tdzp, tdl->dl_name, szp); | |
428870ff BB |
3240 | } else { |
3241 | /* | |
3242 | * At this point, we have successfully created | |
3243 | * the target name, but have failed to remove | |
3244 | * the source name. Since the create was done | |
3245 | * with the ZRENAMING flag, there are | |
3246 | * complications; for one, the link count is | |
3247 | * wrong. The easiest way to deal with this | |
3248 | * is to remove the newly created target, and | |
3249 | * return the original error. This must | |
3250 | * succeed; fortunately, it is very unlikely to | |
3251 | * fail, since we just created it. | |
3252 | */ | |
3253 | VERIFY3U(zfs_link_destroy(tdl, szp, tx, | |
3254 | ZRENAMING, NULL), ==, 0); | |
3255 | } | |
34dc7c2f BB |
3256 | } |
3257 | } | |
3258 | ||
3259 | dmu_tx_commit(tx); | |
3260 | out: | |
3261 | if (zl != NULL) | |
3262 | zfs_rename_unlock(&zl); | |
3263 | ||
3264 | zfs_dirent_unlock(sdl); | |
3265 | zfs_dirent_unlock(tdl); | |
3266 | ||
960e08fe | 3267 | zfs_inode_update(sdzp); |
428870ff BB |
3268 | if (sdzp == tdzp) |
3269 | rw_exit(&sdzp->z_name_lock); | |
3270 | ||
960e08fe BB |
3271 | if (sdzp != tdzp) |
3272 | zfs_inode_update(tdzp); | |
428870ff | 3273 | |
960e08fe | 3274 | zfs_inode_update(szp); |
3558fd73 | 3275 | iput(ZTOI(szp)); |
960e08fe BB |
3276 | if (tzp) { |
3277 | zfs_inode_update(tzp); | |
3558fd73 | 3278 | iput(ZTOI(tzp)); |
960e08fe | 3279 | } |
34dc7c2f | 3280 | |
3558fd73 | 3281 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3282 | zil_commit(zilog, 0); |
428870ff | 3283 | |
3558fd73 | 3284 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3285 | return (error); |
3286 | } | |
e5c39b95 | 3287 | EXPORT_SYMBOL(zfs_rename); |
34dc7c2f BB |
3288 | |
3289 | /* | |
3290 | * Insert the indicated symbolic reference entry into the directory. | |
3291 | * | |
3558fd73 | 3292 | * IN: dip - Directory to contain new symbolic link. |
34dc7c2f BB |
3293 | * link - Name for new symlink entry. |
3294 | * vap - Attributes of new entry. | |
3295 | * target - Target path of new symlink. | |
3558fd73 | 3296 | * |
34dc7c2f | 3297 | * cr - credentials of caller. |
34dc7c2f BB |
3298 | * flags - case flags |
3299 | * | |
3300 | * RETURN: 0 if success | |
3301 | * error code if failure | |
3302 | * | |
3303 | * Timestamps: | |
3558fd73 | 3304 | * dip - ctime|mtime updated |
34dc7c2f BB |
3305 | */ |
3306 | /*ARGSUSED*/ | |
e5c39b95 | 3307 | int |
3558fd73 BB |
3308 | zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link, |
3309 | struct inode **ipp, cred_t *cr, int flags) | |
34dc7c2f | 3310 | { |
3558fd73 | 3311 | znode_t *zp, *dzp = ITOZ(dip); |
34dc7c2f BB |
3312 | zfs_dirlock_t *dl; |
3313 | dmu_tx_t *tx; | |
3558fd73 | 3314 | zfs_sb_t *zsb = ITOZSB(dip); |
34dc7c2f | 3315 | zilog_t *zilog; |
428870ff | 3316 | uint64_t len = strlen(link); |
34dc7c2f BB |
3317 | int error; |
3318 | int zflg = ZNEW; | |
9babb374 BB |
3319 | zfs_acl_ids_t acl_ids; |
3320 | boolean_t fuid_dirtied; | |
428870ff | 3321 | uint64_t txtype = TX_SYMLINK; |
34dc7c2f | 3322 | |
3558fd73 | 3323 | ASSERT(S_ISLNK(vap->va_mode)); |
34dc7c2f | 3324 | |
3558fd73 | 3325 | ZFS_ENTER(zsb); |
34dc7c2f | 3326 | ZFS_VERIFY_ZP(dzp); |
3558fd73 | 3327 | zilog = zsb->z_log; |
34dc7c2f | 3328 | |
3558fd73 | 3329 | if (zsb->z_utf8 && u8_validate(name, strlen(name), |
34dc7c2f | 3330 | NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
3558fd73 | 3331 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3332 | return (EILSEQ); |
3333 | } | |
3334 | if (flags & FIGNORECASE) | |
3335 | zflg |= ZCILOOK; | |
34dc7c2f BB |
3336 | |
3337 | if (len > MAXPATHLEN) { | |
3558fd73 | 3338 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3339 | return (ENAMETOOLONG); |
3340 | } | |
3341 | ||
428870ff BB |
3342 | if ((error = zfs_acl_ids_create(dzp, 0, |
3343 | vap, cr, NULL, &acl_ids)) != 0) { | |
3558fd73 | 3344 | ZFS_EXIT(zsb); |
428870ff BB |
3345 | return (error); |
3346 | } | |
3347 | top: | |
3558fd73 BB |
3348 | *ipp = NULL; |
3349 | ||
34dc7c2f BB |
3350 | /* |
3351 | * Attempt to lock directory; fail if entry already exists. | |
3352 | */ | |
3353 | error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); | |
3354 | if (error) { | |
428870ff | 3355 | zfs_acl_ids_free(&acl_ids); |
3558fd73 | 3356 | ZFS_EXIT(zsb); |
428870ff BB |
3357 | return (error); |
3358 | } | |
3359 | ||
149e873a | 3360 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
428870ff BB |
3361 | zfs_acl_ids_free(&acl_ids); |
3362 | zfs_dirent_unlock(dl); | |
3558fd73 | 3363 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3364 | return (error); |
3365 | } | |
3366 | ||
3558fd73 | 3367 | if (zfs_acl_ids_overquota(zsb, &acl_ids)) { |
9babb374 BB |
3368 | zfs_acl_ids_free(&acl_ids); |
3369 | zfs_dirent_unlock(dl); | |
3558fd73 | 3370 | ZFS_EXIT(zsb); |
9babb374 BB |
3371 | return (EDQUOT); |
3372 | } | |
3558fd73 BB |
3373 | tx = dmu_tx_create(zsb->z_os); |
3374 | fuid_dirtied = zsb->z_fuid_dirty; | |
34dc7c2f | 3375 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); |
34dc7c2f | 3376 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
428870ff BB |
3377 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + |
3378 | ZFS_SA_BASE_ATTR_SIZE + len); | |
3379 | dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); | |
3558fd73 | 3380 | if (!zsb->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
428870ff BB |
3381 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, |
3382 | acl_ids.z_aclp->z_acl_bytes); | |
3383 | } | |
9babb374 | 3384 | if (fuid_dirtied) |
3558fd73 | 3385 | zfs_fuid_txhold(zsb, tx); |
fb5f0bc8 | 3386 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
3387 | if (error) { |
3388 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 3389 | if (error == ERESTART) { |
34dc7c2f BB |
3390 | dmu_tx_wait(tx); |
3391 | dmu_tx_abort(tx); | |
3392 | goto top; | |
3393 | } | |
428870ff | 3394 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 3395 | dmu_tx_abort(tx); |
3558fd73 | 3396 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3397 | return (error); |
3398 | } | |
3399 | ||
34dc7c2f BB |
3400 | /* |
3401 | * Create a new object for the symlink. | |
428870ff | 3402 | * for version 4 ZPL datsets the symlink will be an SA attribute |
34dc7c2f | 3403 | */ |
428870ff | 3404 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
9babb374 | 3405 | |
428870ff | 3406 | if (fuid_dirtied) |
3558fd73 | 3407 | zfs_fuid_sync(zsb, tx); |
34dc7c2f | 3408 | |
572e2857 | 3409 | mutex_enter(&zp->z_lock); |
428870ff | 3410 | if (zp->z_is_sa) |
3558fd73 | 3411 | error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zsb), |
428870ff BB |
3412 | link, len, tx); |
3413 | else | |
3414 | zfs_sa_symlink(zp, link, len, tx); | |
572e2857 | 3415 | mutex_exit(&zp->z_lock); |
34dc7c2f | 3416 | |
428870ff | 3417 | zp->z_size = len; |
3558fd73 | 3418 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zsb), |
428870ff | 3419 | &zp->z_size, sizeof (zp->z_size), tx); |
34dc7c2f BB |
3420 | /* |
3421 | * Insert the new object into the directory. | |
3422 | */ | |
3423 | (void) zfs_link_create(dl, zp, tx, ZNEW); | |
428870ff BB |
3424 | |
3425 | if (flags & FIGNORECASE) | |
3426 | txtype |= TX_CI; | |
3427 | zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); | |
9babb374 | 3428 | |
960e08fe BB |
3429 | zfs_inode_update(dzp); |
3430 | zfs_inode_update(zp); | |
3431 | ||
9babb374 | 3432 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f BB |
3433 | |
3434 | dmu_tx_commit(tx); | |
3435 | ||
3436 | zfs_dirent_unlock(dl); | |
3437 | ||
3558fd73 | 3438 | *ipp = ZTOI(zp); |
34dc7c2f | 3439 | |
3558fd73 | 3440 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3441 | zil_commit(zilog, 0); |
428870ff | 3442 | |
3558fd73 | 3443 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3444 | return (error); |
3445 | } | |
e5c39b95 | 3446 | EXPORT_SYMBOL(zfs_symlink); |
34dc7c2f BB |
3447 | |
3448 | /* | |
3449 | * Return, in the buffer contained in the provided uio structure, | |
3558fd73 | 3450 | * the symbolic path referred to by ip. |
34dc7c2f | 3451 | * |
8b4f9a2d BB |
3452 | * IN: ip - inode of symbolic link |
3453 | * uio - structure to contain the link path. | |
3454 | * cr - credentials of caller. | |
34dc7c2f BB |
3455 | * |
3456 | * RETURN: 0 if success | |
3457 | * error code if failure | |
3458 | * | |
3459 | * Timestamps: | |
3558fd73 | 3460 | * ip - atime updated |
34dc7c2f BB |
3461 | */ |
3462 | /* ARGSUSED */ | |
e5c39b95 | 3463 | int |
8b4f9a2d | 3464 | zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr) |
34dc7c2f | 3465 | { |
3558fd73 BB |
3466 | znode_t *zp = ITOZ(ip); |
3467 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
3468 | int error; |
3469 | ||
3558fd73 | 3470 | ZFS_ENTER(zsb); |
34dc7c2f BB |
3471 | ZFS_VERIFY_ZP(zp); |
3472 | ||
572e2857 | 3473 | mutex_enter(&zp->z_lock); |
428870ff | 3474 | if (zp->z_is_sa) |
8b4f9a2d BB |
3475 | error = sa_lookup_uio(zp->z_sa_hdl, |
3476 | SA_ZPL_SYMLINK(zsb), uio); | |
428870ff | 3477 | else |
8b4f9a2d | 3478 | error = zfs_sa_readlink(zp, uio); |
572e2857 | 3479 | mutex_exit(&zp->z_lock); |
34dc7c2f | 3480 | |
3558fd73 | 3481 | ZFS_ACCESSTIME_STAMP(zsb, zp); |
960e08fe | 3482 | zfs_inode_update(zp); |
3558fd73 | 3483 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3484 | return (error); |
3485 | } | |
8b4f9a2d | 3486 | EXPORT_SYMBOL(zfs_readlink); |
34dc7c2f BB |
3487 | |
3488 | /* | |
3558fd73 | 3489 | * Insert a new entry into directory tdip referencing sip. |
34dc7c2f | 3490 | * |
3558fd73 BB |
3491 | * IN: tdip - Directory to contain new entry. |
3492 | * sip - inode of new entry. | |
34dc7c2f BB |
3493 | * name - name of new entry. |
3494 | * cr - credentials of caller. | |
34dc7c2f BB |
3495 | * |
3496 | * RETURN: 0 if success | |
3497 | * error code if failure | |
3498 | * | |
3499 | * Timestamps: | |
3558fd73 BB |
3500 | * tdip - ctime|mtime updated |
3501 | * sip - ctime updated | |
34dc7c2f BB |
3502 | */ |
3503 | /* ARGSUSED */ | |
e5c39b95 | 3504 | int |
3558fd73 | 3505 | zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr) |
34dc7c2f | 3506 | { |
3558fd73 | 3507 | znode_t *dzp = ITOZ(tdip); |
34dc7c2f | 3508 | znode_t *tzp, *szp; |
3558fd73 | 3509 | zfs_sb_t *zsb = ITOZSB(tdip); |
34dc7c2f BB |
3510 | zilog_t *zilog; |
3511 | zfs_dirlock_t *dl; | |
3512 | dmu_tx_t *tx; | |
34dc7c2f BB |
3513 | int error; |
3514 | int zf = ZNEW; | |
428870ff | 3515 | uint64_t parent; |
572e2857 | 3516 | uid_t owner; |
34dc7c2f | 3517 | |
3558fd73 | 3518 | ASSERT(S_ISDIR(tdip->i_mode)); |
34dc7c2f | 3519 | |
3558fd73 | 3520 | ZFS_ENTER(zsb); |
34dc7c2f | 3521 | ZFS_VERIFY_ZP(dzp); |
3558fd73 | 3522 | zilog = zsb->z_log; |
34dc7c2f | 3523 | |
428870ff BB |
3524 | /* |
3525 | * POSIX dictates that we return EPERM here. | |
3526 | * Better choices include ENOTSUP or EISDIR. | |
3527 | */ | |
3558fd73 BB |
3528 | if (S_ISDIR(sip->i_mode)) { |
3529 | ZFS_EXIT(zsb); | |
428870ff BB |
3530 | return (EPERM); |
3531 | } | |
3532 | ||
3558fd73 BB |
3533 | if (sip->i_sb != tdip->i_sb) { |
3534 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
3535 | return (EXDEV); |
3536 | } | |
428870ff | 3537 | |
3558fd73 | 3538 | szp = ITOZ(sip); |
34dc7c2f BB |
3539 | ZFS_VERIFY_ZP(szp); |
3540 | ||
428870ff BB |
3541 | /* Prevent links to .zfs/shares files */ |
3542 | ||
3558fd73 | 3543 | if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zsb), |
428870ff | 3544 | &parent, sizeof (uint64_t))) != 0) { |
3558fd73 | 3545 | ZFS_EXIT(zsb); |
428870ff BB |
3546 | return (error); |
3547 | } | |
3558fd73 BB |
3548 | if (parent == zsb->z_shares_dir) { |
3549 | ZFS_EXIT(zsb); | |
428870ff BB |
3550 | return (EPERM); |
3551 | } | |
3552 | ||
3558fd73 | 3553 | if (zsb->z_utf8 && u8_validate(name, |
34dc7c2f | 3554 | strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
3558fd73 | 3555 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3556 | return (EILSEQ); |
3557 | } | |
3558fd73 | 3558 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
3559 | if (flags & FIGNORECASE) |
3560 | zf |= ZCILOOK; | |
3558fd73 | 3561 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f | 3562 | |
34dc7c2f BB |
3563 | /* |
3564 | * We do not support links between attributes and non-attributes | |
3565 | * because of the potential security risk of creating links | |
3566 | * into "normal" file space in order to circumvent restrictions | |
3567 | * imposed in attribute space. | |
3568 | */ | |
428870ff | 3569 | if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { |
3558fd73 | 3570 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3571 | return (EINVAL); |
3572 | } | |
3573 | ||
3558fd73 | 3574 | owner = zfs_fuid_map_id(zsb, szp->z_uid, cr, ZFS_OWNER); |
572e2857 | 3575 | if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) { |
3558fd73 | 3576 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3577 | return (EPERM); |
3578 | } | |
3579 | ||
149e873a | 3580 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
3558fd73 | 3581 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3582 | return (error); |
3583 | } | |
3584 | ||
428870ff | 3585 | top: |
34dc7c2f BB |
3586 | /* |
3587 | * Attempt to lock directory; fail if entry already exists. | |
3588 | */ | |
3589 | error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); | |
3590 | if (error) { | |
3558fd73 | 3591 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3592 | return (error); |
3593 | } | |
3594 | ||
3558fd73 | 3595 | tx = dmu_tx_create(zsb->z_os); |
428870ff | 3596 | dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); |
34dc7c2f | 3597 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
428870ff BB |
3598 | zfs_sa_upgrade_txholds(tx, szp); |
3599 | zfs_sa_upgrade_txholds(tx, dzp); | |
fb5f0bc8 | 3600 | error = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f BB |
3601 | if (error) { |
3602 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 3603 | if (error == ERESTART) { |
34dc7c2f BB |
3604 | dmu_tx_wait(tx); |
3605 | dmu_tx_abort(tx); | |
3606 | goto top; | |
3607 | } | |
3608 | dmu_tx_abort(tx); | |
3558fd73 | 3609 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3610 | return (error); |
3611 | } | |
3612 | ||
3613 | error = zfs_link_create(dl, szp, tx, 0); | |
3614 | ||
3615 | if (error == 0) { | |
3616 | uint64_t txtype = TX_LINK; | |
3558fd73 | 3617 | #ifdef HAVE_PN_UTILS |
34dc7c2f BB |
3618 | if (flags & FIGNORECASE) |
3619 | txtype |= TX_CI; | |
3558fd73 | 3620 | #endif /* HAVE_PN_UTILS */ |
34dc7c2f BB |
3621 | zfs_log_link(zilog, tx, txtype, dzp, szp, name); |
3622 | } | |
3623 | ||
3624 | dmu_tx_commit(tx); | |
3625 | ||
3626 | zfs_dirent_unlock(dl); | |
3627 | ||
3558fd73 | 3628 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3629 | zil_commit(zilog, 0); |
428870ff | 3630 | |
960e08fe BB |
3631 | zfs_inode_update(dzp); |
3632 | zfs_inode_update(szp); | |
3558fd73 | 3633 | ZFS_EXIT(zsb); |
34dc7c2f BB |
3634 | return (error); |
3635 | } | |
e5c39b95 | 3636 | EXPORT_SYMBOL(zfs_link); |
34dc7c2f | 3637 | |
c0d35759 | 3638 | #ifdef HAVE_MMAP |
34dc7c2f BB |
3639 | /* |
3640 | * zfs_null_putapage() is used when the file system has been force | |
3641 | * unmounted. It just drops the pages. | |
3642 | */ | |
3643 | /* ARGSUSED */ | |
3644 | static int | |
3645 | zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, | |
3646 | size_t *lenp, int flags, cred_t *cr) | |
3647 | { | |
3648 | pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); | |
3649 | return (0); | |
3650 | } | |
3651 | ||
3652 | /* | |
3653 | * Push a page out to disk, klustering if possible. | |
3654 | * | |
3655 | * IN: vp - file to push page to. | |
3656 | * pp - page to push. | |
3657 | * flags - additional flags. | |
3658 | * cr - credentials of caller. | |
3659 | * | |
3660 | * OUT: offp - start of range pushed. | |
3661 | * lenp - len of range pushed. | |
3662 | * | |
3663 | * RETURN: 0 if success | |
3664 | * error code if failure | |
3665 | * | |
3666 | * NOTE: callers must have locked the page to be pushed. On | |
3667 | * exit, the page (and all other pages in the kluster) must be | |
3668 | * unlocked. | |
3669 | */ | |
3670 | /* ARGSUSED */ | |
3671 | static int | |
3672 | zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, | |
3673 | size_t *lenp, int flags, cred_t *cr) | |
3674 | { | |
3675 | znode_t *zp = VTOZ(vp); | |
3676 | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | |
34dc7c2f | 3677 | dmu_tx_t *tx; |
34dc7c2f BB |
3678 | u_offset_t off, koff; |
3679 | size_t len, klen; | |
34dc7c2f BB |
3680 | int err; |
3681 | ||
34dc7c2f BB |
3682 | off = pp->p_offset; |
3683 | len = PAGESIZE; | |
3684 | /* | |
3685 | * If our blocksize is bigger than the page size, try to kluster | |
fb5f0bc8 | 3686 | * multiple pages so that we write a full block (thus avoiding |
34dc7c2f BB |
3687 | * a read-modify-write). |
3688 | */ | |
428870ff | 3689 | if (off < zp->z_size && zp->z_blksz > PAGESIZE) { |
d164b209 BB |
3690 | klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); |
3691 | koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; | |
428870ff BB |
3692 | ASSERT(koff <= zp->z_size); |
3693 | if (koff + klen > zp->z_size) | |
3694 | klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); | |
34dc7c2f BB |
3695 | pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); |
3696 | } | |
3697 | ASSERT3U(btop(len), ==, btopr(len)); | |
d164b209 | 3698 | |
34dc7c2f BB |
3699 | /* |
3700 | * Can't push pages past end-of-file. | |
3701 | */ | |
428870ff | 3702 | if (off >= zp->z_size) { |
34dc7c2f BB |
3703 | /* ignore all pages */ |
3704 | err = 0; | |
3705 | goto out; | |
428870ff BB |
3706 | } else if (off + len > zp->z_size) { |
3707 | int npages = btopr(zp->z_size - off); | |
34dc7c2f BB |
3708 | page_t *trunc; |
3709 | ||
3710 | page_list_break(&pp, &trunc, npages); | |
3711 | /* ignore pages past end of file */ | |
3712 | if (trunc) | |
3713 | pvn_write_done(trunc, flags); | |
428870ff | 3714 | len = zp->z_size - off; |
34dc7c2f | 3715 | } |
9babb374 | 3716 | |
428870ff BB |
3717 | if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || |
3718 | zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { | |
9babb374 BB |
3719 | err = EDQUOT; |
3720 | goto out; | |
3721 | } | |
d164b209 | 3722 | top: |
34dc7c2f BB |
3723 | tx = dmu_tx_create(zfsvfs->z_os); |
3724 | dmu_tx_hold_write(tx, zp->z_id, off, len); | |
428870ff BB |
3725 | |
3726 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); | |
3727 | zfs_sa_upgrade_txholds(tx, zp); | |
fb5f0bc8 | 3728 | err = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f | 3729 | if (err != 0) { |
fb5f0bc8 | 3730 | if (err == ERESTART) { |
34dc7c2f BB |
3731 | dmu_tx_wait(tx); |
3732 | dmu_tx_abort(tx); | |
34dc7c2f BB |
3733 | goto top; |
3734 | } | |
3735 | dmu_tx_abort(tx); | |
3736 | goto out; | |
3737 | } | |
3738 | ||
3739 | if (zp->z_blksz <= PAGESIZE) { | |
b128c09f | 3740 | caddr_t va = zfs_map_page(pp, S_READ); |
34dc7c2f BB |
3741 | ASSERT3U(len, <=, PAGESIZE); |
3742 | dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); | |
b128c09f | 3743 | zfs_unmap_page(pp, va); |
34dc7c2f BB |
3744 | } else { |
3745 | err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); | |
3746 | } | |
3747 | ||
3748 | if (err == 0) { | |
428870ff BB |
3749 | uint64_t mtime[2], ctime[2]; |
3750 | sa_bulk_attr_t bulk[3]; | |
3751 | int count = 0; | |
3752 | ||
3753 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, | |
3754 | &mtime, 16); | |
3755 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, | |
3756 | &ctime, 16); | |
3757 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, | |
3758 | &zp->z_pflags, 8); | |
3759 | zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, | |
3760 | B_TRUE); | |
d164b209 | 3761 | zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); |
34dc7c2f | 3762 | } |
45d1cae3 | 3763 | dmu_tx_commit(tx); |
34dc7c2f BB |
3764 | |
3765 | out: | |
34dc7c2f BB |
3766 | pvn_write_done(pp, (err ? B_ERROR : 0) | flags); |
3767 | if (offp) | |
3768 | *offp = off; | |
3769 | if (lenp) | |
3770 | *lenp = len; | |
3771 | ||
3772 | return (err); | |
3773 | } | |
3774 | ||
3775 | /* | |
3776 | * Copy the portion of the file indicated from pages into the file. | |
3777 | * The pages are stored in a page list attached to the files vnode. | |
3778 | * | |
3779 | * IN: vp - vnode of file to push page data to. | |
3780 | * off - position in file to put data. | |
3781 | * len - amount of data to write. | |
3782 | * flags - flags to control the operation. | |
3783 | * cr - credentials of caller. | |
3784 | * ct - caller context. | |
3785 | * | |
3786 | * RETURN: 0 if success | |
3787 | * error code if failure | |
3788 | * | |
3789 | * Timestamps: | |
3790 | * vp - ctime|mtime updated | |
3791 | */ | |
3792 | /*ARGSUSED*/ | |
3793 | static int | |
9623f736 | 3794 | zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) |
34dc7c2f BB |
3795 | { |
3796 | znode_t *zp = VTOZ(vp); | |
3797 | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | |
3798 | page_t *pp; | |
3799 | size_t io_len; | |
3800 | u_offset_t io_off; | |
d164b209 BB |
3801 | uint_t blksz; |
3802 | rl_t *rl; | |
34dc7c2f BB |
3803 | int error = 0; |
3804 | ||
3805 | ZFS_ENTER(zfsvfs); | |
3806 | ZFS_VERIFY_ZP(zp); | |
3807 | ||
d164b209 BB |
3808 | /* |
3809 | * Align this request to the file block size in case we kluster. | |
3810 | * XXX - this can result in pretty aggresive locking, which can | |
3811 | * impact simultanious read/write access. One option might be | |
3812 | * to break up long requests (len == 0) into block-by-block | |
3813 | * operations to get narrower locking. | |
3814 | */ | |
3815 | blksz = zp->z_blksz; | |
3816 | if (ISP2(blksz)) | |
3817 | io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); | |
3818 | else | |
3819 | io_off = 0; | |
3820 | if (len > 0 && ISP2(blksz)) | |
9babb374 | 3821 | io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); |
d164b209 BB |
3822 | else |
3823 | io_len = 0; | |
3824 | ||
3825 | if (io_len == 0) { | |
34dc7c2f | 3826 | /* |
d164b209 | 3827 | * Search the entire vp list for pages >= io_off. |
34dc7c2f | 3828 | */ |
d164b209 BB |
3829 | rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); |
3830 | error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); | |
34dc7c2f BB |
3831 | goto out; |
3832 | } | |
d164b209 | 3833 | rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); |
34dc7c2f | 3834 | |
428870ff | 3835 | if (off > zp->z_size) { |
34dc7c2f | 3836 | /* past end of file */ |
d164b209 | 3837 | zfs_range_unlock(rl); |
34dc7c2f BB |
3838 | ZFS_EXIT(zfsvfs); |
3839 | return (0); | |
3840 | } | |
3841 | ||
428870ff | 3842 | len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); |
34dc7c2f | 3843 | |
d164b209 | 3844 | for (off = io_off; io_off < off + len; io_off += io_len) { |
34dc7c2f BB |
3845 | if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { |
3846 | pp = page_lookup(vp, io_off, | |
3847 | (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); | |
3848 | } else { | |
3849 | pp = page_lookup_nowait(vp, io_off, | |
3850 | (flags & B_FREE) ? SE_EXCL : SE_SHARED); | |
3851 | } | |
3852 | ||
3853 | if (pp != NULL && pvn_getdirty(pp, flags)) { | |
3854 | int err; | |
3855 | ||
3856 | /* | |
3857 | * Found a dirty page to push | |
3858 | */ | |
3859 | err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); | |
3860 | if (err) | |
3861 | error = err; | |
3862 | } else { | |
3863 | io_len = PAGESIZE; | |
3864 | } | |
3865 | } | |
3866 | out: | |
d164b209 | 3867 | zfs_range_unlock(rl); |
428870ff | 3868 | if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3869 | zil_commit(zfsvfs->z_log, zp->z_id); |
34dc7c2f BB |
3870 | ZFS_EXIT(zfsvfs); |
3871 | return (error); | |
3872 | } | |
c0d35759 | 3873 | #endif /* HAVE_MMAP */ |
34dc7c2f BB |
3874 | |
3875 | /*ARGSUSED*/ | |
3876 | void | |
c0d35759 | 3877 | zfs_inactive(struct inode *ip) |
34dc7c2f | 3878 | { |
c0d35759 BB |
3879 | znode_t *zp = ITOZ(ip); |
3880 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
3881 | int error; |
3882 | ||
c0d35759 BB |
3883 | #ifdef HAVE_SNAPSHOT |
3884 | /* Early return for snapshot inode? */ | |
3885 | #endif /* HAVE_SNAPSHOT */ | |
34dc7c2f | 3886 | |
c0d35759 BB |
3887 | rw_enter(&zsb->z_teardown_inactive_lock, RW_READER); |
3888 | if (zp->z_sa_hdl == NULL) { | |
3889 | rw_exit(&zsb->z_teardown_inactive_lock); | |
3890 | return; | |
34dc7c2f BB |
3891 | } |
3892 | ||
3893 | if (zp->z_atime_dirty && zp->z_unlinked == 0) { | |
c0d35759 | 3894 | dmu_tx_t *tx = dmu_tx_create(zsb->z_os); |
34dc7c2f | 3895 | |
428870ff BB |
3896 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
3897 | zfs_sa_upgrade_txholds(tx, zp); | |
34dc7c2f BB |
3898 | error = dmu_tx_assign(tx, TXG_WAIT); |
3899 | if (error) { | |
3900 | dmu_tx_abort(tx); | |
3901 | } else { | |
34dc7c2f | 3902 | mutex_enter(&zp->z_lock); |
3558fd73 | 3903 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zsb), |
428870ff | 3904 | (void *)&zp->z_atime, sizeof (zp->z_atime), tx); |
34dc7c2f BB |
3905 | zp->z_atime_dirty = 0; |
3906 | mutex_exit(&zp->z_lock); | |
3907 | dmu_tx_commit(tx); | |
3908 | } | |
3909 | } | |
3910 | ||
3911 | zfs_zinactive(zp); | |
3558fd73 | 3912 | rw_exit(&zsb->z_teardown_inactive_lock); |
34dc7c2f | 3913 | } |
e5c39b95 | 3914 | EXPORT_SYMBOL(zfs_inactive); |
34dc7c2f BB |
3915 | |
3916 | /* | |
3917 | * Bounds-check the seek operation. | |
3918 | * | |
3558fd73 | 3919 | * IN: ip - inode seeking within |
34dc7c2f BB |
3920 | * ooff - old file offset |
3921 | * noffp - pointer to new file offset | |
3922 | * ct - caller context | |
3923 | * | |
3924 | * RETURN: 0 if success | |
3925 | * EINVAL if new offset invalid | |
3926 | */ | |
3927 | /* ARGSUSED */ | |
3558fd73 | 3928 | int |
9623f736 | 3929 | zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp) |
34dc7c2f | 3930 | { |
3558fd73 | 3931 | if (S_ISDIR(ip->i_mode)) |
34dc7c2f BB |
3932 | return (0); |
3933 | return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); | |
3934 | } | |
3558fd73 | 3935 | EXPORT_SYMBOL(zfs_seek); |
34dc7c2f | 3936 | |
c0d35759 | 3937 | #ifdef HAVE_MMAP |
34dc7c2f BB |
3938 | /* |
3939 | * Pre-filter the generic locking function to trap attempts to place | |
3940 | * a mandatory lock on a memory mapped file. | |
3941 | */ | |
3942 | static int | |
3943 | zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, | |
9623f736 | 3944 | flk_callback_t *flk_cbp, cred_t *cr) |
34dc7c2f BB |
3945 | { |
3946 | znode_t *zp = VTOZ(vp); | |
3947 | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | |
34dc7c2f BB |
3948 | |
3949 | ZFS_ENTER(zfsvfs); | |
3950 | ZFS_VERIFY_ZP(zp); | |
3951 | ||
3952 | /* | |
3953 | * We are following the UFS semantics with respect to mapcnt | |
3954 | * here: If we see that the file is mapped already, then we will | |
3955 | * return an error, but we don't worry about races between this | |
3956 | * function and zfs_map(). | |
3957 | */ | |
428870ff | 3958 | if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { |
34dc7c2f BB |
3959 | ZFS_EXIT(zfsvfs); |
3960 | return (EAGAIN); | |
3961 | } | |
34dc7c2f | 3962 | ZFS_EXIT(zfsvfs); |
428870ff | 3963 | return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); |
34dc7c2f BB |
3964 | } |
3965 | ||
3966 | /* | |
3967 | * If we can't find a page in the cache, we will create a new page | |
3968 | * and fill it with file data. For efficiency, we may try to fill | |
d164b209 | 3969 | * multiple pages at once (klustering) to fill up the supplied page |
9babb374 BB |
3970 | * list. Note that the pages to be filled are held with an exclusive |
3971 | * lock to prevent access by other threads while they are being filled. | |
34dc7c2f BB |
3972 | */ |
3973 | static int | |
3974 | zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, | |
3975 | caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) | |
3976 | { | |
3977 | znode_t *zp = VTOZ(vp); | |
3978 | page_t *pp, *cur_pp; | |
3979 | objset_t *os = zp->z_zfsvfs->z_os; | |
34dc7c2f | 3980 | u_offset_t io_off, total; |
34dc7c2f | 3981 | size_t io_len; |
34dc7c2f BB |
3982 | int err; |
3983 | ||
34dc7c2f | 3984 | if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { |
d164b209 BB |
3985 | /* |
3986 | * We only have a single page, don't bother klustering | |
3987 | */ | |
34dc7c2f BB |
3988 | io_off = off; |
3989 | io_len = PAGESIZE; | |
9babb374 BB |
3990 | pp = page_create_va(vp, io_off, io_len, |
3991 | PG_EXCL | PG_WAIT, seg, addr); | |
34dc7c2f BB |
3992 | } else { |
3993 | /* | |
d164b209 | 3994 | * Try to find enough pages to fill the page list |
34dc7c2f | 3995 | */ |
34dc7c2f | 3996 | pp = pvn_read_kluster(vp, off, seg, addr, &io_off, |
d164b209 | 3997 | &io_len, off, plsz, 0); |
34dc7c2f BB |
3998 | } |
3999 | if (pp == NULL) { | |
4000 | /* | |
d164b209 | 4001 | * The page already exists, nothing to do here. |
34dc7c2f BB |
4002 | */ |
4003 | *pl = NULL; | |
4004 | return (0); | |
4005 | } | |
4006 | ||
4007 | /* | |
4008 | * Fill the pages in the kluster. | |
4009 | */ | |
4010 | cur_pp = pp; | |
4011 | for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { | |
d164b209 BB |
4012 | caddr_t va; |
4013 | ||
34dc7c2f | 4014 | ASSERT3U(io_off, ==, cur_pp->p_offset); |
b128c09f | 4015 | va = zfs_map_page(cur_pp, S_WRITE); |
9babb374 BB |
4016 | err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, |
4017 | DMU_READ_PREFETCH); | |
b128c09f | 4018 | zfs_unmap_page(cur_pp, va); |
34dc7c2f BB |
4019 | if (err) { |
4020 | /* On error, toss the entire kluster */ | |
4021 | pvn_read_done(pp, B_ERROR); | |
b128c09f BB |
4022 | /* convert checksum errors into IO errors */ |
4023 | if (err == ECKSUM) | |
4024 | err = EIO; | |
34dc7c2f BB |
4025 | return (err); |
4026 | } | |
4027 | cur_pp = cur_pp->p_next; | |
4028 | } | |
d164b209 | 4029 | |
34dc7c2f | 4030 | /* |
d164b209 BB |
4031 | * Fill in the page list array from the kluster starting |
4032 | * from the desired offset `off'. | |
34dc7c2f BB |
4033 | * NOTE: the page list will always be null terminated. |
4034 | */ | |
4035 | pvn_plist_init(pp, pl, plsz, off, io_len, rw); | |
d164b209 | 4036 | ASSERT(pl == NULL || (*pl)->p_offset == off); |
34dc7c2f BB |
4037 | |
4038 | return (0); | |
4039 | } | |
4040 | ||
4041 | /* | |
4042 | * Return pointers to the pages for the file region [off, off + len] | |
4043 | * in the pl array. If plsz is greater than len, this function may | |
d164b209 BB |
4044 | * also return page pointers from after the specified region |
4045 | * (i.e. the region [off, off + plsz]). These additional pages are | |
4046 | * only returned if they are already in the cache, or were created as | |
4047 | * part of a klustered read. | |
34dc7c2f BB |
4048 | * |
4049 | * IN: vp - vnode of file to get data from. | |
4050 | * off - position in file to get data from. | |
4051 | * len - amount of data to retrieve. | |
4052 | * plsz - length of provided page list. | |
4053 | * seg - segment to obtain pages for. | |
4054 | * addr - virtual address of fault. | |
4055 | * rw - mode of created pages. | |
4056 | * cr - credentials of caller. | |
4057 | * ct - caller context. | |
4058 | * | |
4059 | * OUT: protp - protection mode of created pages. | |
4060 | * pl - list of pages created. | |
4061 | * | |
4062 | * RETURN: 0 if success | |
4063 | * error code if failure | |
4064 | * | |
4065 | * Timestamps: | |
4066 | * vp - atime updated | |
4067 | */ | |
4068 | /* ARGSUSED */ | |
4069 | static int | |
4070 | zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, | |
4071 | page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, | |
9623f736 | 4072 | enum seg_rw rw, cred_t *cr) |
34dc7c2f BB |
4073 | { |
4074 | znode_t *zp = VTOZ(vp); | |
4075 | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | |
d164b209 BB |
4076 | page_t **pl0 = pl; |
4077 | int err = 0; | |
4078 | ||
4079 | /* we do our own caching, faultahead is unnecessary */ | |
4080 | if (pl == NULL) | |
4081 | return (0); | |
4082 | else if (len > plsz) | |
4083 | len = plsz; | |
4084 | else | |
4085 | len = P2ROUNDUP(len, PAGESIZE); | |
4086 | ASSERT(plsz >= len); | |
34dc7c2f BB |
4087 | |
4088 | ZFS_ENTER(zfsvfs); | |
4089 | ZFS_VERIFY_ZP(zp); | |
4090 | ||
4091 | if (protp) | |
4092 | *protp = PROT_ALL; | |
4093 | ||
34dc7c2f | 4094 | /* |
9babb374 | 4095 | * Loop through the requested range [off, off + len) looking |
34dc7c2f BB |
4096 | * for pages. If we don't find a page, we will need to create |
4097 | * a new page and fill it with data from the file. | |
4098 | */ | |
4099 | while (len > 0) { | |
d164b209 BB |
4100 | if (*pl = page_lookup(vp, off, SE_SHARED)) |
4101 | *(pl+1) = NULL; | |
4102 | else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) | |
4103 | goto out; | |
4104 | while (*pl) { | |
4105 | ASSERT3U((*pl)->p_offset, ==, off); | |
34dc7c2f BB |
4106 | off += PAGESIZE; |
4107 | addr += PAGESIZE; | |
d164b209 BB |
4108 | if (len > 0) { |
4109 | ASSERT3U(len, >=, PAGESIZE); | |
4110 | len -= PAGESIZE; | |
34dc7c2f | 4111 | } |
d164b209 BB |
4112 | ASSERT3U(plsz, >=, PAGESIZE); |
4113 | plsz -= PAGESIZE; | |
4114 | pl++; | |
34dc7c2f BB |
4115 | } |
4116 | } | |
4117 | ||
4118 | /* | |
4119 | * Fill out the page array with any pages already in the cache. | |
4120 | */ | |
d164b209 BB |
4121 | while (plsz > 0 && |
4122 | (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { | |
4123 | off += PAGESIZE; | |
4124 | plsz -= PAGESIZE; | |
34dc7c2f | 4125 | } |
34dc7c2f | 4126 | out: |
34dc7c2f BB |
4127 | if (err) { |
4128 | /* | |
4129 | * Release any pages we have previously locked. | |
4130 | */ | |
4131 | while (pl > pl0) | |
4132 | page_unlock(*--pl); | |
d164b209 BB |
4133 | } else { |
4134 | ZFS_ACCESSTIME_STAMP(zfsvfs, zp); | |
34dc7c2f BB |
4135 | } |
4136 | ||
4137 | *pl = NULL; | |
4138 | ||
34dc7c2f BB |
4139 | ZFS_EXIT(zfsvfs); |
4140 | return (err); | |
4141 | } | |
4142 | ||
4143 | /* | |
4144 | * Request a memory map for a section of a file. This code interacts | |
4145 | * with common code and the VM system as follows: | |
4146 | * | |
4147 | * common code calls mmap(), which ends up in smmap_common() | |
4148 | * | |
4149 | * this calls VOP_MAP(), which takes you into (say) zfs | |
4150 | * | |
4151 | * zfs_map() calls as_map(), passing segvn_create() as the callback | |
4152 | * | |
4153 | * segvn_create() creates the new segment and calls VOP_ADDMAP() | |
4154 | * | |
4155 | * zfs_addmap() updates z_mapcnt | |
4156 | */ | |
4157 | /*ARGSUSED*/ | |
4158 | static int | |
4159 | zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, | |
9623f736 | 4160 | size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) |
34dc7c2f BB |
4161 | { |
4162 | znode_t *zp = VTOZ(vp); | |
4163 | zfsvfs_t *zfsvfs = zp->z_zfsvfs; | |
4164 | segvn_crargs_t vn_a; | |
4165 | int error; | |
4166 | ||
4167 | ZFS_ENTER(zfsvfs); | |
4168 | ZFS_VERIFY_ZP(zp); | |
4169 | ||
428870ff BB |
4170 | if ((prot & PROT_WRITE) && (zp->z_pflags & |
4171 | (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { | |
34dc7c2f BB |
4172 | ZFS_EXIT(zfsvfs); |
4173 | return (EPERM); | |
4174 | } | |
4175 | ||
4176 | if ((prot & (PROT_READ | PROT_EXEC)) && | |
428870ff | 4177 | (zp->z_pflags & ZFS_AV_QUARANTINED)) { |
34dc7c2f BB |
4178 | ZFS_EXIT(zfsvfs); |
4179 | return (EACCES); | |
4180 | } | |
4181 | ||
4182 | if (vp->v_flag & VNOMAP) { | |
4183 | ZFS_EXIT(zfsvfs); | |
4184 | return (ENOSYS); | |
4185 | } | |
4186 | ||
4187 | if (off < 0 || len > MAXOFFSET_T - off) { | |
4188 | ZFS_EXIT(zfsvfs); | |
4189 | return (ENXIO); | |
4190 | } | |
4191 | ||
4192 | if (vp->v_type != VREG) { | |
4193 | ZFS_EXIT(zfsvfs); | |
4194 | return (ENODEV); | |
4195 | } | |
4196 | ||
4197 | /* | |
4198 | * If file is locked, disallow mapping. | |
4199 | */ | |
428870ff | 4200 | if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { |
34dc7c2f BB |
4201 | ZFS_EXIT(zfsvfs); |
4202 | return (EAGAIN); | |
4203 | } | |
4204 | ||
4205 | as_rangelock(as); | |
4206 | error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); | |
4207 | if (error != 0) { | |
4208 | as_rangeunlock(as); | |
4209 | ZFS_EXIT(zfsvfs); | |
4210 | return (error); | |
4211 | } | |
4212 | ||
4213 | vn_a.vp = vp; | |
4214 | vn_a.offset = (u_offset_t)off; | |
4215 | vn_a.type = flags & MAP_TYPE; | |
4216 | vn_a.prot = prot; | |
4217 | vn_a.maxprot = maxprot; | |
4218 | vn_a.cred = cr; | |
4219 | vn_a.amp = NULL; | |
4220 | vn_a.flags = flags & ~MAP_TYPE; | |
4221 | vn_a.szc = 0; | |
4222 | vn_a.lgrp_mem_policy_flags = 0; | |
4223 | ||
4224 | error = as_map(as, *addrp, len, segvn_create, &vn_a); | |
4225 | ||
4226 | as_rangeunlock(as); | |
4227 | ZFS_EXIT(zfsvfs); | |
4228 | return (error); | |
4229 | } | |
4230 | ||
4231 | /* ARGSUSED */ | |
4232 | static int | |
4233 | zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, | |
9623f736 | 4234 | size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) |
34dc7c2f BB |
4235 | { |
4236 | uint64_t pages = btopr(len); | |
4237 | ||
4238 | atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); | |
4239 | return (0); | |
4240 | } | |
4241 | ||
4242 | /* | |
4243 | * The reason we push dirty pages as part of zfs_delmap() is so that we get a | |
4244 | * more accurate mtime for the associated file. Since we don't have a way of | |
4245 | * detecting when the data was actually modified, we have to resort to | |
4246 | * heuristics. If an explicit msync() is done, then we mark the mtime when the | |
4247 | * last page is pushed. The problem occurs when the msync() call is omitted, | |
4248 | * which by far the most common case: | |
4249 | * | |
4250 | * open() | |
4251 | * mmap() | |
4252 | * <modify memory> | |
4253 | * munmap() | |
4254 | * close() | |
4255 | * <time lapse> | |
4256 | * putpage() via fsflush | |
4257 | * | |
4258 | * If we wait until fsflush to come along, we can have a modification time that | |
4259 | * is some arbitrary point in the future. In order to prevent this in the | |
4260 | * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is | |
4261 | * torn down. | |
4262 | */ | |
4263 | /* ARGSUSED */ | |
4264 | static int | |
4265 | zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, | |
9623f736 | 4266 | size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) |
34dc7c2f BB |
4267 | { |
4268 | uint64_t pages = btopr(len); | |
4269 | ||
4270 | ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); | |
4271 | atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); | |
4272 | ||
4273 | if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && | |
4274 | vn_has_cached_data(vp)) | |
4275 | (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); | |
4276 | ||
4277 | return (0); | |
4278 | } | |
c0d35759 | 4279 | #endif /* HAVE_MMAP */ |
34dc7c2f | 4280 | |
3558fd73 BB |
4281 | /* |
4282 | * convoff - converts the given data (start, whence) to the | |
4283 | * given whence. | |
4284 | */ | |
4285 | int | |
4286 | convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset) | |
4287 | { | |
5484965a | 4288 | vattr_t vap; |
3558fd73 BB |
4289 | int error; |
4290 | ||
4291 | if ((lckdat->l_whence == 2) || (whence == 2)) { | |
5484965a | 4292 | if ((error = zfs_getattr(ip, &vap, 0, CRED()) != 0)) |
3558fd73 BB |
4293 | return (error); |
4294 | } | |
4295 | ||
4296 | switch (lckdat->l_whence) { | |
4297 | case 1: | |
4298 | lckdat->l_start += offset; | |
4299 | break; | |
4300 | case 2: | |
5484965a | 4301 | lckdat->l_start += vap.va_size; |
3558fd73 BB |
4302 | /* FALLTHRU */ |
4303 | case 0: | |
4304 | break; | |
4305 | default: | |
4306 | return (EINVAL); | |
4307 | } | |
4308 | ||
4309 | if (lckdat->l_start < 0) | |
4310 | return (EINVAL); | |
4311 | ||
4312 | switch (whence) { | |
4313 | case 1: | |
4314 | lckdat->l_start -= offset; | |
4315 | break; | |
4316 | case 2: | |
5484965a | 4317 | lckdat->l_start -= vap.va_size; |
3558fd73 BB |
4318 | /* FALLTHRU */ |
4319 | case 0: | |
4320 | break; | |
4321 | default: | |
4322 | return (EINVAL); | |
4323 | } | |
4324 | ||
4325 | lckdat->l_whence = (short)whence; | |
4326 | return (0); | |
4327 | } | |
4328 | ||
34dc7c2f BB |
4329 | /* |
4330 | * Free or allocate space in a file. Currently, this function only | |
4331 | * supports the `F_FREESP' command. However, this command is somewhat | |
4332 | * misnamed, as its functionality includes the ability to allocate as | |
4333 | * well as free space. | |
4334 | * | |
3558fd73 | 4335 | * IN: ip - inode of file to free data in. |
34dc7c2f BB |
4336 | * cmd - action to take (only F_FREESP supported). |
4337 | * bfp - section of file to free/alloc. | |
4338 | * flag - current file open mode flags. | |
4339 | * offset - current file offset. | |
4340 | * cr - credentials of caller [UNUSED]. | |
34dc7c2f BB |
4341 | * |
4342 | * RETURN: 0 if success | |
4343 | * error code if failure | |
4344 | * | |
4345 | * Timestamps: | |
3558fd73 | 4346 | * ip - ctime|mtime updated |
34dc7c2f BB |
4347 | */ |
4348 | /* ARGSUSED */ | |
e5c39b95 | 4349 | int |
3558fd73 BB |
4350 | zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag, |
4351 | offset_t offset, cred_t *cr) | |
34dc7c2f | 4352 | { |
3558fd73 BB |
4353 | znode_t *zp = ITOZ(ip); |
4354 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
4355 | uint64_t off, len; |
4356 | int error; | |
4357 | ||
3558fd73 | 4358 | ZFS_ENTER(zsb); |
34dc7c2f BB |
4359 | ZFS_VERIFY_ZP(zp); |
4360 | ||
34dc7c2f | 4361 | if (cmd != F_FREESP) { |
3558fd73 | 4362 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4363 | return (EINVAL); |
4364 | } | |
4365 | ||
3558fd73 BB |
4366 | if ((error = convoff(ip, bfp, 0, offset))) { |
4367 | ZFS_EXIT(zsb); | |
34dc7c2f BB |
4368 | return (error); |
4369 | } | |
4370 | ||
4371 | if (bfp->l_len < 0) { | |
3558fd73 | 4372 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4373 | return (EINVAL); |
4374 | } | |
4375 | ||
4376 | off = bfp->l_start; | |
4377 | len = bfp->l_len; /* 0 means from off to end of file */ | |
4378 | ||
b128c09f | 4379 | error = zfs_freesp(zp, off, len, flag, TRUE); |
34dc7c2f | 4380 | |
3558fd73 | 4381 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4382 | return (error); |
4383 | } | |
e5c39b95 | 4384 | EXPORT_SYMBOL(zfs_space); |
34dc7c2f BB |
4385 | |
4386 | /*ARGSUSED*/ | |
e5c39b95 | 4387 | int |
3558fd73 | 4388 | zfs_fid(struct inode *ip, fid_t *fidp) |
34dc7c2f | 4389 | { |
3558fd73 BB |
4390 | znode_t *zp = ITOZ(ip); |
4391 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f | 4392 | uint32_t gen; |
428870ff | 4393 | uint64_t gen64; |
34dc7c2f BB |
4394 | uint64_t object = zp->z_id; |
4395 | zfid_short_t *zfid; | |
428870ff | 4396 | int size, i, error; |
34dc7c2f | 4397 | |
3558fd73 | 4398 | ZFS_ENTER(zsb); |
34dc7c2f | 4399 | ZFS_VERIFY_ZP(zp); |
428870ff | 4400 | |
3558fd73 | 4401 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zsb), |
428870ff | 4402 | &gen64, sizeof (uint64_t))) != 0) { |
3558fd73 | 4403 | ZFS_EXIT(zsb); |
428870ff BB |
4404 | return (error); |
4405 | } | |
4406 | ||
4407 | gen = (uint32_t)gen64; | |
34dc7c2f | 4408 | |
3558fd73 | 4409 | size = (zsb->z_parent != zsb) ? LONG_FID_LEN : SHORT_FID_LEN; |
34dc7c2f BB |
4410 | if (fidp->fid_len < size) { |
4411 | fidp->fid_len = size; | |
3558fd73 | 4412 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4413 | return (ENOSPC); |
4414 | } | |
4415 | ||
4416 | zfid = (zfid_short_t *)fidp; | |
4417 | ||
4418 | zfid->zf_len = size; | |
4419 | ||
4420 | for (i = 0; i < sizeof (zfid->zf_object); i++) | |
4421 | zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); | |
4422 | ||
4423 | /* Must have a non-zero generation number to distinguish from .zfs */ | |
4424 | if (gen == 0) | |
4425 | gen = 1; | |
4426 | for (i = 0; i < sizeof (zfid->zf_gen); i++) | |
4427 | zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); | |
4428 | ||
4429 | if (size == LONG_FID_LEN) { | |
3558fd73 | 4430 | uint64_t objsetid = dmu_objset_id(zsb->z_os); |
34dc7c2f BB |
4431 | zfid_long_t *zlfid; |
4432 | ||
4433 | zlfid = (zfid_long_t *)fidp; | |
4434 | ||
4435 | for (i = 0; i < sizeof (zlfid->zf_setid); i++) | |
4436 | zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); | |
4437 | ||
4438 | /* XXX - this should be the generation number for the objset */ | |
4439 | for (i = 0; i < sizeof (zlfid->zf_setgen); i++) | |
4440 | zlfid->zf_setgen[i] = 0; | |
4441 | } | |
4442 | ||
3558fd73 | 4443 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4444 | return (0); |
4445 | } | |
e5c39b95 | 4446 | EXPORT_SYMBOL(zfs_fid); |
34dc7c2f | 4447 | |
34dc7c2f | 4448 | /*ARGSUSED*/ |
e5c39b95 | 4449 | int |
3558fd73 | 4450 | zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr) |
34dc7c2f | 4451 | { |
3558fd73 BB |
4452 | znode_t *zp = ITOZ(ip); |
4453 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
4454 | int error; |
4455 | boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; | |
4456 | ||
3558fd73 | 4457 | ZFS_ENTER(zsb); |
34dc7c2f BB |
4458 | ZFS_VERIFY_ZP(zp); |
4459 | error = zfs_getacl(zp, vsecp, skipaclchk, cr); | |
3558fd73 | 4460 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4461 | |
4462 | return (error); | |
4463 | } | |
e5c39b95 | 4464 | EXPORT_SYMBOL(zfs_getsecattr); |
34dc7c2f BB |
4465 | |
4466 | /*ARGSUSED*/ | |
e5c39b95 | 4467 | int |
3558fd73 | 4468 | zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr) |
34dc7c2f | 4469 | { |
3558fd73 BB |
4470 | znode_t *zp = ITOZ(ip); |
4471 | zfs_sb_t *zsb = ITOZSB(ip); | |
34dc7c2f BB |
4472 | int error; |
4473 | boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; | |
3558fd73 | 4474 | zilog_t *zilog = zsb->z_log; |
34dc7c2f | 4475 | |
3558fd73 | 4476 | ZFS_ENTER(zsb); |
34dc7c2f | 4477 | ZFS_VERIFY_ZP(zp); |
428870ff | 4478 | |
34dc7c2f | 4479 | error = zfs_setacl(zp, vsecp, skipaclchk, cr); |
428870ff | 4480 | |
3558fd73 | 4481 | if (zsb->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 4482 | zil_commit(zilog, 0); |
428870ff | 4483 | |
3558fd73 | 4484 | ZFS_EXIT(zsb); |
34dc7c2f BB |
4485 | return (error); |
4486 | } | |
e5c39b95 | 4487 | EXPORT_SYMBOL(zfs_setsecattr); |
34dc7c2f | 4488 | |
3558fd73 | 4489 | #ifdef HAVE_UIO_ZEROCOPY |
428870ff BB |
4490 | /* |
4491 | * Tunable, both must be a power of 2. | |
4492 | * | |
4493 | * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf | |
4494 | * zcr_blksz_max: if set to less than the file block size, allow loaning out of | |
3558fd73 | 4495 | * an arcbuf for a partial block read |
428870ff BB |
4496 | */ |
4497 | int zcr_blksz_min = (1 << 10); /* 1K */ | |
4498 | int zcr_blksz_max = (1 << 17); /* 128K */ | |
4499 | ||
4500 | /*ARGSUSED*/ | |
4501 | static int | |
3558fd73 | 4502 | zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr) |
428870ff | 4503 | { |
3558fd73 BB |
4504 | znode_t *zp = ITOZ(ip); |
4505 | zfs_sb_t *zsb = ITOZSB(ip); | |
4506 | int max_blksz = zsb->z_max_blksz; | |
428870ff BB |
4507 | uio_t *uio = &xuio->xu_uio; |
4508 | ssize_t size = uio->uio_resid; | |
4509 | offset_t offset = uio->uio_loffset; | |
4510 | int blksz; | |
4511 | int fullblk, i; | |
4512 | arc_buf_t *abuf; | |
4513 | ssize_t maxsize; | |
4514 | int preamble, postamble; | |
4515 | ||
4516 | if (xuio->xu_type != UIOTYPE_ZEROCOPY) | |
4517 | return (EINVAL); | |
4518 | ||
3558fd73 | 4519 | ZFS_ENTER(zsb); |
428870ff BB |
4520 | ZFS_VERIFY_ZP(zp); |
4521 | switch (ioflag) { | |
4522 | case UIO_WRITE: | |
4523 | /* | |
4524 | * Loan out an arc_buf for write if write size is bigger than | |
4525 | * max_blksz, and the file's block size is also max_blksz. | |
4526 | */ | |
4527 | blksz = max_blksz; | |
4528 | if (size < blksz || zp->z_blksz != blksz) { | |
3558fd73 | 4529 | ZFS_EXIT(zsb); |
428870ff BB |
4530 | return (EINVAL); |
4531 | } | |
4532 | /* | |
4533 | * Caller requests buffers for write before knowing where the | |
4534 | * write offset might be (e.g. NFS TCP write). | |
4535 | */ | |
4536 | if (offset == -1) { | |
4537 | preamble = 0; | |
4538 | } else { | |
4539 | preamble = P2PHASE(offset, blksz); | |
4540 | if (preamble) { | |
4541 | preamble = blksz - preamble; | |
4542 | size -= preamble; | |
4543 | } | |
4544 | } | |
4545 | ||
4546 | postamble = P2PHASE(size, blksz); | |
4547 | size -= postamble; | |
4548 | ||
4549 | fullblk = size / blksz; | |
4550 | (void) dmu_xuio_init(xuio, | |
4551 | (preamble != 0) + fullblk + (postamble != 0)); | |
428870ff BB |
4552 | |
4553 | /* | |
4554 | * Have to fix iov base/len for partial buffers. They | |
4555 | * currently represent full arc_buf's. | |
4556 | */ | |
4557 | if (preamble) { | |
4558 | /* data begins in the middle of the arc_buf */ | |
4559 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
4560 | blksz); | |
4561 | ASSERT(abuf); | |
4562 | (void) dmu_xuio_add(xuio, abuf, | |
4563 | blksz - preamble, preamble); | |
4564 | } | |
4565 | ||
4566 | for (i = 0; i < fullblk; i++) { | |
4567 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
4568 | blksz); | |
4569 | ASSERT(abuf); | |
4570 | (void) dmu_xuio_add(xuio, abuf, 0, blksz); | |
4571 | } | |
4572 | ||
4573 | if (postamble) { | |
4574 | /* data ends in the middle of the arc_buf */ | |
4575 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
4576 | blksz); | |
4577 | ASSERT(abuf); | |
4578 | (void) dmu_xuio_add(xuio, abuf, 0, postamble); | |
4579 | } | |
4580 | break; | |
4581 | case UIO_READ: | |
4582 | /* | |
4583 | * Loan out an arc_buf for read if the read size is larger than | |
4584 | * the current file block size. Block alignment is not | |
4585 | * considered. Partial arc_buf will be loaned out for read. | |
4586 | */ | |
4587 | blksz = zp->z_blksz; | |
4588 | if (blksz < zcr_blksz_min) | |
4589 | blksz = zcr_blksz_min; | |
4590 | if (blksz > zcr_blksz_max) | |
4591 | blksz = zcr_blksz_max; | |
4592 | /* avoid potential complexity of dealing with it */ | |
4593 | if (blksz > max_blksz) { | |
3558fd73 | 4594 | ZFS_EXIT(zsb); |
428870ff BB |
4595 | return (EINVAL); |
4596 | } | |
4597 | ||
4598 | maxsize = zp->z_size - uio->uio_loffset; | |
4599 | if (size > maxsize) | |
4600 | size = maxsize; | |
4601 | ||
3558fd73 BB |
4602 | if (size < blksz) { |
4603 | ZFS_EXIT(zsb); | |
428870ff BB |
4604 | return (EINVAL); |
4605 | } | |
4606 | break; | |
4607 | default: | |
3558fd73 | 4608 | ZFS_EXIT(zsb); |
428870ff BB |
4609 | return (EINVAL); |
4610 | } | |
4611 | ||
4612 | uio->uio_extflg = UIO_XUIO; | |
4613 | XUIO_XUZC_RW(xuio) = ioflag; | |
3558fd73 | 4614 | ZFS_EXIT(zsb); |
428870ff BB |
4615 | return (0); |
4616 | } | |
4617 | ||
4618 | /*ARGSUSED*/ | |
4619 | static int | |
3558fd73 | 4620 | zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr) |
428870ff BB |
4621 | { |
4622 | int i; | |
4623 | arc_buf_t *abuf; | |
4624 | int ioflag = XUIO_XUZC_RW(xuio); | |
4625 | ||
4626 | ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); | |
4627 | ||
4628 | i = dmu_xuio_cnt(xuio); | |
4629 | while (i-- > 0) { | |
4630 | abuf = dmu_xuio_arcbuf(xuio, i); | |
4631 | /* | |
4632 | * if abuf == NULL, it must be a write buffer | |
4633 | * that has been returned in zfs_write(). | |
4634 | */ | |
4635 | if (abuf) | |
4636 | dmu_return_arcbuf(abuf); | |
4637 | ASSERT(abuf || ioflag == UIO_WRITE); | |
4638 | } | |
4639 | ||
4640 | dmu_xuio_fini(xuio); | |
4641 | return (0); | |
4642 | } | |
3558fd73 | 4643 | #endif /* HAVE_UIO_ZEROCOPY */ |