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