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