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