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