]>
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; | |
a1d477c2 MA |
1114 | /* |
1115 | * TX_WRITE2 relies on the data previously | |
1116 | * written by the TX_WRITE that caused | |
1117 | * EALREADY. We zero out the BP because | |
1118 | * it is the old, currently-on-disk BP, | |
1119 | * so there's no need to zio_flush() its | |
1120 | * vdevs (flushing would needlesly hurt | |
1121 | * performance, and doesn't work on | |
1122 | * indirect vdevs). | |
1123 | */ | |
1124 | zgd->zgd_bp = NULL; | |
1125 | BP_ZERO(bp); | |
428870ff BB |
1126 | error = 0; |
1127 | } | |
1128 | } | |
34dc7c2f | 1129 | } |
428870ff BB |
1130 | |
1131 | zfs_get_done(zgd, error); | |
1132 | ||
34dc7c2f BB |
1133 | return (error); |
1134 | } | |
1135 | ||
1136 | /*ARGSUSED*/ | |
3558fd73 BB |
1137 | int |
1138 | zfs_access(struct inode *ip, int mode, int flag, cred_t *cr) | |
34dc7c2f | 1139 | { |
3558fd73 | 1140 | znode_t *zp = ITOZ(ip); |
0037b49e | 1141 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
1142 | int error; |
1143 | ||
0037b49e | 1144 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
1145 | ZFS_VERIFY_ZP(zp); |
1146 | ||
1147 | if (flag & V_ACE_MASK) | |
1148 | error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); | |
1149 | else | |
1150 | error = zfs_zaccess_rwx(zp, mode, flag, cr); | |
1151 | ||
0037b49e | 1152 | ZFS_EXIT(zfsvfs); |
45d1cae3 BB |
1153 | return (error); |
1154 | } | |
45d1cae3 | 1155 | |
34dc7c2f BB |
1156 | /* |
1157 | * Lookup an entry in a directory, or an extended attribute directory. | |
3558fd73 | 1158 | * If it exists, return a held inode reference for it. |
34dc7c2f | 1159 | * |
3558fd73 | 1160 | * IN: dip - inode of directory to search. |
34dc7c2f | 1161 | * nm - name of entry to lookup. |
34dc7c2f | 1162 | * flags - LOOKUP_XATTR set if looking for an attribute. |
34dc7c2f | 1163 | * cr - credentials of caller. |
34dc7c2f BB |
1164 | * direntflags - directory lookup flags |
1165 | * realpnp - returned pathname. | |
1166 | * | |
3558fd73 | 1167 | * OUT: ipp - inode of located entry, NULL if not found. |
34dc7c2f | 1168 | * |
d3cc8b15 | 1169 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
1170 | * |
1171 | * Timestamps: | |
1172 | * NA | |
1173 | */ | |
1174 | /* ARGSUSED */ | |
e5c39b95 | 1175 | int |
3558fd73 BB |
1176 | zfs_lookup(struct inode *dip, char *nm, struct inode **ipp, int flags, |
1177 | cred_t *cr, int *direntflags, pathname_t *realpnp) | |
34dc7c2f | 1178 | { |
3558fd73 | 1179 | znode_t *zdp = ITOZ(dip); |
0037b49e | 1180 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
3558fd73 | 1181 | int error = 0; |
45d1cae3 | 1182 | |
9b7b9cd3 GM |
1183 | /* |
1184 | * Fast path lookup, however we must skip DNLC lookup | |
1185 | * for case folding or normalizing lookups because the | |
1186 | * DNLC code only stores the passed in name. This means | |
1187 | * creating 'a' and removing 'A' on a case insensitive | |
1188 | * file system would work, but DNLC still thinks 'a' | |
1189 | * exists and won't let you create it again on the next | |
1190 | * pass through fast path. | |
1191 | */ | |
45d1cae3 BB |
1192 | if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { |
1193 | ||
3558fd73 | 1194 | if (!S_ISDIR(dip->i_mode)) { |
2e528b49 | 1195 | return (SET_ERROR(ENOTDIR)); |
428870ff | 1196 | } else if (zdp->z_sa_hdl == NULL) { |
2e528b49 | 1197 | return (SET_ERROR(EIO)); |
45d1cae3 BB |
1198 | } |
1199 | ||
1200 | if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { | |
1201 | error = zfs_fastaccesschk_execute(zdp, cr); | |
1202 | if (!error) { | |
3558fd73 BB |
1203 | *ipp = dip; |
1204 | igrab(*ipp); | |
45d1cae3 BB |
1205 | return (0); |
1206 | } | |
1207 | return (error); | |
3558fd73 | 1208 | #ifdef HAVE_DNLC |
9b7b9cd3 GM |
1209 | } else if (!zdp->z_zfsvfs->z_norm && |
1210 | (zdp->z_zfsvfs->z_case == ZFS_CASE_SENSITIVE)) { | |
1211 | ||
45d1cae3 BB |
1212 | vnode_t *tvp = dnlc_lookup(dvp, nm); |
1213 | ||
1214 | if (tvp) { | |
1215 | error = zfs_fastaccesschk_execute(zdp, cr); | |
1216 | if (error) { | |
3558fd73 | 1217 | iput(tvp); |
45d1cae3 BB |
1218 | return (error); |
1219 | } | |
1220 | if (tvp == DNLC_NO_VNODE) { | |
3558fd73 | 1221 | iput(tvp); |
2e528b49 | 1222 | return (SET_ERROR(ENOENT)); |
45d1cae3 BB |
1223 | } else { |
1224 | *vpp = tvp; | |
1225 | return (specvp_check(vpp, cr)); | |
1226 | } | |
1227 | } | |
3558fd73 | 1228 | #endif /* HAVE_DNLC */ |
45d1cae3 BB |
1229 | } |
1230 | } | |
1231 | ||
0037b49e | 1232 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
1233 | ZFS_VERIFY_ZP(zdp); |
1234 | ||
3558fd73 | 1235 | *ipp = NULL; |
34dc7c2f BB |
1236 | |
1237 | if (flags & LOOKUP_XATTR) { | |
34dc7c2f BB |
1238 | /* |
1239 | * We don't allow recursive attributes.. | |
1240 | * Maybe someday we will. | |
1241 | */ | |
428870ff | 1242 | if (zdp->z_pflags & ZFS_XATTR) { |
0037b49e | 1243 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1244 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1245 | } |
1246 | ||
3558fd73 | 1247 | if ((error = zfs_get_xattrdir(zdp, ipp, cr, flags))) { |
0037b49e | 1248 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1249 | return (error); |
1250 | } | |
1251 | ||
1252 | /* | |
1253 | * Do we have permission to get into attribute directory? | |
1254 | */ | |
1255 | ||
3558fd73 | 1256 | if ((error = zfs_zaccess(ITOZ(*ipp), ACE_EXECUTE, 0, |
149e873a | 1257 | B_FALSE, cr))) { |
3558fd73 BB |
1258 | iput(*ipp); |
1259 | *ipp = NULL; | |
34dc7c2f BB |
1260 | } |
1261 | ||
0037b49e | 1262 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1263 | return (error); |
1264 | } | |
1265 | ||
3558fd73 | 1266 | if (!S_ISDIR(dip->i_mode)) { |
0037b49e | 1267 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1268 | return (SET_ERROR(ENOTDIR)); |
34dc7c2f BB |
1269 | } |
1270 | ||
1271 | /* | |
1272 | * Check accessibility of directory. | |
1273 | */ | |
1274 | ||
149e873a | 1275 | if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) { |
0037b49e | 1276 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1277 | return (error); |
1278 | } | |
1279 | ||
0037b49e | 1280 | if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), |
34dc7c2f | 1281 | NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 1282 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1283 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
1284 | } |
1285 | ||
3558fd73 BB |
1286 | error = zfs_dirlook(zdp, nm, ipp, flags, direntflags, realpnp); |
1287 | if ((error == 0) && (*ipp)) | |
1288 | zfs_inode_update(ITOZ(*ipp)); | |
34dc7c2f | 1289 | |
0037b49e | 1290 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1291 | return (error); |
1292 | } | |
1293 | ||
1294 | /* | |
1295 | * Attempt to create a new entry in a directory. If the entry | |
1296 | * already exists, truncate the file if permissible, else return | |
3558fd73 | 1297 | * an error. Return the ip of the created or trunc'd file. |
34dc7c2f | 1298 | * |
3558fd73 | 1299 | * IN: dip - inode of directory to put new file entry in. |
34dc7c2f BB |
1300 | * name - name of new file entry. |
1301 | * vap - attributes of new file. | |
1302 | * excl - flag indicating exclusive or non-exclusive mode. | |
1303 | * mode - mode to open file with. | |
1304 | * cr - credentials of caller. | |
1305 | * flag - large file flag [UNUSED]. | |
3558fd73 | 1306 | * vsecp - ACL to be set |
34dc7c2f | 1307 | * |
3558fd73 | 1308 | * OUT: ipp - inode of created or trunc'd entry. |
34dc7c2f | 1309 | * |
d3cc8b15 | 1310 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
1311 | * |
1312 | * Timestamps: | |
3558fd73 BB |
1313 | * dip - ctime|mtime updated if new entry created |
1314 | * ip - ctime|mtime always, atime if new | |
34dc7c2f BB |
1315 | */ |
1316 | ||
1317 | /* ARGSUSED */ | |
e5c39b95 | 1318 | int |
3558fd73 BB |
1319 | zfs_create(struct inode *dip, char *name, vattr_t *vap, int excl, |
1320 | int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp) | |
34dc7c2f | 1321 | { |
3558fd73 | 1322 | znode_t *zp, *dzp = ITOZ(dip); |
0037b49e | 1323 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
34dc7c2f BB |
1324 | zilog_t *zilog; |
1325 | objset_t *os; | |
1326 | zfs_dirlock_t *dl; | |
1327 | dmu_tx_t *tx; | |
1328 | int error; | |
b128c09f | 1329 | uid_t uid; |
149e873a | 1330 | gid_t gid; |
428870ff | 1331 | zfs_acl_ids_t acl_ids; |
9babb374 | 1332 | boolean_t fuid_dirtied; |
428870ff | 1333 | boolean_t have_acl = B_FALSE; |
e8b96c60 | 1334 | boolean_t waited = B_FALSE; |
34dc7c2f BB |
1335 | |
1336 | /* | |
1337 | * If we have an ephemeral id, ACL, or XVATTR then | |
1338 | * make sure file system is at proper version | |
1339 | */ | |
1340 | ||
149e873a | 1341 | gid = crgetgid(cr); |
3558fd73 | 1342 | uid = crgetuid(cr); |
b128c09f | 1343 | |
0037b49e | 1344 | if (zfsvfs->z_use_fuids == B_FALSE && |
3558fd73 | 1345 | (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) |
2e528b49 | 1346 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 1347 | |
32dec7bd | 1348 | if (name == NULL) |
1349 | return (SET_ERROR(EINVAL)); | |
1350 | ||
0037b49e | 1351 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 1352 | ZFS_VERIFY_ZP(dzp); |
0037b49e BB |
1353 | os = zfsvfs->z_os; |
1354 | zilog = zfsvfs->z_log; | |
34dc7c2f | 1355 | |
0037b49e | 1356 | if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), |
34dc7c2f | 1357 | NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 1358 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1359 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
1360 | } |
1361 | ||
5484965a | 1362 | if (vap->va_mask & ATTR_XVATTR) { |
34dc7c2f | 1363 | if ((error = secpolicy_xvattr((xvattr_t *)vap, |
3558fd73 | 1364 | crgetuid(cr), cr, vap->va_mode)) != 0) { |
0037b49e | 1365 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1366 | return (error); |
1367 | } | |
1368 | } | |
34dc7c2f | 1369 | |
3558fd73 BB |
1370 | top: |
1371 | *ipp = NULL; | |
34dc7c2f BB |
1372 | if (*name == '\0') { |
1373 | /* | |
1374 | * Null component name refers to the directory itself. | |
1375 | */ | |
3558fd73 | 1376 | igrab(dip); |
34dc7c2f BB |
1377 | zp = dzp; |
1378 | dl = NULL; | |
1379 | error = 0; | |
1380 | } else { | |
3558fd73 | 1381 | /* possible igrab(zp) */ |
34dc7c2f BB |
1382 | int zflg = 0; |
1383 | ||
1384 | if (flag & FIGNORECASE) | |
1385 | zflg |= ZCILOOK; | |
1386 | ||
1387 | error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, | |
1388 | NULL, NULL); | |
1389 | if (error) { | |
572e2857 BB |
1390 | if (have_acl) |
1391 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f | 1392 | if (strcmp(name, "..") == 0) |
2e528b49 | 1393 | error = SET_ERROR(EISDIR); |
0037b49e | 1394 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1395 | return (error); |
1396 | } | |
1397 | } | |
428870ff | 1398 | |
34dc7c2f BB |
1399 | if (zp == NULL) { |
1400 | uint64_t txtype; | |
9c5167d1 | 1401 | uint64_t projid = ZFS_DEFAULT_PROJID; |
34dc7c2f BB |
1402 | |
1403 | /* | |
1404 | * Create a new file object and update the directory | |
1405 | * to reference it. | |
1406 | */ | |
149e873a | 1407 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
572e2857 BB |
1408 | if (have_acl) |
1409 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1410 | goto out; |
1411 | } | |
1412 | ||
1413 | /* | |
1414 | * We only support the creation of regular files in | |
1415 | * extended attribute directories. | |
1416 | */ | |
428870ff | 1417 | |
3558fd73 | 1418 | if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) { |
572e2857 BB |
1419 | if (have_acl) |
1420 | zfs_acl_ids_free(&acl_ids); | |
2e528b49 | 1421 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
1422 | goto out; |
1423 | } | |
1424 | ||
428870ff BB |
1425 | if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, |
1426 | cr, vsecp, &acl_ids)) != 0) | |
9babb374 | 1427 | goto out; |
428870ff BB |
1428 | have_acl = B_TRUE; |
1429 | ||
9c5167d1 NF |
1430 | if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) |
1431 | projid = zfs_inherit_projid(dzp); | |
1432 | if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) { | |
45d1cae3 | 1433 | zfs_acl_ids_free(&acl_ids); |
2e528b49 | 1434 | error = SET_ERROR(EDQUOT); |
9babb374 BB |
1435 | goto out; |
1436 | } | |
1437 | ||
34dc7c2f | 1438 | tx = dmu_tx_create(os); |
428870ff BB |
1439 | |
1440 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + | |
1441 | ZFS_SA_BASE_ATTR_SIZE); | |
1442 | ||
0037b49e | 1443 | fuid_dirtied = zfsvfs->z_fuid_dirty; |
9babb374 | 1444 | if (fuid_dirtied) |
0037b49e | 1445 | zfs_fuid_txhold(zfsvfs, tx); |
34dc7c2f | 1446 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
428870ff | 1447 | dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); |
0037b49e | 1448 | if (!zfsvfs->z_use_sa && |
428870ff | 1449 | acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
34dc7c2f | 1450 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, |
428870ff | 1451 | 0, acl_ids.z_aclp->z_acl_bytes); |
34dc7c2f | 1452 | } |
0735ecb3 PS |
1453 | error = dmu_tx_assign(tx, |
1454 | (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); | |
34dc7c2f BB |
1455 | if (error) { |
1456 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 1457 | if (error == ERESTART) { |
e8b96c60 | 1458 | waited = B_TRUE; |
34dc7c2f BB |
1459 | dmu_tx_wait(tx); |
1460 | dmu_tx_abort(tx); | |
1461 | goto top; | |
1462 | } | |
428870ff | 1463 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 1464 | dmu_tx_abort(tx); |
0037b49e | 1465 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1466 | return (error); |
1467 | } | |
428870ff | 1468 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
9babb374 BB |
1469 | |
1470 | if (fuid_dirtied) | |
0037b49e | 1471 | zfs_fuid_sync(zfsvfs, tx); |
9babb374 | 1472 | |
4f301661 | 1473 | (void) zfs_link_create(dl, zp, tx, ZNEW); |
34dc7c2f BB |
1474 | txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); |
1475 | if (flag & FIGNORECASE) | |
1476 | txtype |= TX_CI; | |
1477 | zfs_log_create(zilog, tx, txtype, dzp, zp, name, | |
9babb374 BB |
1478 | vsecp, acl_ids.z_fuidp, vap); |
1479 | zfs_acl_ids_free(&acl_ids); | |
34dc7c2f BB |
1480 | dmu_tx_commit(tx); |
1481 | } else { | |
1482 | int aflags = (flag & FAPPEND) ? V_APPEND : 0; | |
1483 | ||
572e2857 BB |
1484 | if (have_acl) |
1485 | zfs_acl_ids_free(&acl_ids); | |
1486 | have_acl = B_FALSE; | |
1487 | ||
34dc7c2f BB |
1488 | /* |
1489 | * A directory entry already exists for this name. | |
1490 | */ | |
1491 | /* | |
1492 | * Can't truncate an existing file if in exclusive mode. | |
1493 | */ | |
3558fd73 | 1494 | if (excl) { |
2e528b49 | 1495 | error = SET_ERROR(EEXIST); |
34dc7c2f BB |
1496 | goto out; |
1497 | } | |
1498 | /* | |
1499 | * Can't open a directory for writing. | |
1500 | */ | |
3558fd73 | 1501 | if (S_ISDIR(ZTOI(zp)->i_mode)) { |
2e528b49 | 1502 | error = SET_ERROR(EISDIR); |
34dc7c2f BB |
1503 | goto out; |
1504 | } | |
1505 | /* | |
1506 | * Verify requested access to file. | |
1507 | */ | |
1508 | if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { | |
1509 | goto out; | |
1510 | } | |
1511 | ||
1512 | mutex_enter(&dzp->z_lock); | |
1513 | dzp->z_seq++; | |
1514 | mutex_exit(&dzp->z_lock); | |
1515 | ||
1516 | /* | |
1517 | * Truncate regular files if requested. | |
1518 | */ | |
3558fd73 BB |
1519 | if (S_ISREG(ZTOI(zp)->i_mode) && |
1520 | (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) { | |
b128c09f | 1521 | /* we can't hold any locks when calling zfs_freesp() */ |
609603a5 B |
1522 | if (dl) { |
1523 | zfs_dirent_unlock(dl); | |
1524 | dl = NULL; | |
1525 | } | |
34dc7c2f | 1526 | error = zfs_freesp(zp, 0, 0, mode, TRUE); |
34dc7c2f BB |
1527 | } |
1528 | } | |
1529 | out: | |
1530 | ||
1531 | if (dl) | |
1532 | zfs_dirent_unlock(dl); | |
1533 | ||
1534 | if (error) { | |
1535 | if (zp) | |
3558fd73 | 1536 | iput(ZTOI(zp)); |
34dc7c2f | 1537 | } else { |
960e08fe BB |
1538 | zfs_inode_update(dzp); |
1539 | zfs_inode_update(zp); | |
3558fd73 | 1540 | *ipp = ZTOI(zp); |
34dc7c2f | 1541 | } |
34dc7c2f | 1542 | |
0037b49e | 1543 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1544 | zil_commit(zilog, 0); |
428870ff | 1545 | |
0037b49e | 1546 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1547 | return (error); |
1548 | } | |
1549 | ||
ace1eae8 CC |
1550 | /* ARGSUSED */ |
1551 | int | |
1552 | zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl, | |
1553 | int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp) | |
1554 | { | |
1555 | znode_t *zp = NULL, *dzp = ITOZ(dip); | |
0037b49e | 1556 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
ace1eae8 CC |
1557 | objset_t *os; |
1558 | dmu_tx_t *tx; | |
1559 | int error; | |
1560 | uid_t uid; | |
1561 | gid_t gid; | |
1562 | zfs_acl_ids_t acl_ids; | |
9c5167d1 | 1563 | uint64_t projid = ZFS_DEFAULT_PROJID; |
ace1eae8 CC |
1564 | boolean_t fuid_dirtied; |
1565 | boolean_t have_acl = B_FALSE; | |
1566 | boolean_t waited = B_FALSE; | |
1567 | ||
1568 | /* | |
1569 | * If we have an ephemeral id, ACL, or XVATTR then | |
1570 | * make sure file system is at proper version | |
1571 | */ | |
1572 | ||
1573 | gid = crgetgid(cr); | |
1574 | uid = crgetuid(cr); | |
1575 | ||
0037b49e | 1576 | if (zfsvfs->z_use_fuids == B_FALSE && |
ace1eae8 CC |
1577 | (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) |
1578 | return (SET_ERROR(EINVAL)); | |
1579 | ||
0037b49e | 1580 | ZFS_ENTER(zfsvfs); |
ace1eae8 | 1581 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 1582 | os = zfsvfs->z_os; |
ace1eae8 CC |
1583 | |
1584 | if (vap->va_mask & ATTR_XVATTR) { | |
1585 | if ((error = secpolicy_xvattr((xvattr_t *)vap, | |
1586 | crgetuid(cr), cr, vap->va_mode)) != 0) { | |
0037b49e | 1587 | ZFS_EXIT(zfsvfs); |
ace1eae8 CC |
1588 | return (error); |
1589 | } | |
1590 | } | |
1591 | ||
1592 | top: | |
1593 | *ipp = NULL; | |
1594 | ||
1595 | /* | |
1596 | * Create a new file object and update the directory | |
1597 | * to reference it. | |
1598 | */ | |
1599 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { | |
1600 | if (have_acl) | |
1601 | zfs_acl_ids_free(&acl_ids); | |
1602 | goto out; | |
1603 | } | |
1604 | ||
1605 | if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, | |
1606 | cr, vsecp, &acl_ids)) != 0) | |
1607 | goto out; | |
1608 | have_acl = B_TRUE; | |
1609 | ||
9c5167d1 NF |
1610 | if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) |
1611 | projid = zfs_inherit_projid(dzp); | |
1612 | if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) { | |
ace1eae8 CC |
1613 | zfs_acl_ids_free(&acl_ids); |
1614 | error = SET_ERROR(EDQUOT); | |
1615 | goto out; | |
1616 | } | |
1617 | ||
1618 | tx = dmu_tx_create(os); | |
1619 | ||
1620 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + | |
1621 | ZFS_SA_BASE_ATTR_SIZE); | |
0037b49e | 1622 | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); |
ace1eae8 | 1623 | |
0037b49e | 1624 | fuid_dirtied = zfsvfs->z_fuid_dirty; |
ace1eae8 | 1625 | if (fuid_dirtied) |
0037b49e BB |
1626 | zfs_fuid_txhold(zfsvfs, tx); |
1627 | if (!zfsvfs->z_use_sa && | |
ace1eae8 CC |
1628 | acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
1629 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, | |
1630 | 0, acl_ids.z_aclp->z_acl_bytes); | |
1631 | } | |
0735ecb3 | 1632 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
ace1eae8 CC |
1633 | if (error) { |
1634 | if (error == ERESTART) { | |
1635 | waited = B_TRUE; | |
1636 | dmu_tx_wait(tx); | |
1637 | dmu_tx_abort(tx); | |
1638 | goto top; | |
1639 | } | |
1640 | zfs_acl_ids_free(&acl_ids); | |
1641 | dmu_tx_abort(tx); | |
0037b49e | 1642 | ZFS_EXIT(zfsvfs); |
ace1eae8 CC |
1643 | return (error); |
1644 | } | |
1645 | zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids); | |
1646 | ||
1647 | if (fuid_dirtied) | |
0037b49e | 1648 | zfs_fuid_sync(zfsvfs, tx); |
ace1eae8 CC |
1649 | |
1650 | /* Add to unlinked set */ | |
1651 | zp->z_unlinked = 1; | |
1652 | zfs_unlinked_add(zp, tx); | |
1653 | zfs_acl_ids_free(&acl_ids); | |
1654 | dmu_tx_commit(tx); | |
1655 | out: | |
1656 | ||
1657 | if (error) { | |
1658 | if (zp) | |
1659 | iput(ZTOI(zp)); | |
1660 | } else { | |
1661 | zfs_inode_update(dzp); | |
1662 | zfs_inode_update(zp); | |
1663 | *ipp = ZTOI(zp); | |
1664 | } | |
1665 | ||
0037b49e | 1666 | ZFS_EXIT(zfsvfs); |
ace1eae8 CC |
1667 | return (error); |
1668 | } | |
1669 | ||
34dc7c2f BB |
1670 | /* |
1671 | * Remove an entry from a directory. | |
1672 | * | |
3558fd73 | 1673 | * IN: dip - inode of directory to remove entry from. |
34dc7c2f BB |
1674 | * name - name of entry to remove. |
1675 | * cr - credentials of caller. | |
34dc7c2f BB |
1676 | * |
1677 | * RETURN: 0 if success | |
1678 | * error code if failure | |
1679 | * | |
1680 | * Timestamps: | |
3558fd73 BB |
1681 | * dip - ctime|mtime |
1682 | * ip - ctime (if nlink > 0) | |
34dc7c2f | 1683 | */ |
428870ff BB |
1684 | |
1685 | uint64_t null_xattr = 0; | |
1686 | ||
34dc7c2f | 1687 | /*ARGSUSED*/ |
e5c39b95 | 1688 | int |
da5e151f | 1689 | zfs_remove(struct inode *dip, char *name, cred_t *cr, int flags) |
34dc7c2f | 1690 | { |
3558fd73 | 1691 | znode_t *zp, *dzp = ITOZ(dip); |
572e2857 | 1692 | znode_t *xzp; |
3558fd73 | 1693 | struct inode *ip; |
0037b49e | 1694 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
34dc7c2f | 1695 | zilog_t *zilog; |
a966c564 | 1696 | uint64_t acl_obj, xattr_obj; |
3558fd73 | 1697 | uint64_t xattr_obj_unlinked = 0; |
572e2857 | 1698 | uint64_t obj = 0; |
dfbc8630 | 1699 | uint64_t links; |
34dc7c2f BB |
1700 | zfs_dirlock_t *dl; |
1701 | dmu_tx_t *tx; | |
a966c564 K |
1702 | boolean_t may_delete_now, delete_now = FALSE; |
1703 | boolean_t unlinked, toobig = FALSE; | |
34dc7c2f BB |
1704 | uint64_t txtype; |
1705 | pathname_t *realnmp = NULL; | |
1706 | pathname_t realnm; | |
1707 | int error; | |
1708 | int zflg = ZEXISTS; | |
e8b96c60 | 1709 | boolean_t waited = B_FALSE; |
34dc7c2f | 1710 | |
32dec7bd | 1711 | if (name == NULL) |
1712 | return (SET_ERROR(EINVAL)); | |
1713 | ||
0037b49e | 1714 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 1715 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 1716 | zilog = zfsvfs->z_log; |
34dc7c2f BB |
1717 | |
1718 | if (flags & FIGNORECASE) { | |
1719 | zflg |= ZCILOOK; | |
1720 | pn_alloc(&realnm); | |
1721 | realnmp = &realnm; | |
1722 | } | |
1723 | ||
1724 | top: | |
572e2857 BB |
1725 | xattr_obj = 0; |
1726 | xzp = NULL; | |
34dc7c2f BB |
1727 | /* |
1728 | * Attempt to lock directory; fail if entry doesn't exist. | |
1729 | */ | |
149e873a BB |
1730 | if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, |
1731 | NULL, realnmp))) { | |
34dc7c2f BB |
1732 | if (realnmp) |
1733 | pn_free(realnmp); | |
0037b49e | 1734 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1735 | return (error); |
1736 | } | |
1737 | ||
3558fd73 | 1738 | ip = ZTOI(zp); |
34dc7c2f | 1739 | |
149e873a | 1740 | if ((error = zfs_zaccess_delete(dzp, zp, cr))) { |
34dc7c2f BB |
1741 | goto out; |
1742 | } | |
1743 | ||
1744 | /* | |
1745 | * Need to use rmdir for removing directories. | |
1746 | */ | |
3558fd73 | 1747 | if (S_ISDIR(ip->i_mode)) { |
2e528b49 | 1748 | error = SET_ERROR(EPERM); |
34dc7c2f BB |
1749 | goto out; |
1750 | } | |
1751 | ||
3558fd73 | 1752 | #ifdef HAVE_DNLC |
34dc7c2f BB |
1753 | if (realnmp) |
1754 | dnlc_remove(dvp, realnmp->pn_buf); | |
1755 | else | |
1756 | dnlc_remove(dvp, name); | |
3558fd73 | 1757 | #endif /* HAVE_DNLC */ |
34dc7c2f | 1758 | |
19d55079 MA |
1759 | mutex_enter(&zp->z_lock); |
1760 | may_delete_now = atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped); | |
1761 | mutex_exit(&zp->z_lock); | |
1762 | ||
34dc7c2f | 1763 | /* |
a966c564 K |
1764 | * We may delete the znode now, or we may put it in the unlinked set; |
1765 | * it depends on whether we're the last link, and on whether there are | |
1766 | * other holds on the inode. So we dmu_tx_hold() the right things to | |
1767 | * allow for either case. | |
34dc7c2f | 1768 | */ |
572e2857 | 1769 | obj = zp->z_id; |
0037b49e | 1770 | tx = dmu_tx_create(zfsvfs->z_os); |
34dc7c2f | 1771 | dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); |
428870ff BB |
1772 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
1773 | zfs_sa_upgrade_txholds(tx, zp); | |
1774 | zfs_sa_upgrade_txholds(tx, dzp); | |
a966c564 K |
1775 | if (may_delete_now) { |
1776 | toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks; | |
1777 | /* if the file is too big, only hold_free a token amount */ | |
1778 | dmu_tx_hold_free(tx, zp->z_id, 0, | |
1779 | (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); | |
1780 | } | |
34dc7c2f BB |
1781 | |
1782 | /* are there any extended attributes? */ | |
0037b49e | 1783 | error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), |
428870ff | 1784 | &xattr_obj, sizeof (xattr_obj)); |
572e2857 | 1785 | if (error == 0 && xattr_obj) { |
0037b49e | 1786 | error = zfs_zget(zfsvfs, xattr_obj, &xzp); |
c99c9001 | 1787 | ASSERT0(error); |
428870ff BB |
1788 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); |
1789 | dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
1790 | } |
1791 | ||
a966c564 K |
1792 | mutex_enter(&zp->z_lock); |
1793 | if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) | |
1794 | dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); | |
1795 | mutex_exit(&zp->z_lock); | |
1796 | ||
34dc7c2f | 1797 | /* charge as an update -- would be nice not to charge at all */ |
0037b49e | 1798 | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); |
34dc7c2f | 1799 | |
19d55079 | 1800 | /* |
1a04bab3 | 1801 | * Mark this transaction as typically resulting in a net free of space |
19d55079 | 1802 | */ |
1a04bab3 | 1803 | dmu_tx_mark_netfree(tx); |
19d55079 | 1804 | |
0735ecb3 | 1805 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
1806 | if (error) { |
1807 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 1808 | if (error == ERESTART) { |
e8b96c60 | 1809 | waited = B_TRUE; |
34dc7c2f BB |
1810 | dmu_tx_wait(tx); |
1811 | dmu_tx_abort(tx); | |
ea7e86d8 BB |
1812 | iput(ip); |
1813 | if (xzp) | |
1814 | iput(ZTOI(xzp)); | |
34dc7c2f BB |
1815 | goto top; |
1816 | } | |
1817 | if (realnmp) | |
1818 | pn_free(realnmp); | |
1819 | dmu_tx_abort(tx); | |
ea7e86d8 BB |
1820 | iput(ip); |
1821 | if (xzp) | |
1822 | iput(ZTOI(xzp)); | |
0037b49e | 1823 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1824 | return (error); |
1825 | } | |
1826 | ||
1827 | /* | |
1828 | * Remove the directory entry. | |
1829 | */ | |
1830 | error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); | |
1831 | ||
1832 | if (error) { | |
1833 | dmu_tx_commit(tx); | |
1834 | goto out; | |
1835 | } | |
1836 | ||
1837 | if (unlinked) { | |
572e2857 BB |
1838 | /* |
1839 | * Hold z_lock so that we can make sure that the ACL obj | |
1840 | * hasn't changed. Could have been deleted due to | |
1841 | * zfs_sa_upgrade(). | |
1842 | */ | |
1843 | mutex_enter(&zp->z_lock); | |
0037b49e | 1844 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), |
428870ff | 1845 | &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); |
a966c564 K |
1846 | delete_now = may_delete_now && !toobig && |
1847 | atomic_read(&ip->i_count) == 1 && !(zp->z_is_mapped) && | |
1848 | xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == | |
1849 | acl_obj; | |
1850 | } | |
1851 | ||
1852 | if (delete_now) { | |
1853 | if (xattr_obj_unlinked) { | |
dfbc8630 | 1854 | ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2); |
a966c564 K |
1855 | mutex_enter(&xzp->z_lock); |
1856 | xzp->z_unlinked = 1; | |
dfbc8630 CD |
1857 | clear_nlink(ZTOI(xzp)); |
1858 | links = 0; | |
0037b49e | 1859 | error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), |
dfbc8630 | 1860 | &links, sizeof (links), tx); |
a966c564 K |
1861 | ASSERT3U(error, ==, 0); |
1862 | mutex_exit(&xzp->z_lock); | |
1863 | zfs_unlinked_add(xzp, tx); | |
1864 | ||
1865 | if (zp->z_is_sa) | |
1866 | error = sa_remove(zp->z_sa_hdl, | |
0037b49e | 1867 | SA_ZPL_XATTR(zfsvfs), tx); |
a966c564 K |
1868 | else |
1869 | error = sa_update(zp->z_sa_hdl, | |
0037b49e | 1870 | SA_ZPL_XATTR(zfsvfs), &null_xattr, |
a966c564 K |
1871 | sizeof (uint64_t), tx); |
1872 | ASSERT0(error); | |
1873 | } | |
1874 | /* | |
1875 | * Add to the unlinked set because a new reference could be | |
1876 | * taken concurrently resulting in a deferred destruction. | |
1877 | */ | |
1878 | zfs_unlinked_add(zp, tx); | |
1879 | mutex_exit(&zp->z_lock); | |
a966c564 | 1880 | } else if (unlinked) { |
572e2857 | 1881 | mutex_exit(&zp->z_lock); |
34dc7c2f BB |
1882 | zfs_unlinked_add(zp, tx); |
1883 | } | |
1884 | ||
1885 | txtype = TX_REMOVE; | |
1886 | if (flags & FIGNORECASE) | |
1887 | txtype |= TX_CI; | |
572e2857 | 1888 | zfs_log_remove(zilog, tx, txtype, dzp, name, obj); |
34dc7c2f BB |
1889 | |
1890 | dmu_tx_commit(tx); | |
1891 | out: | |
1892 | if (realnmp) | |
1893 | pn_free(realnmp); | |
1894 | ||
1895 | zfs_dirent_unlock(dl); | |
960e08fe | 1896 | zfs_inode_update(dzp); |
ea7e86d8 | 1897 | zfs_inode_update(zp); |
34dc7c2f | 1898 | |
ea7e86d8 BB |
1899 | if (delete_now) |
1900 | iput(ip); | |
1901 | else | |
a966c564 | 1902 | zfs_iput_async(ip); |
a966c564 K |
1903 | |
1904 | if (xzp) { | |
1905 | zfs_inode_update(xzp); | |
1906 | zfs_iput_async(ZTOI(xzp)); | |
1907 | } | |
428870ff | 1908 | |
0037b49e | 1909 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 1910 | zil_commit(zilog, 0); |
34dc7c2f | 1911 | |
0037b49e | 1912 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1913 | return (error); |
1914 | } | |
1915 | ||
1916 | /* | |
3558fd73 | 1917 | * Create a new directory and insert it into dip using the name |
34dc7c2f BB |
1918 | * provided. Return a pointer to the inserted directory. |
1919 | * | |
3558fd73 | 1920 | * IN: dip - inode of directory to add subdir to. |
34dc7c2f BB |
1921 | * dirname - name of new directory. |
1922 | * vap - attributes of new directory. | |
1923 | * cr - credentials of caller. | |
34dc7c2f BB |
1924 | * vsecp - ACL to be set |
1925 | * | |
3558fd73 | 1926 | * OUT: ipp - inode of created directory. |
34dc7c2f BB |
1927 | * |
1928 | * RETURN: 0 if success | |
1929 | * error code if failure | |
1930 | * | |
1931 | * Timestamps: | |
3558fd73 BB |
1932 | * dip - ctime|mtime updated |
1933 | * ipp - ctime|mtime|atime updated | |
34dc7c2f BB |
1934 | */ |
1935 | /*ARGSUSED*/ | |
e5c39b95 | 1936 | int |
3558fd73 BB |
1937 | zfs_mkdir(struct inode *dip, char *dirname, vattr_t *vap, struct inode **ipp, |
1938 | cred_t *cr, int flags, vsecattr_t *vsecp) | |
34dc7c2f | 1939 | { |
3558fd73 | 1940 | znode_t *zp, *dzp = ITOZ(dip); |
0037b49e | 1941 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
34dc7c2f BB |
1942 | zilog_t *zilog; |
1943 | zfs_dirlock_t *dl; | |
1944 | uint64_t txtype; | |
1945 | dmu_tx_t *tx; | |
1946 | int error; | |
34dc7c2f | 1947 | int zf = ZNEW; |
b128c09f BB |
1948 | uid_t uid; |
1949 | gid_t gid = crgetgid(cr); | |
428870ff | 1950 | zfs_acl_ids_t acl_ids; |
9babb374 | 1951 | boolean_t fuid_dirtied; |
e8b96c60 | 1952 | boolean_t waited = B_FALSE; |
34dc7c2f | 1953 | |
3558fd73 | 1954 | ASSERT(S_ISDIR(vap->va_mode)); |
34dc7c2f BB |
1955 | |
1956 | /* | |
1957 | * If we have an ephemeral id, ACL, or XVATTR then | |
1958 | * make sure file system is at proper version | |
1959 | */ | |
1960 | ||
3558fd73 | 1961 | uid = crgetuid(cr); |
0037b49e | 1962 | if (zfsvfs->z_use_fuids == B_FALSE && |
3558fd73 | 1963 | (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) |
2e528b49 | 1964 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 1965 | |
32dec7bd | 1966 | if (dirname == NULL) |
1967 | return (SET_ERROR(EINVAL)); | |
1968 | ||
0037b49e | 1969 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 1970 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 1971 | zilog = zfsvfs->z_log; |
34dc7c2f | 1972 | |
428870ff | 1973 | if (dzp->z_pflags & ZFS_XATTR) { |
0037b49e | 1974 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1975 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1976 | } |
1977 | ||
0037b49e | 1978 | if (zfsvfs->z_utf8 && u8_validate(dirname, |
34dc7c2f | 1979 | strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 1980 | ZFS_EXIT(zfsvfs); |
2e528b49 | 1981 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
1982 | } |
1983 | if (flags & FIGNORECASE) | |
1984 | zf |= ZCILOOK; | |
1985 | ||
5484965a | 1986 | if (vap->va_mask & ATTR_XVATTR) { |
34dc7c2f | 1987 | if ((error = secpolicy_xvattr((xvattr_t *)vap, |
3558fd73 | 1988 | crgetuid(cr), cr, vap->va_mode)) != 0) { |
0037b49e | 1989 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
1990 | return (error); |
1991 | } | |
428870ff | 1992 | } |
34dc7c2f | 1993 | |
428870ff BB |
1994 | if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, |
1995 | vsecp, &acl_ids)) != 0) { | |
0037b49e | 1996 | ZFS_EXIT(zfsvfs); |
428870ff BB |
1997 | return (error); |
1998 | } | |
34dc7c2f BB |
1999 | /* |
2000 | * First make sure the new directory doesn't exist. | |
428870ff BB |
2001 | * |
2002 | * Existence is checked first to make sure we don't return | |
2003 | * EACCES instead of EEXIST which can cause some applications | |
2004 | * to fail. | |
34dc7c2f BB |
2005 | */ |
2006 | top: | |
3558fd73 | 2007 | *ipp = NULL; |
34dc7c2f | 2008 | |
149e873a BB |
2009 | if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, |
2010 | NULL, NULL))) { | |
428870ff | 2011 | zfs_acl_ids_free(&acl_ids); |
0037b49e | 2012 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2013 | return (error); |
2014 | } | |
2015 | ||
149e873a | 2016 | if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) { |
428870ff | 2017 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 2018 | zfs_dirent_unlock(dl); |
0037b49e | 2019 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2020 | return (error); |
2021 | } | |
2022 | ||
9c5167d1 | 2023 | if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) { |
45d1cae3 | 2024 | zfs_acl_ids_free(&acl_ids); |
9babb374 | 2025 | zfs_dirent_unlock(dl); |
0037b49e | 2026 | ZFS_EXIT(zfsvfs); |
2e528b49 | 2027 | return (SET_ERROR(EDQUOT)); |
9babb374 BB |
2028 | } |
2029 | ||
34dc7c2f BB |
2030 | /* |
2031 | * Add a new entry to the directory. | |
2032 | */ | |
0037b49e | 2033 | tx = dmu_tx_create(zfsvfs->z_os); |
34dc7c2f BB |
2034 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); |
2035 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); | |
0037b49e | 2036 | fuid_dirtied = zfsvfs->z_fuid_dirty; |
9babb374 | 2037 | if (fuid_dirtied) |
0037b49e BB |
2038 | zfs_fuid_txhold(zfsvfs, tx); |
2039 | if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { | |
428870ff BB |
2040 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, |
2041 | acl_ids.z_aclp->z_acl_bytes); | |
2042 | } | |
2043 | ||
2044 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + | |
2045 | ZFS_SA_BASE_ATTR_SIZE); | |
2046 | ||
0735ecb3 | 2047 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
2048 | if (error) { |
2049 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 2050 | if (error == ERESTART) { |
e8b96c60 | 2051 | waited = B_TRUE; |
34dc7c2f BB |
2052 | dmu_tx_wait(tx); |
2053 | dmu_tx_abort(tx); | |
2054 | goto top; | |
2055 | } | |
428870ff | 2056 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 2057 | dmu_tx_abort(tx); |
0037b49e | 2058 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2059 | return (error); |
2060 | } | |
2061 | ||
2062 | /* | |
2063 | * Create new node. | |
2064 | */ | |
428870ff | 2065 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
34dc7c2f | 2066 | |
4f301661 TH |
2067 | if (fuid_dirtied) |
2068 | zfs_fuid_sync(zfsvfs, tx); | |
2069 | ||
34dc7c2f BB |
2070 | /* |
2071 | * Now put new name in parent dir. | |
2072 | */ | |
4f301661 | 2073 | (void) zfs_link_create(dl, zp, tx, ZNEW); |
34dc7c2f | 2074 | |
3558fd73 | 2075 | *ipp = ZTOI(zp); |
34dc7c2f BB |
2076 | |
2077 | txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); | |
2078 | if (flags & FIGNORECASE) | |
2079 | txtype |= TX_CI; | |
9babb374 BB |
2080 | zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, |
2081 | acl_ids.z_fuidp, vap); | |
34dc7c2f | 2082 | |
9babb374 | 2083 | zfs_acl_ids_free(&acl_ids); |
428870ff | 2084 | |
34dc7c2f BB |
2085 | dmu_tx_commit(tx); |
2086 | ||
2087 | zfs_dirent_unlock(dl); | |
2088 | ||
0037b49e | 2089 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 2090 | zil_commit(zilog, 0); |
428870ff | 2091 | |
4f301661 TH |
2092 | zfs_inode_update(dzp); |
2093 | zfs_inode_update(zp); | |
0037b49e | 2094 | ZFS_EXIT(zfsvfs); |
4f301661 | 2095 | return (0); |
34dc7c2f BB |
2096 | } |
2097 | ||
2098 | /* | |
2099 | * Remove a directory subdir entry. If the current working | |
2100 | * directory is the same as the subdir to be removed, the | |
2101 | * remove will fail. | |
2102 | * | |
3558fd73 | 2103 | * IN: dip - inode of directory to remove from. |
34dc7c2f | 2104 | * name - name of directory to be removed. |
3558fd73 | 2105 | * cwd - inode of current working directory. |
34dc7c2f | 2106 | * cr - credentials of caller. |
34dc7c2f BB |
2107 | * flags - case flags |
2108 | * | |
d3cc8b15 | 2109 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
2110 | * |
2111 | * Timestamps: | |
3558fd73 | 2112 | * dip - ctime|mtime updated |
34dc7c2f BB |
2113 | */ |
2114 | /*ARGSUSED*/ | |
e5c39b95 | 2115 | int |
3558fd73 BB |
2116 | zfs_rmdir(struct inode *dip, char *name, struct inode *cwd, cred_t *cr, |
2117 | int flags) | |
34dc7c2f | 2118 | { |
3558fd73 | 2119 | znode_t *dzp = ITOZ(dip); |
34dc7c2f | 2120 | znode_t *zp; |
3558fd73 | 2121 | struct inode *ip; |
0037b49e | 2122 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
34dc7c2f BB |
2123 | zilog_t *zilog; |
2124 | zfs_dirlock_t *dl; | |
2125 | dmu_tx_t *tx; | |
2126 | int error; | |
2127 | int zflg = ZEXISTS; | |
e8b96c60 | 2128 | boolean_t waited = B_FALSE; |
34dc7c2f | 2129 | |
32dec7bd | 2130 | if (name == NULL) |
2131 | return (SET_ERROR(EINVAL)); | |
2132 | ||
0037b49e | 2133 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 2134 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 2135 | zilog = zfsvfs->z_log; |
34dc7c2f BB |
2136 | |
2137 | if (flags & FIGNORECASE) | |
2138 | zflg |= ZCILOOK; | |
2139 | top: | |
2140 | zp = NULL; | |
2141 | ||
2142 | /* | |
2143 | * Attempt to lock directory; fail if entry doesn't exist. | |
2144 | */ | |
149e873a BB |
2145 | if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, |
2146 | NULL, NULL))) { | |
0037b49e | 2147 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2148 | return (error); |
2149 | } | |
2150 | ||
3558fd73 | 2151 | ip = ZTOI(zp); |
34dc7c2f | 2152 | |
149e873a | 2153 | if ((error = zfs_zaccess_delete(dzp, zp, cr))) { |
34dc7c2f BB |
2154 | goto out; |
2155 | } | |
2156 | ||
3558fd73 | 2157 | if (!S_ISDIR(ip->i_mode)) { |
2e528b49 | 2158 | error = SET_ERROR(ENOTDIR); |
34dc7c2f BB |
2159 | goto out; |
2160 | } | |
2161 | ||
3558fd73 | 2162 | if (ip == cwd) { |
2e528b49 | 2163 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
2164 | goto out; |
2165 | } | |
2166 | ||
34dc7c2f | 2167 | /* |
4e33ba4c | 2168 | * Grab a lock on the directory to make sure that no one is |
34dc7c2f BB |
2169 | * trying to add (or lookup) entries while we are removing it. |
2170 | */ | |
2171 | rw_enter(&zp->z_name_lock, RW_WRITER); | |
2172 | ||
2173 | /* | |
2174 | * Grab a lock on the parent pointer to make sure we play well | |
2175 | * with the treewalk and directory rename code. | |
2176 | */ | |
2177 | rw_enter(&zp->z_parent_lock, RW_WRITER); | |
2178 | ||
0037b49e | 2179 | tx = dmu_tx_create(zfsvfs->z_os); |
34dc7c2f | 2180 | dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); |
428870ff | 2181 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
0037b49e | 2182 | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); |
428870ff BB |
2183 | zfs_sa_upgrade_txholds(tx, zp); |
2184 | zfs_sa_upgrade_txholds(tx, dzp); | |
db707ad0 | 2185 | dmu_tx_mark_netfree(tx); |
0735ecb3 | 2186 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
2187 | if (error) { |
2188 | rw_exit(&zp->z_parent_lock); | |
2189 | rw_exit(&zp->z_name_lock); | |
2190 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 2191 | if (error == ERESTART) { |
e8b96c60 | 2192 | waited = B_TRUE; |
34dc7c2f BB |
2193 | dmu_tx_wait(tx); |
2194 | dmu_tx_abort(tx); | |
ea7e86d8 | 2195 | iput(ip); |
34dc7c2f BB |
2196 | goto top; |
2197 | } | |
2198 | dmu_tx_abort(tx); | |
ea7e86d8 | 2199 | iput(ip); |
0037b49e | 2200 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2201 | return (error); |
2202 | } | |
2203 | ||
2204 | error = zfs_link_destroy(dl, zp, tx, zflg, NULL); | |
2205 | ||
2206 | if (error == 0) { | |
2207 | uint64_t txtype = TX_RMDIR; | |
2208 | if (flags & FIGNORECASE) | |
2209 | txtype |= TX_CI; | |
572e2857 | 2210 | zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); |
34dc7c2f BB |
2211 | } |
2212 | ||
2213 | dmu_tx_commit(tx); | |
2214 | ||
2215 | rw_exit(&zp->z_parent_lock); | |
2216 | rw_exit(&zp->z_name_lock); | |
2217 | out: | |
2218 | zfs_dirent_unlock(dl); | |
2219 | ||
59157910 BB |
2220 | zfs_inode_update(dzp); |
2221 | zfs_inode_update(zp); | |
3558fd73 | 2222 | iput(ip); |
34dc7c2f | 2223 | |
0037b49e | 2224 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 2225 | zil_commit(zilog, 0); |
428870ff | 2226 | |
0037b49e | 2227 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2228 | return (error); |
2229 | } | |
2230 | ||
2231 | /* | |
2232 | * Read as many directory entries as will fit into the provided | |
3558fd73 | 2233 | * dirent buffer from the given directory cursor position. |
34dc7c2f | 2234 | * |
3558fd73 BB |
2235 | * IN: ip - inode of directory to read. |
2236 | * dirent - buffer for directory entries. | |
34dc7c2f | 2237 | * |
3558fd73 | 2238 | * OUT: dirent - filler buffer of directory entries. |
34dc7c2f BB |
2239 | * |
2240 | * RETURN: 0 if success | |
2241 | * error code if failure | |
2242 | * | |
2243 | * Timestamps: | |
3558fd73 | 2244 | * ip - atime updated |
34dc7c2f BB |
2245 | * |
2246 | * Note that the low 4 bits of the cookie returned by zap is always zero. | |
2247 | * This allows us to use the low range for "special" directory entries: | |
2248 | * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, | |
2249 | * we use the offset 2 for the '.zfs' directory. | |
2250 | */ | |
2251 | /* ARGSUSED */ | |
3558fd73 | 2252 | int |
0f37d0c8 | 2253 | zfs_readdir(struct inode *ip, struct dir_context *ctx, cred_t *cr) |
34dc7c2f | 2254 | { |
3558fd73 | 2255 | znode_t *zp = ITOZ(ip); |
0037b49e | 2256 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f | 2257 | objset_t *os; |
34dc7c2f BB |
2258 | zap_cursor_t zc; |
2259 | zap_attribute_t zap; | |
34dc7c2f BB |
2260 | int error; |
2261 | uint8_t prefetch; | |
c12e3a59 | 2262 | uint8_t type; |
3558fd73 BB |
2263 | int done = 0; |
2264 | uint64_t parent; | |
c12e3a59 | 2265 | uint64_t offset; /* must be unsigned; checks for < 1 */ |
34dc7c2f | 2266 | |
0037b49e | 2267 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
2268 | ZFS_VERIFY_ZP(zp); |
2269 | ||
0037b49e | 2270 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), |
3558fd73 BB |
2271 | &parent, sizeof (parent))) != 0) |
2272 | goto out; | |
34dc7c2f BB |
2273 | |
2274 | /* | |
2275 | * Quit if directory has been removed (posix) | |
2276 | */ | |
3558fd73 BB |
2277 | if (zp->z_unlinked) |
2278 | goto out; | |
2279 | ||
c12e3a59 | 2280 | error = 0; |
0037b49e | 2281 | os = zfsvfs->z_os; |
c12e3a59 | 2282 | offset = ctx->pos; |
34dc7c2f BB |
2283 | prefetch = zp->z_zn_prefetch; |
2284 | ||
2285 | /* | |
2286 | * Initialize the iterator cursor. | |
2287 | */ | |
c12e3a59 | 2288 | if (offset <= 3) { |
34dc7c2f BB |
2289 | /* |
2290 | * Start iteration from the beginning of the directory. | |
2291 | */ | |
2292 | zap_cursor_init(&zc, os, zp->z_id); | |
2293 | } else { | |
2294 | /* | |
2295 | * The offset is a serialized cursor. | |
2296 | */ | |
c12e3a59 | 2297 | zap_cursor_init_serialized(&zc, os, zp->z_id, offset); |
34dc7c2f BB |
2298 | } |
2299 | ||
34dc7c2f BB |
2300 | /* |
2301 | * Transform to file-system independent format | |
2302 | */ | |
3558fd73 BB |
2303 | while (!done) { |
2304 | uint64_t objnum; | |
34dc7c2f BB |
2305 | /* |
2306 | * Special case `.', `..', and `.zfs'. | |
2307 | */ | |
c12e3a59 | 2308 | if (offset == 0) { |
34dc7c2f BB |
2309 | (void) strcpy(zap.za_name, "."); |
2310 | zap.za_normalization_conflict = 0; | |
2311 | objnum = zp->z_id; | |
c12e3a59 RY |
2312 | type = DT_DIR; |
2313 | } else if (offset == 1) { | |
34dc7c2f BB |
2314 | (void) strcpy(zap.za_name, ".."); |
2315 | zap.za_normalization_conflict = 0; | |
428870ff | 2316 | objnum = parent; |
c12e3a59 RY |
2317 | type = DT_DIR; |
2318 | } else if (offset == 2 && zfs_show_ctldir(zp)) { | |
34dc7c2f BB |
2319 | (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); |
2320 | zap.za_normalization_conflict = 0; | |
2321 | objnum = ZFSCTL_INO_ROOT; | |
c12e3a59 | 2322 | type = DT_DIR; |
34dc7c2f BB |
2323 | } else { |
2324 | /* | |
2325 | * Grab next entry. | |
2326 | */ | |
3558fd73 BB |
2327 | if ((error = zap_cursor_retrieve(&zc, &zap))) { |
2328 | if (error == ENOENT) | |
34dc7c2f BB |
2329 | break; |
2330 | else | |
2331 | goto update; | |
2332 | } | |
2333 | ||
0c5dde49 BB |
2334 | /* |
2335 | * Allow multiple entries provided the first entry is | |
2336 | * the object id. Non-zpl consumers may safely make | |
2337 | * use of the additional space. | |
2338 | * | |
2339 | * XXX: This should be a feature flag for compatibility | |
2340 | */ | |
34dc7c2f | 2341 | if (zap.za_integer_length != 8 || |
0c5dde49 | 2342 | zap.za_num_integers == 0) { |
34dc7c2f | 2343 | cmn_err(CE_WARN, "zap_readdir: bad directory " |
0c5dde49 BB |
2344 | "entry, obj = %lld, offset = %lld, " |
2345 | "length = %d, num = %lld\n", | |
34dc7c2f | 2346 | (u_longlong_t)zp->z_id, |
c12e3a59 | 2347 | (u_longlong_t)offset, |
0c5dde49 BB |
2348 | zap.za_integer_length, |
2349 | (u_longlong_t)zap.za_num_integers); | |
2e528b49 | 2350 | error = SET_ERROR(ENXIO); |
34dc7c2f BB |
2351 | goto update; |
2352 | } | |
2353 | ||
2354 | objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); | |
c12e3a59 | 2355 | type = ZFS_DIRENT_TYPE(zap.za_first_integer); |
34dc7c2f | 2356 | } |
0f37d0c8 RY |
2357 | |
2358 | done = !dir_emit(ctx, zap.za_name, strlen(zap.za_name), | |
c12e3a59 | 2359 | objnum, type); |
0f37d0c8 | 2360 | if (done) |
34dc7c2f | 2361 | break; |
34dc7c2f BB |
2362 | |
2363 | /* Prefetch znode */ | |
3558fd73 | 2364 | if (prefetch) { |
fcff0f35 PD |
2365 | dmu_prefetch(os, objnum, 0, 0, 0, |
2366 | ZIO_PRIORITY_SYNC_READ); | |
3558fd73 | 2367 | } |
34dc7c2f | 2368 | |
c12e3a59 RY |
2369 | /* |
2370 | * Move to the next entry, fill in the previous offset. | |
2371 | */ | |
2372 | if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { | |
34dc7c2f | 2373 | zap_cursor_advance(&zc); |
c12e3a59 | 2374 | offset = zap_cursor_serialize(&zc); |
34dc7c2f | 2375 | } else { |
c12e3a59 | 2376 | offset += 1; |
34dc7c2f | 2377 | } |
c12e3a59 | 2378 | ctx->pos = offset; |
34dc7c2f BB |
2379 | } |
2380 | zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ | |
2381 | ||
34dc7c2f BB |
2382 | update: |
2383 | zap_cursor_fini(&zc); | |
34dc7c2f BB |
2384 | if (error == ENOENT) |
2385 | error = 0; | |
3558fd73 | 2386 | out: |
0037b49e | 2387 | ZFS_EXIT(zfsvfs); |
34dc7c2f | 2388 | |
34dc7c2f BB |
2389 | return (error); |
2390 | } | |
2391 | ||
d5446cfc BB |
2392 | ulong_t zfs_fsync_sync_cnt = 4; |
2393 | ||
e5c39b95 | 2394 | int |
3558fd73 | 2395 | zfs_fsync(struct inode *ip, int syncflag, cred_t *cr) |
34dc7c2f | 2396 | { |
3558fd73 | 2397 | znode_t *zp = ITOZ(ip); |
0037b49e | 2398 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f | 2399 | |
d5446cfc BB |
2400 | (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); |
2401 | ||
0037b49e BB |
2402 | if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { |
2403 | ZFS_ENTER(zfsvfs); | |
428870ff | 2404 | ZFS_VERIFY_ZP(zp); |
0037b49e BB |
2405 | zil_commit(zfsvfs->z_log, zp->z_id); |
2406 | ZFS_EXIT(zfsvfs); | |
428870ff | 2407 | } |
07012da6 CC |
2408 | tsd_set(zfs_fsyncer_key, NULL); |
2409 | ||
34dc7c2f BB |
2410 | return (0); |
2411 | } | |
2412 | ||
2413 | ||
2414 | /* | |
2415 | * Get the requested file attributes and place them in the provided | |
2416 | * vattr structure. | |
2417 | * | |
3558fd73 | 2418 | * IN: ip - inode of file. |
5484965a BB |
2419 | * vap - va_mask identifies requested attributes. |
2420 | * If ATTR_XVATTR set, then optional attrs are requested | |
34dc7c2f BB |
2421 | * flags - ATTR_NOACLCHECK (CIFS server context) |
2422 | * cr - credentials of caller. | |
34dc7c2f | 2423 | * |
5484965a BB |
2424 | * OUT: vap - attribute values. |
2425 | * | |
2426 | * RETURN: 0 (always succeeds) | |
34dc7c2f BB |
2427 | */ |
2428 | /* ARGSUSED */ | |
e5c39b95 | 2429 | int |
5484965a | 2430 | zfs_getattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr) |
34dc7c2f | 2431 | { |
3558fd73 | 2432 | znode_t *zp = ITOZ(ip); |
0037b49e | 2433 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
2434 | int error = 0; |
2435 | uint64_t links; | |
0df9673f | 2436 | uint64_t atime[2], mtime[2], ctime[2]; |
5484965a BB |
2437 | xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ |
2438 | xoptattr_t *xoap = NULL; | |
34dc7c2f | 2439 | boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; |
0df9673f | 2440 | sa_bulk_attr_t bulk[3]; |
428870ff | 2441 | int count = 0; |
34dc7c2f | 2442 | |
0037b49e | 2443 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 2444 | ZFS_VERIFY_ZP(zp); |
428870ff | 2445 | |
5484965a | 2446 | zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); |
572e2857 | 2447 | |
0037b49e BB |
2448 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16); |
2449 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); | |
2450 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); | |
428870ff BB |
2451 | |
2452 | if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { | |
0037b49e | 2453 | ZFS_EXIT(zfsvfs); |
428870ff BB |
2454 | return (error); |
2455 | } | |
34dc7c2f | 2456 | |
34dc7c2f BB |
2457 | /* |
2458 | * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. | |
2459 | * Also, if we are the owner don't bother, since owner should | |
2460 | * always be allowed to read basic attributes of file. | |
2461 | */ | |
572e2857 | 2462 | if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && |
5484965a | 2463 | (vap->va_uid != crgetuid(cr))) { |
149e873a BB |
2464 | if ((error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, |
2465 | skipaclchk, cr))) { | |
0037b49e | 2466 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2467 | return (error); |
2468 | } | |
2469 | } | |
2470 | ||
2471 | /* | |
2472 | * Return all attributes. It's cheaper to provide the answer | |
2473 | * than to determine whether we were asked the question. | |
2474 | */ | |
2475 | ||
9babb374 | 2476 | mutex_enter(&zp->z_lock); |
5484965a BB |
2477 | vap->va_type = vn_mode_to_vtype(zp->z_mode); |
2478 | vap->va_mode = zp->z_mode; | |
53cf50e0 | 2479 | vap->va_fsid = ZTOI(zp)->i_sb->s_dev; |
5484965a | 2480 | vap->va_nodeid = zp->z_id; |
0037b49e | 2481 | if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp)) |
dfbc8630 | 2482 | links = ZTOI(zp)->i_nlink + 1; |
34dc7c2f | 2483 | else |
dfbc8630 | 2484 | links = ZTOI(zp)->i_nlink; |
5484965a BB |
2485 | vap->va_nlink = MIN(links, ZFS_LINK_MAX); |
2486 | vap->va_size = i_size_read(ip); | |
2487 | vap->va_rdev = ip->i_rdev; | |
2488 | vap->va_seq = ip->i_generation; | |
2489 | ||
2490 | /* | |
2491 | * Add in any requested optional attributes and the create time. | |
2492 | * Also set the corresponding bits in the returned attribute bitmap. | |
2493 | */ | |
0037b49e | 2494 | if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { |
5484965a BB |
2495 | if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { |
2496 | xoap->xoa_archive = | |
2497 | ((zp->z_pflags & ZFS_ARCHIVE) != 0); | |
2498 | XVA_SET_RTN(xvap, XAT_ARCHIVE); | |
2499 | } | |
2500 | ||
2501 | if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { | |
2502 | xoap->xoa_readonly = | |
2503 | ((zp->z_pflags & ZFS_READONLY) != 0); | |
2504 | XVA_SET_RTN(xvap, XAT_READONLY); | |
2505 | } | |
2506 | ||
2507 | if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { | |
2508 | xoap->xoa_system = | |
2509 | ((zp->z_pflags & ZFS_SYSTEM) != 0); | |
2510 | XVA_SET_RTN(xvap, XAT_SYSTEM); | |
2511 | } | |
2512 | ||
2513 | if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { | |
2514 | xoap->xoa_hidden = | |
2515 | ((zp->z_pflags & ZFS_HIDDEN) != 0); | |
2516 | XVA_SET_RTN(xvap, XAT_HIDDEN); | |
2517 | } | |
2518 | ||
2519 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { | |
2520 | xoap->xoa_nounlink = | |
2521 | ((zp->z_pflags & ZFS_NOUNLINK) != 0); | |
2522 | XVA_SET_RTN(xvap, XAT_NOUNLINK); | |
2523 | } | |
2524 | ||
2525 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
2526 | xoap->xoa_immutable = | |
2527 | ((zp->z_pflags & ZFS_IMMUTABLE) != 0); | |
2528 | XVA_SET_RTN(xvap, XAT_IMMUTABLE); | |
2529 | } | |
2530 | ||
2531 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
2532 | xoap->xoa_appendonly = | |
2533 | ((zp->z_pflags & ZFS_APPENDONLY) != 0); | |
2534 | XVA_SET_RTN(xvap, XAT_APPENDONLY); | |
2535 | } | |
2536 | ||
2537 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
2538 | xoap->xoa_nodump = | |
2539 | ((zp->z_pflags & ZFS_NODUMP) != 0); | |
2540 | XVA_SET_RTN(xvap, XAT_NODUMP); | |
2541 | } | |
2542 | ||
2543 | if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { | |
2544 | xoap->xoa_opaque = | |
2545 | ((zp->z_pflags & ZFS_OPAQUE) != 0); | |
2546 | XVA_SET_RTN(xvap, XAT_OPAQUE); | |
2547 | } | |
2548 | ||
2549 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
2550 | xoap->xoa_av_quarantined = | |
2551 | ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); | |
2552 | XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); | |
2553 | } | |
2554 | ||
2555 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
2556 | xoap->xoa_av_modified = | |
2557 | ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); | |
2558 | XVA_SET_RTN(xvap, XAT_AV_MODIFIED); | |
2559 | } | |
2560 | ||
2561 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && | |
2562 | S_ISREG(ip->i_mode)) { | |
2563 | zfs_sa_get_scanstamp(zp, xvap); | |
2564 | } | |
34dc7c2f | 2565 | |
5484965a BB |
2566 | if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { |
2567 | uint64_t times[2]; | |
2568 | ||
0037b49e | 2569 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), |
5484965a BB |
2570 | times, sizeof (times)); |
2571 | ZFS_TIME_DECODE(&xoap->xoa_createtime, times); | |
2572 | XVA_SET_RTN(xvap, XAT_CREATETIME); | |
2573 | } | |
2574 | ||
2575 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
2576 | xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); | |
2577 | XVA_SET_RTN(xvap, XAT_REPARSE); | |
2578 | } | |
2579 | if (XVA_ISSET_REQ(xvap, XAT_GEN)) { | |
278f2236 | 2580 | xoap->xoa_generation = ip->i_generation; |
5484965a BB |
2581 | XVA_SET_RTN(xvap, XAT_GEN); |
2582 | } | |
2583 | ||
2584 | if (XVA_ISSET_REQ(xvap, XAT_OFFLINE)) { | |
2585 | xoap->xoa_offline = | |
2586 | ((zp->z_pflags & ZFS_OFFLINE) != 0); | |
2587 | XVA_SET_RTN(xvap, XAT_OFFLINE); | |
2588 | } | |
2589 | ||
2590 | if (XVA_ISSET_REQ(xvap, XAT_SPARSE)) { | |
2591 | xoap->xoa_sparse = | |
2592 | ((zp->z_pflags & ZFS_SPARSE) != 0); | |
2593 | XVA_SET_RTN(xvap, XAT_SPARSE); | |
2594 | } | |
9c5167d1 NF |
2595 | |
2596 | if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) { | |
2597 | xoap->xoa_projinherit = | |
2598 | ((zp->z_pflags & ZFS_PROJINHERIT) != 0); | |
2599 | XVA_SET_RTN(xvap, XAT_PROJINHERIT); | |
2600 | } | |
2601 | ||
2602 | if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { | |
2603 | xoap->xoa_projid = zp->z_projid; | |
2604 | XVA_SET_RTN(xvap, XAT_PROJID); | |
2605 | } | |
5484965a BB |
2606 | } |
2607 | ||
0df9673f | 2608 | ZFS_TIME_DECODE(&vap->va_atime, atime); |
5484965a BB |
2609 | ZFS_TIME_DECODE(&vap->va_mtime, mtime); |
2610 | ZFS_TIME_DECODE(&vap->va_ctime, ctime); | |
34dc7c2f BB |
2611 | |
2612 | mutex_exit(&zp->z_lock); | |
2613 | ||
5484965a | 2614 | sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); |
34dc7c2f BB |
2615 | |
2616 | if (zp->z_blksz == 0) { | |
2617 | /* | |
2618 | * Block size hasn't been set; suggest maximal I/O transfers. | |
2619 | */ | |
0037b49e | 2620 | vap->va_blksize = zfsvfs->z_max_blksz; |
34dc7c2f BB |
2621 | } |
2622 | ||
0037b49e | 2623 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
2624 | return (0); |
2625 | } | |
2626 | ||
057e8eee BB |
2627 | /* |
2628 | * Get the basic file attributes and place them in the provided kstat | |
2629 | * structure. The inode is assumed to be the authoritative source | |
2630 | * for most of the attributes. However, the znode currently has the | |
2631 | * authoritative atime, blksize, and block count. | |
2632 | * | |
2633 | * IN: ip - inode of file. | |
2634 | * | |
2635 | * OUT: sp - kstat values. | |
2636 | * | |
2637 | * RETURN: 0 (always succeeds) | |
2638 | */ | |
2639 | /* ARGSUSED */ | |
2640 | int | |
2641 | zfs_getattr_fast(struct inode *ip, struct kstat *sp) | |
2642 | { | |
2643 | znode_t *zp = ITOZ(ip); | |
0037b49e | 2644 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
b585bc4a BB |
2645 | uint32_t blksize; |
2646 | u_longlong_t nblocks; | |
057e8eee | 2647 | |
0037b49e | 2648 | ZFS_ENTER(zfsvfs); |
a7b125e9 GB |
2649 | ZFS_VERIFY_ZP(zp); |
2650 | ||
057e8eee BB |
2651 | mutex_enter(&zp->z_lock); |
2652 | ||
2653 | generic_fillattr(ip, sp); | |
057e8eee | 2654 | |
b585bc4a BB |
2655 | sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); |
2656 | sp->blksize = blksize; | |
2657 | sp->blocks = nblocks; | |
2658 | ||
057e8eee BB |
2659 | if (unlikely(zp->z_blksz == 0)) { |
2660 | /* | |
2661 | * Block size hasn't been set; suggest maximal I/O transfers. | |
2662 | */ | |
0037b49e | 2663 | sp->blksize = zfsvfs->z_max_blksz; |
057e8eee BB |
2664 | } |
2665 | ||
2666 | mutex_exit(&zp->z_lock); | |
2667 | ||
aa9b2708 AV |
2668 | /* |
2669 | * Required to prevent NFS client from detecting different inode | |
2670 | * numbers of snapshot root dentry before and after snapshot mount. | |
2671 | */ | |
0037b49e | 2672 | if (zfsvfs->z_issnap) { |
aa9b2708 AV |
2673 | if (ip->i_sb->s_root->d_inode == ip) |
2674 | sp->ino = ZFSCTL_INO_SNAPDIRS - | |
0037b49e | 2675 | dmu_objset_id(zfsvfs->z_os); |
aa9b2708 AV |
2676 | } |
2677 | ||
0037b49e | 2678 | ZFS_EXIT(zfsvfs); |
a7b125e9 | 2679 | |
057e8eee BB |
2680 | return (0); |
2681 | } | |
057e8eee | 2682 | |
9c5167d1 NF |
2683 | /* |
2684 | * For the operation of changing file's user/group/project, we need to | |
2685 | * handle not only the main object that is assigned to the file directly, | |
2686 | * but also the ones that are used by the file via hidden xattr directory. | |
2687 | * | |
2688 | * Because the xattr directory may contains many EA entries, as to it may | |
2689 | * be impossible to change all of them via the transaction of changing the | |
2690 | * main object's user/group/project attributes. Then we have to change them | |
2691 | * via other multiple independent transactions one by one. It may be not good | |
2692 | * solution, but we have no better idea yet. | |
2693 | */ | |
2694 | static int | |
2695 | zfs_setattr_dir(znode_t *dzp) | |
2696 | { | |
2697 | struct inode *dxip = ZTOI(dzp); | |
2698 | struct inode *xip = NULL; | |
2699 | zfsvfs_t *zfsvfs = ITOZSB(dxip); | |
2700 | objset_t *os = zfsvfs->z_os; | |
2701 | zap_cursor_t zc; | |
2702 | zap_attribute_t zap; | |
2703 | zfs_dirlock_t *dl; | |
2704 | znode_t *zp; | |
2705 | dmu_tx_t *tx = NULL; | |
2706 | uint64_t uid, gid; | |
2707 | sa_bulk_attr_t bulk[4]; | |
2708 | int count = 0; | |
2709 | int err; | |
2710 | ||
2711 | zap_cursor_init(&zc, os, dzp->z_id); | |
2712 | while ((err = zap_cursor_retrieve(&zc, &zap)) == 0) { | |
2713 | if (zap.za_integer_length != 8 || zap.za_num_integers != 1) { | |
2714 | err = ENXIO; | |
2715 | break; | |
2716 | } | |
2717 | ||
2718 | err = zfs_dirent_lock(&dl, dzp, (char *)zap.za_name, &zp, | |
2719 | ZEXISTS, NULL, NULL); | |
2720 | if (err == ENOENT) | |
2721 | goto next; | |
2722 | if (err) | |
2723 | break; | |
2724 | ||
2725 | xip = ZTOI(zp); | |
2726 | if (KUID_TO_SUID(xip->i_uid) == KUID_TO_SUID(dxip->i_uid) && | |
2727 | KGID_TO_SGID(xip->i_gid) == KGID_TO_SGID(dxip->i_gid) && | |
2728 | zp->z_projid == dzp->z_projid) | |
2729 | goto next; | |
2730 | ||
2731 | tx = dmu_tx_create(os); | |
2732 | if (!(zp->z_pflags & ZFS_PROJID)) | |
2733 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); | |
2734 | else | |
2735 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); | |
2736 | ||
2737 | err = dmu_tx_assign(tx, TXG_WAIT); | |
2738 | if (err) | |
2739 | break; | |
2740 | ||
2741 | mutex_enter(&dzp->z_lock); | |
2742 | ||
2743 | if (KUID_TO_SUID(xip->i_uid) != KUID_TO_SUID(dxip->i_uid)) { | |
2744 | xip->i_uid = dxip->i_uid; | |
2745 | uid = zfs_uid_read(dxip); | |
2746 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, | |
2747 | &uid, sizeof (uid)); | |
2748 | } | |
2749 | ||
2750 | if (KGID_TO_SGID(xip->i_gid) != KGID_TO_SGID(dxip->i_gid)) { | |
2751 | xip->i_gid = dxip->i_gid; | |
2752 | gid = zfs_gid_read(dxip); | |
2753 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, | |
2754 | &gid, sizeof (gid)); | |
2755 | } | |
2756 | ||
2757 | if (zp->z_projid != dzp->z_projid) { | |
2758 | if (!(zp->z_pflags & ZFS_PROJID)) { | |
2759 | zp->z_pflags |= ZFS_PROJID; | |
2760 | SA_ADD_BULK_ATTR(bulk, count, | |
2761 | SA_ZPL_FLAGS(zfsvfs), NULL, &zp->z_pflags, | |
2762 | sizeof (zp->z_pflags)); | |
2763 | } | |
2764 | ||
2765 | zp->z_projid = dzp->z_projid; | |
2766 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PROJID(zfsvfs), | |
2767 | NULL, &zp->z_projid, sizeof (zp->z_projid)); | |
2768 | } | |
2769 | ||
2770 | mutex_exit(&dzp->z_lock); | |
2771 | ||
2772 | if (likely(count > 0)) { | |
2773 | err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
2774 | dmu_tx_commit(tx); | |
2775 | } else { | |
2776 | dmu_tx_abort(tx); | |
2777 | } | |
2778 | tx = NULL; | |
2779 | if (err != 0 && err != ENOENT) | |
2780 | break; | |
2781 | ||
2782 | next: | |
2783 | if (xip) { | |
2784 | iput(xip); | |
2785 | xip = NULL; | |
2786 | zfs_dirent_unlock(dl); | |
2787 | } | |
2788 | zap_cursor_advance(&zc); | |
2789 | } | |
2790 | ||
2791 | if (tx) | |
2792 | dmu_tx_abort(tx); | |
2793 | if (xip) { | |
2794 | iput(xip); | |
2795 | zfs_dirent_unlock(dl); | |
2796 | } | |
2797 | zap_cursor_fini(&zc); | |
2798 | ||
2799 | return (err == ENOENT ? 0 : err); | |
2800 | } | |
2801 | ||
34dc7c2f BB |
2802 | /* |
2803 | * Set the file attributes to the values contained in the | |
2804 | * vattr structure. | |
2805 | * | |
3558fd73 | 2806 | * IN: ip - inode of file to be modified. |
34dc7c2f | 2807 | * vap - new attribute values. |
5484965a | 2808 | * If ATTR_XVATTR set, then optional attrs are being set |
34dc7c2f BB |
2809 | * flags - ATTR_UTIME set if non-default time values provided. |
2810 | * - ATTR_NOACLCHECK (CIFS context only). | |
2811 | * cr - credentials of caller. | |
34dc7c2f BB |
2812 | * |
2813 | * RETURN: 0 if success | |
2814 | * error code if failure | |
2815 | * | |
2816 | * Timestamps: | |
3558fd73 | 2817 | * ip - ctime updated, mtime updated if size changed. |
34dc7c2f BB |
2818 | */ |
2819 | /* ARGSUSED */ | |
e5c39b95 | 2820 | int |
5484965a | 2821 | zfs_setattr(struct inode *ip, vattr_t *vap, int flags, cred_t *cr) |
34dc7c2f | 2822 | { |
3558fd73 | 2823 | znode_t *zp = ITOZ(ip); |
0037b49e | 2824 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
9c5167d1 | 2825 | objset_t *os = zfsvfs->z_os; |
34dc7c2f BB |
2826 | zilog_t *zilog; |
2827 | dmu_tx_t *tx; | |
2828 | vattr_t oldva; | |
f4ea75d4 | 2829 | xvattr_t *tmpxvattr; |
5484965a | 2830 | uint_t mask = vap->va_mask; |
a117a6d6 | 2831 | uint_t saved_mask = 0; |
34dc7c2f BB |
2832 | int trim_mask = 0; |
2833 | uint64_t new_mode; | |
64aefee1 | 2834 | uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid; |
572e2857 | 2835 | uint64_t xattr_obj; |
0df9673f | 2836 | uint64_t mtime[2], ctime[2], atime[2]; |
9c5167d1 | 2837 | uint64_t projid = ZFS_INVALID_PROJID; |
34dc7c2f BB |
2838 | znode_t *attrzp; |
2839 | int need_policy = FALSE; | |
9c5167d1 | 2840 | int err, err2 = 0; |
34dc7c2f | 2841 | zfs_fuid_info_t *fuidp = NULL; |
5484965a BB |
2842 | xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ |
2843 | xoptattr_t *xoap; | |
2844 | zfs_acl_t *aclp; | |
34dc7c2f | 2845 | boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; |
428870ff | 2846 | boolean_t fuid_dirtied = B_FALSE; |
9c5167d1 | 2847 | boolean_t handle_eadir = B_FALSE; |
17c37660 | 2848 | sa_bulk_attr_t *bulk, *xattr_bulk; |
9c5167d1 | 2849 | int count = 0, xattr_count = 0, bulks = 8; |
34dc7c2f BB |
2850 | |
2851 | if (mask == 0) | |
2852 | return (0); | |
2853 | ||
0037b49e | 2854 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
2855 | ZFS_VERIFY_ZP(zp); |
2856 | ||
9c5167d1 NF |
2857 | /* |
2858 | * If this is a xvattr_t, then get a pointer to the structure of | |
2859 | * optional attributes. If this is NULL, then we have a vattr_t. | |
2860 | */ | |
2861 | xoap = xva_getxoptattr(xvap); | |
2862 | if (xoap != NULL && (mask & ATTR_XVATTR)) { | |
2863 | if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { | |
2864 | if (!dmu_objset_projectquota_enabled(os) || | |
2865 | (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode))) { | |
2866 | ZFS_EXIT(zfsvfs); | |
2867 | return (SET_ERROR(ENOTSUP)); | |
2868 | } | |
2869 | ||
2870 | projid = xoap->xoa_projid; | |
2871 | if (unlikely(projid == ZFS_INVALID_PROJID)) { | |
2872 | ZFS_EXIT(zfsvfs); | |
2873 | return (SET_ERROR(EINVAL)); | |
2874 | } | |
2875 | ||
2876 | if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID) | |
2877 | projid = ZFS_INVALID_PROJID; | |
2878 | else | |
2879 | need_policy = TRUE; | |
2880 | } | |
2881 | ||
2882 | if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) && | |
2705ebf0 NF |
2883 | (xoap->xoa_projinherit != |
2884 | ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) && | |
9c5167d1 NF |
2885 | (!dmu_objset_projectquota_enabled(os) || |
2886 | (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode)))) { | |
2705ebf0 NF |
2887 | ZFS_EXIT(zfsvfs); |
2888 | return (SET_ERROR(ENOTSUP)); | |
9c5167d1 NF |
2889 | } |
2890 | } | |
2891 | ||
0037b49e | 2892 | zilog = zfsvfs->z_log; |
34dc7c2f BB |
2893 | |
2894 | /* | |
2895 | * Make sure that if we have ephemeral uid/gid or xvattr specified | |
2896 | * that file system is at proper version level | |
2897 | */ | |
5484965a | 2898 | |
0037b49e | 2899 | if (zfsvfs->z_use_fuids == B_FALSE && |
5484965a BB |
2900 | (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) || |
2901 | ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) || | |
2902 | (mask & ATTR_XVATTR))) { | |
0037b49e | 2903 | ZFS_EXIT(zfsvfs); |
2e528b49 | 2904 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
2905 | } |
2906 | ||
3558fd73 | 2907 | if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) { |
0037b49e | 2908 | ZFS_EXIT(zfsvfs); |
2e528b49 | 2909 | return (SET_ERROR(EISDIR)); |
34dc7c2f BB |
2910 | } |
2911 | ||
3558fd73 | 2912 | if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) { |
0037b49e | 2913 | ZFS_EXIT(zfsvfs); |
2e528b49 | 2914 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
2915 | } |
2916 | ||
d1d7e268 | 2917 | tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP); |
f4ea75d4 | 2918 | xva_init(tmpxvattr); |
5484965a | 2919 | |
9c5167d1 NF |
2920 | bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP); |
2921 | xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP); | |
17c37660 | 2922 | |
5484965a BB |
2923 | /* |
2924 | * Immutable files can only alter immutable bit and atime | |
2925 | */ | |
2926 | if ((zp->z_pflags & ZFS_IMMUTABLE) && | |
2927 | ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) || | |
2928 | ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { | |
ecb2b7dc | 2929 | err = SET_ERROR(EPERM); |
f4ea75d4 | 2930 | goto out3; |
5484965a BB |
2931 | } |
2932 | ||
3558fd73 | 2933 | if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) { |
ecb2b7dc | 2934 | err = SET_ERROR(EPERM); |
f4ea75d4 | 2935 | goto out3; |
34dc7c2f BB |
2936 | } |
2937 | ||
5484965a BB |
2938 | /* |
2939 | * Verify timestamps doesn't overflow 32 bits. | |
2940 | * ZFS can handle large timestamps, but 32bit syscalls can't | |
2941 | * handle times greater than 2039. This check should be removed | |
2942 | * once large timestamps are fully supported. | |
2943 | */ | |
2944 | if (mask & (ATTR_ATIME | ATTR_MTIME)) { | |
d1d7e268 MK |
2945 | if (((mask & ATTR_ATIME) && |
2946 | TIMESPEC_OVERFLOW(&vap->va_atime)) || | |
2947 | ((mask & ATTR_MTIME) && | |
2948 | TIMESPEC_OVERFLOW(&vap->va_mtime))) { | |
ecb2b7dc | 2949 | err = SET_ERROR(EOVERFLOW); |
f4ea75d4 | 2950 | goto out3; |
5484965a BB |
2951 | } |
2952 | } | |
2953 | ||
34dc7c2f BB |
2954 | top: |
2955 | attrzp = NULL; | |
572e2857 | 2956 | aclp = NULL; |
34dc7c2f | 2957 | |
45d1cae3 | 2958 | /* Can this be moved to before the top label? */ |
0037b49e | 2959 | if (zfs_is_readonly(zfsvfs)) { |
ecb2b7dc | 2960 | err = SET_ERROR(EROFS); |
f4ea75d4 | 2961 | goto out3; |
34dc7c2f BB |
2962 | } |
2963 | ||
2964 | /* | |
2965 | * First validate permissions | |
2966 | */ | |
2967 | ||
3558fd73 | 2968 | if (mask & ATTR_SIZE) { |
34dc7c2f | 2969 | err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); |
f4ea75d4 BB |
2970 | if (err) |
2971 | goto out3; | |
2972 | ||
34dc7c2f BB |
2973 | /* |
2974 | * XXX - Note, we are not providing any open | |
2975 | * mode flags here (like FNDELAY), so we may | |
2976 | * block if there are locks present... this | |
2977 | * should be addressed in openat(). | |
2978 | */ | |
b128c09f | 2979 | /* XXX - would it be OK to generate a log record here? */ |
5484965a | 2980 | err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); |
f4ea75d4 BB |
2981 | if (err) |
2982 | goto out3; | |
428870ff | 2983 | } |
34dc7c2f | 2984 | |
5484965a BB |
2985 | if (mask & (ATTR_ATIME|ATTR_MTIME) || |
2986 | ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || | |
2987 | XVA_ISSET_REQ(xvap, XAT_READONLY) || | |
2988 | XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || | |
2989 | XVA_ISSET_REQ(xvap, XAT_OFFLINE) || | |
2990 | XVA_ISSET_REQ(xvap, XAT_SPARSE) || | |
2991 | XVA_ISSET_REQ(xvap, XAT_CREATETIME) || | |
2992 | XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { | |
2993 | need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, | |
2994 | skipaclchk, cr); | |
2995 | } | |
2996 | ||
3558fd73 BB |
2997 | if (mask & (ATTR_UID|ATTR_GID)) { |
2998 | int idmask = (mask & (ATTR_UID|ATTR_GID)); | |
34dc7c2f BB |
2999 | int take_owner; |
3000 | int take_group; | |
3001 | ||
3002 | /* | |
3003 | * NOTE: even if a new mode is being set, | |
3004 | * we may clear S_ISUID/S_ISGID bits. | |
3005 | */ | |
3006 | ||
3558fd73 | 3007 | if (!(mask & ATTR_MODE)) |
5484965a | 3008 | vap->va_mode = zp->z_mode; |
34dc7c2f BB |
3009 | |
3010 | /* | |
3011 | * Take ownership or chgrp to group we are a member of | |
3012 | */ | |
3013 | ||
5484965a | 3014 | take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr)); |
3558fd73 | 3015 | take_group = (mask & ATTR_GID) && |
0037b49e | 3016 | zfs_groupmember(zfsvfs, vap->va_gid, cr); |
34dc7c2f BB |
3017 | |
3018 | /* | |
5484965a | 3019 | * If both ATTR_UID and ATTR_GID are set then take_owner and |
34dc7c2f BB |
3020 | * take_group must both be set in order to allow taking |
3021 | * ownership. | |
3022 | * | |
3023 | * Otherwise, send the check through secpolicy_vnode_setattr() | |
3024 | * | |
3025 | */ | |
3026 | ||
3558fd73 BB |
3027 | if (((idmask == (ATTR_UID|ATTR_GID)) && |
3028 | take_owner && take_group) || | |
3029 | ((idmask == ATTR_UID) && take_owner) || | |
3030 | ((idmask == ATTR_GID) && take_group)) { | |
34dc7c2f BB |
3031 | if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, |
3032 | skipaclchk, cr) == 0) { | |
3033 | /* | |
3034 | * Remove setuid/setgid for non-privileged users | |
3035 | */ | |
5484965a | 3036 | (void) secpolicy_setid_clear(vap, cr); |
3558fd73 | 3037 | trim_mask = (mask & (ATTR_UID|ATTR_GID)); |
34dc7c2f BB |
3038 | } else { |
3039 | need_policy = TRUE; | |
3040 | } | |
3041 | } else { | |
3042 | need_policy = TRUE; | |
3043 | } | |
3044 | } | |
3045 | ||
3046 | mutex_enter(&zp->z_lock); | |
428870ff | 3047 | oldva.va_mode = zp->z_mode; |
572e2857 | 3048 | zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); |
5484965a BB |
3049 | if (mask & ATTR_XVATTR) { |
3050 | /* | |
3051 | * Update xvattr mask to include only those attributes | |
3052 | * that are actually changing. | |
3053 | * | |
3054 | * the bits will be restored prior to actually setting | |
3055 | * the attributes so the caller thinks they were set. | |
3056 | */ | |
3057 | if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { | |
3058 | if (xoap->xoa_appendonly != | |
3059 | ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { | |
3060 | need_policy = TRUE; | |
3061 | } else { | |
3062 | XVA_CLR_REQ(xvap, XAT_APPENDONLY); | |
f4ea75d4 | 3063 | XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY); |
5484965a BB |
3064 | } |
3065 | } | |
3066 | ||
9c5167d1 NF |
3067 | if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) { |
3068 | if (xoap->xoa_projinherit != | |
3069 | ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) { | |
3070 | need_policy = TRUE; | |
3071 | } else { | |
3072 | XVA_CLR_REQ(xvap, XAT_PROJINHERIT); | |
3073 | XVA_SET_REQ(tmpxvattr, XAT_PROJINHERIT); | |
3074 | } | |
3075 | } | |
3076 | ||
5484965a BB |
3077 | if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { |
3078 | if (xoap->xoa_nounlink != | |
3079 | ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { | |
3080 | need_policy = TRUE; | |
3081 | } else { | |
3082 | XVA_CLR_REQ(xvap, XAT_NOUNLINK); | |
f4ea75d4 | 3083 | XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK); |
5484965a BB |
3084 | } |
3085 | } | |
3086 | ||
3087 | if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { | |
3088 | if (xoap->xoa_immutable != | |
3089 | ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { | |
3090 | need_policy = TRUE; | |
3091 | } else { | |
3092 | XVA_CLR_REQ(xvap, XAT_IMMUTABLE); | |
f4ea75d4 | 3093 | XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE); |
5484965a BB |
3094 | } |
3095 | } | |
3096 | ||
3097 | if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { | |
3098 | if (xoap->xoa_nodump != | |
3099 | ((zp->z_pflags & ZFS_NODUMP) != 0)) { | |
3100 | need_policy = TRUE; | |
3101 | } else { | |
3102 | XVA_CLR_REQ(xvap, XAT_NODUMP); | |
f4ea75d4 | 3103 | XVA_SET_REQ(tmpxvattr, XAT_NODUMP); |
5484965a BB |
3104 | } |
3105 | } | |
3106 | ||
3107 | if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { | |
3108 | if (xoap->xoa_av_modified != | |
3109 | ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { | |
3110 | need_policy = TRUE; | |
3111 | } else { | |
3112 | XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); | |
f4ea75d4 | 3113 | XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED); |
5484965a BB |
3114 | } |
3115 | } | |
3116 | ||
3117 | if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { | |
3118 | if ((!S_ISREG(ip->i_mode) && | |
3119 | xoap->xoa_av_quarantined) || | |
3120 | xoap->xoa_av_quarantined != | |
3121 | ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { | |
3122 | need_policy = TRUE; | |
3123 | } else { | |
3124 | XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); | |
f4ea75d4 | 3125 | XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED); |
5484965a BB |
3126 | } |
3127 | } | |
3128 | ||
3129 | if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { | |
3130 | mutex_exit(&zp->z_lock); | |
ecb2b7dc | 3131 | err = SET_ERROR(EPERM); |
f4ea75d4 | 3132 | goto out3; |
5484965a BB |
3133 | } |
3134 | ||
3135 | if (need_policy == FALSE && | |
3136 | (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || | |
3137 | XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { | |
3138 | need_policy = TRUE; | |
3139 | } | |
3140 | } | |
34dc7c2f BB |
3141 | |
3142 | mutex_exit(&zp->z_lock); | |
3143 | ||
3558fd73 | 3144 | if (mask & ATTR_MODE) { |
34dc7c2f | 3145 | if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { |
5484965a | 3146 | err = secpolicy_setid_setsticky_clear(ip, vap, |
34dc7c2f | 3147 | &oldva, cr); |
f4ea75d4 BB |
3148 | if (err) |
3149 | goto out3; | |
3150 | ||
3558fd73 | 3151 | trim_mask |= ATTR_MODE; |
34dc7c2f BB |
3152 | } else { |
3153 | need_policy = TRUE; | |
3154 | } | |
3155 | } | |
3156 | ||
3157 | if (need_policy) { | |
3158 | /* | |
3159 | * If trim_mask is set then take ownership | |
3160 | * has been granted or write_acl is present and user | |
3161 | * has the ability to modify mode. In that case remove | |
3162 | * UID|GID and or MODE from mask so that | |
3163 | * secpolicy_vnode_setattr() doesn't revoke it. | |
3164 | */ | |
3165 | ||
3166 | if (trim_mask) { | |
5484965a BB |
3167 | saved_mask = vap->va_mask; |
3168 | vap->va_mask &= ~trim_mask; | |
34dc7c2f | 3169 | } |
5484965a | 3170 | err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags, |
34dc7c2f | 3171 | (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); |
f4ea75d4 BB |
3172 | if (err) |
3173 | goto out3; | |
34dc7c2f BB |
3174 | |
3175 | if (trim_mask) | |
5484965a | 3176 | vap->va_mask |= saved_mask; |
34dc7c2f BB |
3177 | } |
3178 | ||
3179 | /* | |
3180 | * secpolicy_vnode_setattr, or take ownership may have | |
3181 | * changed va_mask | |
3182 | */ | |
5484965a | 3183 | mask = vap->va_mask; |
34dc7c2f | 3184 | |
9c5167d1 NF |
3185 | if ((mask & (ATTR_UID | ATTR_GID)) || projid != ZFS_INVALID_PROJID) { |
3186 | handle_eadir = B_TRUE; | |
0037b49e | 3187 | err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), |
572e2857 | 3188 | &xattr_obj, sizeof (xattr_obj)); |
428870ff | 3189 | |
572e2857 | 3190 | if (err == 0 && xattr_obj) { |
3558fd73 | 3191 | err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp); |
428870ff BB |
3192 | if (err) |
3193 | goto out2; | |
3194 | } | |
3558fd73 | 3195 | if (mask & ATTR_UID) { |
0037b49e | 3196 | new_kuid = zfs_fuid_create(zfsvfs, |
5484965a | 3197 | (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); |
64aefee1 | 3198 | if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) && |
9c5167d1 NF |
3199 | zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT, |
3200 | new_kuid)) { | |
572e2857 | 3201 | if (attrzp) |
3558fd73 | 3202 | iput(ZTOI(attrzp)); |
ecb2b7dc | 3203 | err = SET_ERROR(EDQUOT); |
428870ff BB |
3204 | goto out2; |
3205 | } | |
3206 | } | |
3207 | ||
3558fd73 | 3208 | if (mask & ATTR_GID) { |
0037b49e BB |
3209 | new_kgid = zfs_fuid_create(zfsvfs, |
3210 | (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp); | |
64aefee1 | 3211 | if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) && |
9c5167d1 NF |
3212 | zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT, |
3213 | new_kgid)) { | |
572e2857 | 3214 | if (attrzp) |
3558fd73 | 3215 | iput(ZTOI(attrzp)); |
ecb2b7dc | 3216 | err = SET_ERROR(EDQUOT); |
428870ff BB |
3217 | goto out2; |
3218 | } | |
3219 | } | |
9c5167d1 NF |
3220 | |
3221 | if (projid != ZFS_INVALID_PROJID && | |
3222 | zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) { | |
3223 | if (attrzp) | |
3224 | iput(ZTOI(attrzp)); | |
3225 | err = EDQUOT; | |
3226 | goto out2; | |
3227 | } | |
428870ff | 3228 | } |
9c5167d1 | 3229 | tx = dmu_tx_create(os); |
34dc7c2f | 3230 | |
3558fd73 | 3231 | if (mask & ATTR_MODE) { |
428870ff | 3232 | uint64_t pmode = zp->z_mode; |
572e2857 | 3233 | uint64_t acl_obj; |
5484965a | 3234 | new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); |
34dc7c2f | 3235 | |
572e2857 | 3236 | zfs_acl_chmod_setattr(zp, &aclp, new_mode); |
428870ff | 3237 | |
572e2857 BB |
3238 | mutex_enter(&zp->z_lock); |
3239 | if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { | |
428870ff BB |
3240 | /* |
3241 | * Are we upgrading ACL from old V0 format | |
3242 | * to V1 format? | |
3243 | */ | |
0037b49e | 3244 | if (zfsvfs->z_version >= ZPL_VERSION_FUID && |
572e2857 | 3245 | zfs_znode_acl_version(zp) == |
34dc7c2f | 3246 | ZFS_ACL_VERSION_INITIAL) { |
572e2857 | 3247 | dmu_tx_hold_free(tx, acl_obj, 0, |
34dc7c2f BB |
3248 | DMU_OBJECT_END); |
3249 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, | |
3250 | 0, aclp->z_acl_bytes); | |
3251 | } else { | |
572e2857 | 3252 | dmu_tx_hold_write(tx, acl_obj, 0, |
34dc7c2f BB |
3253 | aclp->z_acl_bytes); |
3254 | } | |
428870ff | 3255 | } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
34dc7c2f BB |
3256 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, |
3257 | 0, aclp->z_acl_bytes); | |
3258 | } | |
572e2857 | 3259 | mutex_exit(&zp->z_lock); |
428870ff BB |
3260 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); |
3261 | } else { | |
9c5167d1 NF |
3262 | if (((mask & ATTR_XVATTR) && |
3263 | XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) || | |
3264 | (projid != ZFS_INVALID_PROJID && | |
3265 | !(zp->z_pflags & ZFS_PROJID))) | |
5484965a BB |
3266 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); |
3267 | else | |
3268 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
3269 | } |
3270 | ||
428870ff BB |
3271 | if (attrzp) { |
3272 | dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
3273 | } |
3274 | ||
0037b49e | 3275 | fuid_dirtied = zfsvfs->z_fuid_dirty; |
428870ff | 3276 | if (fuid_dirtied) |
0037b49e | 3277 | zfs_fuid_txhold(zfsvfs, tx); |
428870ff BB |
3278 | |
3279 | zfs_sa_upgrade_txholds(tx, zp); | |
3280 | ||
384f8a09 MA |
3281 | err = dmu_tx_assign(tx, TXG_WAIT); |
3282 | if (err) | |
9babb374 | 3283 | goto out; |
34dc7c2f | 3284 | |
428870ff | 3285 | count = 0; |
34dc7c2f BB |
3286 | /* |
3287 | * Set each attribute requested. | |
3288 | * We group settings according to the locks they need to acquire. | |
3289 | * | |
3290 | * Note: you cannot set ctime directly, although it will be | |
3291 | * updated as a side-effect of calling this function. | |
3292 | */ | |
3293 | ||
9c5167d1 NF |
3294 | if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) { |
3295 | /* | |
3296 | * For the existed object that is upgraded from old system, | |
3297 | * its on-disk layout has no slot for the project ID attribute. | |
3298 | * But quota accounting logic needs to access related slots by | |
3299 | * offset directly. So we need to adjust old objects' layout | |
3300 | * to make the project ID to some unified and fixed offset. | |
3301 | */ | |
3302 | if (attrzp) | |
3303 | err = sa_add_projid(attrzp->z_sa_hdl, tx, projid); | |
3304 | if (err == 0) | |
3305 | err = sa_add_projid(zp->z_sa_hdl, tx, projid); | |
3306 | ||
3307 | if (unlikely(err == EEXIST)) | |
3308 | err = 0; | |
3309 | else if (err != 0) | |
3310 | goto out; | |
3311 | else | |
3312 | projid = ZFS_INVALID_PROJID; | |
3313 | } | |
572e2857 | 3314 | |
3558fd73 | 3315 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 3316 | mutex_enter(&zp->z_acl_lock); |
34dc7c2f BB |
3317 | mutex_enter(&zp->z_lock); |
3318 | ||
0037b49e | 3319 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, |
428870ff BB |
3320 | &zp->z_pflags, sizeof (zp->z_pflags)); |
3321 | ||
3322 | if (attrzp) { | |
3558fd73 | 3323 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 3324 | mutex_enter(&attrzp->z_acl_lock); |
428870ff BB |
3325 | mutex_enter(&attrzp->z_lock); |
3326 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
0037b49e | 3327 | SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, |
428870ff | 3328 | sizeof (attrzp->z_pflags)); |
9c5167d1 NF |
3329 | if (projid != ZFS_INVALID_PROJID) { |
3330 | attrzp->z_projid = projid; | |
3331 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
3332 | SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid, | |
3333 | sizeof (attrzp->z_projid)); | |
3334 | } | |
428870ff BB |
3335 | } |
3336 | ||
3558fd73 | 3337 | if (mask & (ATTR_UID|ATTR_GID)) { |
428870ff | 3338 | |
3558fd73 | 3339 | if (mask & ATTR_UID) { |
64aefee1 NB |
3340 | ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid); |
3341 | new_uid = zfs_uid_read(ZTOI(zp)); | |
0037b49e | 3342 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, |
428870ff | 3343 | &new_uid, sizeof (new_uid)); |
428870ff BB |
3344 | if (attrzp) { |
3345 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
0037b49e | 3346 | SA_ZPL_UID(zfsvfs), NULL, &new_uid, |
428870ff | 3347 | sizeof (new_uid)); |
2c6abf15 | 3348 | ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid); |
428870ff BB |
3349 | } |
3350 | } | |
3351 | ||
3558fd73 | 3352 | if (mask & ATTR_GID) { |
64aefee1 NB |
3353 | ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid); |
3354 | new_gid = zfs_gid_read(ZTOI(zp)); | |
0037b49e | 3355 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), |
428870ff | 3356 | NULL, &new_gid, sizeof (new_gid)); |
428870ff BB |
3357 | if (attrzp) { |
3358 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
0037b49e | 3359 | SA_ZPL_GID(zfsvfs), NULL, &new_gid, |
428870ff | 3360 | sizeof (new_gid)); |
64aefee1 | 3361 | ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid); |
428870ff BB |
3362 | } |
3363 | } | |
3558fd73 | 3364 | if (!(mask & ATTR_MODE)) { |
0037b49e | 3365 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), |
428870ff BB |
3366 | NULL, &new_mode, sizeof (new_mode)); |
3367 | new_mode = zp->z_mode; | |
3368 | } | |
3369 | err = zfs_acl_chown_setattr(zp); | |
3370 | ASSERT(err == 0); | |
3371 | if (attrzp) { | |
3372 | err = zfs_acl_chown_setattr(attrzp); | |
3373 | ASSERT(err == 0); | |
3374 | } | |
3375 | } | |
3376 | ||
3558fd73 | 3377 | if (mask & ATTR_MODE) { |
0037b49e | 3378 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, |
428870ff | 3379 | &new_mode, sizeof (new_mode)); |
12fa7f34 | 3380 | zp->z_mode = ZTOI(zp)->i_mode = new_mode; |
99c564bc | 3381 | ASSERT3P(aclp, !=, NULL); |
9babb374 | 3382 | err = zfs_aclset_common(zp, aclp, cr, tx); |
c99c9001 | 3383 | ASSERT0(err); |
572e2857 BB |
3384 | if (zp->z_acl_cached) |
3385 | zfs_acl_free(zp->z_acl_cached); | |
45d1cae3 BB |
3386 | zp->z_acl_cached = aclp; |
3387 | aclp = NULL; | |
34dc7c2f BB |
3388 | } |
3389 | ||
704cd075 CC |
3390 | if ((mask & ATTR_ATIME) || zp->z_atime_dirty) { |
3391 | zp->z_atime_dirty = 0; | |
3392 | ZFS_TIME_ENCODE(&ip->i_atime, atime); | |
0037b49e | 3393 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, |
0df9673f | 3394 | &atime, sizeof (atime)); |
34dc7c2f BB |
3395 | } |
3396 | ||
99834d19 | 3397 | if (mask & (ATTR_MTIME | ATTR_SIZE)) { |
5484965a | 3398 | ZFS_TIME_ENCODE(&vap->va_mtime, mtime); |
87f9371a NB |
3399 | ZTOI(zp)->i_mtime = timespec_trunc(vap->va_mtime, |
3400 | ZTOI(zp)->i_sb->s_time_gran); | |
3401 | ||
0037b49e | 3402 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, |
428870ff | 3403 | mtime, sizeof (mtime)); |
34dc7c2f BB |
3404 | } |
3405 | ||
99834d19 | 3406 | if (mask & (ATTR_CTIME | ATTR_SIZE)) { |
87f9371a NB |
3407 | ZFS_TIME_ENCODE(&vap->va_ctime, ctime); |
3408 | ZTOI(zp)->i_ctime = timespec_trunc(vap->va_ctime, | |
3409 | ZTOI(zp)->i_sb->s_time_gran); | |
0037b49e | 3410 | SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, |
87f9371a | 3411 | ctime, sizeof (ctime)); |
428870ff | 3412 | } |
87f9371a | 3413 | |
9c5167d1 NF |
3414 | if (projid != ZFS_INVALID_PROJID) { |
3415 | zp->z_projid = projid; | |
3416 | SA_ADD_BULK_ATTR(bulk, count, | |
3417 | SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid, | |
3418 | sizeof (zp->z_projid)); | |
3419 | } | |
3420 | ||
87f9371a NB |
3421 | if (attrzp && mask) { |
3422 | SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, | |
0037b49e | 3423 | SA_ZPL_CTIME(zfsvfs), NULL, &ctime, |
87f9371a NB |
3424 | sizeof (ctime)); |
3425 | } | |
3426 | ||
34dc7c2f BB |
3427 | /* |
3428 | * Do this after setting timestamps to prevent timestamp | |
3429 | * update from toggling bit | |
3430 | */ | |
3431 | ||
5484965a BB |
3432 | if (xoap && (mask & ATTR_XVATTR)) { |
3433 | ||
3434 | /* | |
3435 | * restore trimmed off masks | |
3436 | * so that return masks can be set for caller. | |
3437 | */ | |
3438 | ||
f4ea75d4 | 3439 | if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) { |
5484965a BB |
3440 | XVA_SET_REQ(xvap, XAT_APPENDONLY); |
3441 | } | |
f4ea75d4 | 3442 | if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) { |
5484965a BB |
3443 | XVA_SET_REQ(xvap, XAT_NOUNLINK); |
3444 | } | |
f4ea75d4 | 3445 | if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) { |
5484965a BB |
3446 | XVA_SET_REQ(xvap, XAT_IMMUTABLE); |
3447 | } | |
f4ea75d4 | 3448 | if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) { |
5484965a BB |
3449 | XVA_SET_REQ(xvap, XAT_NODUMP); |
3450 | } | |
f4ea75d4 | 3451 | if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) { |
5484965a BB |
3452 | XVA_SET_REQ(xvap, XAT_AV_MODIFIED); |
3453 | } | |
f4ea75d4 | 3454 | if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) { |
5484965a BB |
3455 | XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); |
3456 | } | |
9c5167d1 NF |
3457 | if (XVA_ISSET_REQ(tmpxvattr, XAT_PROJINHERIT)) { |
3458 | XVA_SET_REQ(xvap, XAT_PROJINHERIT); | |
3459 | } | |
5484965a BB |
3460 | |
3461 | if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) | |
3462 | ASSERT(S_ISREG(ip->i_mode)); | |
3463 | ||
3464 | zfs_xvattr_set(zp, xvap, tx); | |
3465 | } | |
3466 | ||
9babb374 | 3467 | if (fuid_dirtied) |
0037b49e | 3468 | zfs_fuid_sync(zfsvfs, tx); |
9babb374 | 3469 | |
34dc7c2f | 3470 | if (mask != 0) |
5484965a | 3471 | zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); |
34dc7c2f | 3472 | |
34dc7c2f | 3473 | mutex_exit(&zp->z_lock); |
3558fd73 | 3474 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 | 3475 | mutex_exit(&zp->z_acl_lock); |
34dc7c2f | 3476 | |
572e2857 | 3477 | if (attrzp) { |
3558fd73 | 3478 | if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) |
572e2857 BB |
3479 | mutex_exit(&attrzp->z_acl_lock); |
3480 | mutex_exit(&attrzp->z_lock); | |
3481 | } | |
9babb374 | 3482 | out: |
9c5167d1 | 3483 | if (err == 0 && xattr_count > 0) { |
428870ff BB |
3484 | err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, |
3485 | xattr_count, tx); | |
3486 | ASSERT(err2 == 0); | |
3487 | } | |
3488 | ||
45d1cae3 | 3489 | if (aclp) |
9babb374 | 3490 | zfs_acl_free(aclp); |
9babb374 BB |
3491 | |
3492 | if (fuidp) { | |
3493 | zfs_fuid_info_free(fuidp); | |
3494 | fuidp = NULL; | |
3495 | } | |
3496 | ||
428870ff | 3497 | if (err) { |
9babb374 | 3498 | dmu_tx_abort(tx); |
ea7e86d8 BB |
3499 | if (attrzp) |
3500 | iput(ZTOI(attrzp)); | |
428870ff BB |
3501 | if (err == ERESTART) |
3502 | goto top; | |
3503 | } else { | |
9c5167d1 NF |
3504 | if (count > 0) |
3505 | err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); | |
9babb374 | 3506 | dmu_tx_commit(tx); |
9c5167d1 NF |
3507 | if (attrzp) { |
3508 | if (err2 == 0 && handle_eadir) | |
3509 | err2 = zfs_setattr_dir(attrzp); | |
ea7e86d8 | 3510 | iput(ZTOI(attrzp)); |
9c5167d1 | 3511 | } |
037849f8 | 3512 | zfs_inode_update(zp); |
428870ff BB |
3513 | } |
3514 | ||
428870ff | 3515 | out2: |
9c5167d1 | 3516 | if (os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3517 | zil_commit(zilog, 0); |
34dc7c2f | 3518 | |
f4ea75d4 | 3519 | out3: |
9c5167d1 NF |
3520 | kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * bulks); |
3521 | kmem_free(bulk, sizeof (sa_bulk_attr_t) * bulks); | |
d1d7e268 | 3522 | kmem_free(tmpxvattr, sizeof (xvattr_t)); |
0037b49e | 3523 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3524 | return (err); |
3525 | } | |
3526 | ||
3527 | typedef struct zfs_zlock { | |
3528 | krwlock_t *zl_rwlock; /* lock we acquired */ | |
3529 | znode_t *zl_znode; /* znode we held */ | |
3530 | struct zfs_zlock *zl_next; /* next in list */ | |
3531 | } zfs_zlock_t; | |
3532 | ||
3533 | /* | |
3534 | * Drop locks and release vnodes that were held by zfs_rename_lock(). | |
3535 | */ | |
3536 | static void | |
3537 | zfs_rename_unlock(zfs_zlock_t **zlpp) | |
3538 | { | |
3539 | zfs_zlock_t *zl; | |
3540 | ||
3541 | while ((zl = *zlpp) != NULL) { | |
3542 | if (zl->zl_znode != NULL) | |
ea7e86d8 | 3543 | zfs_iput_async(ZTOI(zl->zl_znode)); |
34dc7c2f BB |
3544 | rw_exit(zl->zl_rwlock); |
3545 | *zlpp = zl->zl_next; | |
3546 | kmem_free(zl, sizeof (*zl)); | |
3547 | } | |
3548 | } | |
3549 | ||
3550 | /* | |
3551 | * Search back through the directory tree, using the ".." entries. | |
3552 | * Lock each directory in the chain to prevent concurrent renames. | |
3553 | * Fail any attempt to move a directory into one of its own descendants. | |
3554 | * XXX - z_parent_lock can overlap with map or grow locks | |
3555 | */ | |
3556 | static int | |
3557 | zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) | |
3558 | { | |
3559 | zfs_zlock_t *zl; | |
3560 | znode_t *zp = tdzp; | |
3558fd73 | 3561 | uint64_t rootid = ZTOZSB(zp)->z_root; |
428870ff | 3562 | uint64_t oidp = zp->z_id; |
34dc7c2f BB |
3563 | krwlock_t *rwlp = &szp->z_parent_lock; |
3564 | krw_t rw = RW_WRITER; | |
3565 | ||
3566 | /* | |
3567 | * First pass write-locks szp and compares to zp->z_id. | |
3568 | * Later passes read-lock zp and compare to zp->z_parent. | |
3569 | */ | |
3570 | do { | |
3571 | if (!rw_tryenter(rwlp, rw)) { | |
3572 | /* | |
3573 | * Another thread is renaming in this path. | |
3574 | * Note that if we are a WRITER, we don't have any | |
3575 | * parent_locks held yet. | |
3576 | */ | |
3577 | if (rw == RW_READER && zp->z_id > szp->z_id) { | |
3578 | /* | |
3579 | * Drop our locks and restart | |
3580 | */ | |
3581 | zfs_rename_unlock(&zl); | |
3582 | *zlpp = NULL; | |
3583 | zp = tdzp; | |
428870ff | 3584 | oidp = zp->z_id; |
34dc7c2f BB |
3585 | rwlp = &szp->z_parent_lock; |
3586 | rw = RW_WRITER; | |
3587 | continue; | |
3588 | } else { | |
3589 | /* | |
3590 | * Wait for other thread to drop its locks | |
3591 | */ | |
3592 | rw_enter(rwlp, rw); | |
3593 | } | |
3594 | } | |
3595 | ||
3596 | zl = kmem_alloc(sizeof (*zl), KM_SLEEP); | |
3597 | zl->zl_rwlock = rwlp; | |
3598 | zl->zl_znode = NULL; | |
3599 | zl->zl_next = *zlpp; | |
3600 | *zlpp = zl; | |
3601 | ||
428870ff | 3602 | if (oidp == szp->z_id) /* We're a descendant of szp */ |
2e528b49 | 3603 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3604 | |
428870ff | 3605 | if (oidp == rootid) /* We've hit the top */ |
34dc7c2f BB |
3606 | return (0); |
3607 | ||
3608 | if (rw == RW_READER) { /* i.e. not the first pass */ | |
3558fd73 | 3609 | int error = zfs_zget(ZTOZSB(zp), oidp, &zp); |
34dc7c2f BB |
3610 | if (error) |
3611 | return (error); | |
3612 | zl->zl_znode = zp; | |
3613 | } | |
3558fd73 | 3614 | (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)), |
428870ff | 3615 | &oidp, sizeof (oidp)); |
34dc7c2f BB |
3616 | rwlp = &zp->z_parent_lock; |
3617 | rw = RW_READER; | |
3618 | ||
3619 | } while (zp->z_id != sdzp->z_id); | |
3620 | ||
3621 | return (0); | |
3622 | } | |
3623 | ||
3624 | /* | |
3625 | * Move an entry from the provided source directory to the target | |
3626 | * directory. Change the entry name as indicated. | |
3627 | * | |
3558fd73 | 3628 | * IN: sdip - Source directory containing the "old entry". |
34dc7c2f | 3629 | * snm - Old entry name. |
3558fd73 | 3630 | * tdip - Target directory to contain the "new entry". |
34dc7c2f BB |
3631 | * tnm - New entry name. |
3632 | * cr - credentials of caller. | |
34dc7c2f BB |
3633 | * flags - case flags |
3634 | * | |
d3cc8b15 | 3635 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
3636 | * |
3637 | * Timestamps: | |
3558fd73 | 3638 | * sdip,tdip - ctime|mtime updated |
34dc7c2f BB |
3639 | */ |
3640 | /*ARGSUSED*/ | |
e5c39b95 | 3641 | int |
3558fd73 BB |
3642 | zfs_rename(struct inode *sdip, char *snm, struct inode *tdip, char *tnm, |
3643 | cred_t *cr, int flags) | |
34dc7c2f BB |
3644 | { |
3645 | znode_t *tdzp, *szp, *tzp; | |
3558fd73 | 3646 | znode_t *sdzp = ITOZ(sdip); |
0037b49e | 3647 | zfsvfs_t *zfsvfs = ITOZSB(sdip); |
34dc7c2f | 3648 | zilog_t *zilog; |
34dc7c2f BB |
3649 | zfs_dirlock_t *sdl, *tdl; |
3650 | dmu_tx_t *tx; | |
3651 | zfs_zlock_t *zl; | |
3652 | int cmp, serr, terr; | |
3653 | int error = 0; | |
3654 | int zflg = 0; | |
e8b96c60 | 3655 | boolean_t waited = B_FALSE; |
34dc7c2f | 3656 | |
32dec7bd | 3657 | if (snm == NULL || tnm == NULL) |
3658 | return (SET_ERROR(EINVAL)); | |
3659 | ||
0037b49e | 3660 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 3661 | ZFS_VERIFY_ZP(sdzp); |
0037b49e | 3662 | zilog = zfsvfs->z_log; |
34dc7c2f | 3663 | |
812e91a7 MT |
3664 | tdzp = ITOZ(tdip); |
3665 | ZFS_VERIFY_ZP(tdzp); | |
3666 | ||
3667 | /* | |
3668 | * We check i_sb because snapshots and the ctldir must have different | |
3669 | * super blocks. | |
3670 | */ | |
c0ebc844 | 3671 | if (tdip->i_sb != sdip->i_sb || zfsctl_is_node(tdip)) { |
0037b49e | 3672 | ZFS_EXIT(zfsvfs); |
2e528b49 | 3673 | return (SET_ERROR(EXDEV)); |
34dc7c2f BB |
3674 | } |
3675 | ||
0037b49e | 3676 | if (zfsvfs->z_utf8 && u8_validate(tnm, |
34dc7c2f | 3677 | strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 3678 | ZFS_EXIT(zfsvfs); |
2e528b49 | 3679 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
3680 | } |
3681 | ||
3682 | if (flags & FIGNORECASE) | |
3683 | zflg |= ZCILOOK; | |
3684 | ||
3685 | top: | |
3686 | szp = NULL; | |
3687 | tzp = NULL; | |
3688 | zl = NULL; | |
3689 | ||
3690 | /* | |
3691 | * This is to prevent the creation of links into attribute space | |
3692 | * by renaming a linked file into/outof an attribute directory. | |
3693 | * See the comment in zfs_link() for why this is considered bad. | |
3694 | */ | |
428870ff | 3695 | if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { |
0037b49e | 3696 | ZFS_EXIT(zfsvfs); |
2e528b49 | 3697 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
3698 | } |
3699 | ||
3700 | /* | |
3701 | * Lock source and target directory entries. To prevent deadlock, | |
3702 | * a lock ordering must be defined. We lock the directory with | |
3703 | * the smallest object id first, or if it's a tie, the one with | |
3704 | * the lexically first name. | |
3705 | */ | |
3706 | if (sdzp->z_id < tdzp->z_id) { | |
3707 | cmp = -1; | |
3708 | } else if (sdzp->z_id > tdzp->z_id) { | |
3709 | cmp = 1; | |
3710 | } else { | |
3711 | /* | |
3712 | * First compare the two name arguments without | |
3713 | * considering any case folding. | |
3714 | */ | |
0037b49e | 3715 | int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); |
34dc7c2f BB |
3716 | |
3717 | cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); | |
0037b49e | 3718 | ASSERT(error == 0 || !zfsvfs->z_utf8); |
34dc7c2f BB |
3719 | if (cmp == 0) { |
3720 | /* | |
3721 | * POSIX: "If the old argument and the new argument | |
3722 | * both refer to links to the same existing file, | |
3723 | * the rename() function shall return successfully | |
3724 | * and perform no other action." | |
3725 | */ | |
0037b49e | 3726 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3727 | return (0); |
3728 | } | |
3729 | /* | |
3730 | * If the file system is case-folding, then we may | |
3731 | * have some more checking to do. A case-folding file | |
3732 | * system is either supporting mixed case sensitivity | |
3733 | * access or is completely case-insensitive. Note | |
3734 | * that the file system is always case preserving. | |
3735 | * | |
3736 | * In mixed sensitivity mode case sensitive behavior | |
3737 | * is the default. FIGNORECASE must be used to | |
3738 | * explicitly request case insensitive behavior. | |
3739 | * | |
3740 | * If the source and target names provided differ only | |
3741 | * by case (e.g., a request to rename 'tim' to 'Tim'), | |
3742 | * we will treat this as a special case in the | |
3743 | * case-insensitive mode: as long as the source name | |
3744 | * is an exact match, we will allow this to proceed as | |
3745 | * a name-change request. | |
3746 | */ | |
0037b49e BB |
3747 | if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || |
3748 | (zfsvfs->z_case == ZFS_CASE_MIXED && | |
34dc7c2f | 3749 | flags & FIGNORECASE)) && |
0037b49e | 3750 | u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, |
34dc7c2f BB |
3751 | &error) == 0) { |
3752 | /* | |
3753 | * case preserving rename request, require exact | |
3754 | * name matches | |
3755 | */ | |
3756 | zflg |= ZCIEXACT; | |
3757 | zflg &= ~ZCILOOK; | |
3758 | } | |
3759 | } | |
3760 | ||
428870ff BB |
3761 | /* |
3762 | * If the source and destination directories are the same, we should | |
3763 | * grab the z_name_lock of that directory only once. | |
3764 | */ | |
3765 | if (sdzp == tdzp) { | |
3766 | zflg |= ZHAVELOCK; | |
3767 | rw_enter(&sdzp->z_name_lock, RW_READER); | |
3768 | } | |
3769 | ||
34dc7c2f BB |
3770 | if (cmp < 0) { |
3771 | serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, | |
3772 | ZEXISTS | zflg, NULL, NULL); | |
3773 | terr = zfs_dirent_lock(&tdl, | |
3774 | tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); | |
3775 | } else { | |
3776 | terr = zfs_dirent_lock(&tdl, | |
3777 | tdzp, tnm, &tzp, zflg, NULL, NULL); | |
3778 | serr = zfs_dirent_lock(&sdl, | |
3779 | sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, | |
3780 | NULL, NULL); | |
3781 | } | |
3782 | ||
3783 | if (serr) { | |
3784 | /* | |
3785 | * Source entry invalid or not there. | |
3786 | */ | |
3787 | if (!terr) { | |
3788 | zfs_dirent_unlock(tdl); | |
3789 | if (tzp) | |
3558fd73 | 3790 | iput(ZTOI(tzp)); |
34dc7c2f | 3791 | } |
428870ff BB |
3792 | |
3793 | if (sdzp == tdzp) | |
3794 | rw_exit(&sdzp->z_name_lock); | |
3795 | ||
34dc7c2f BB |
3796 | if (strcmp(snm, "..") == 0) |
3797 | serr = EINVAL; | |
0037b49e | 3798 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3799 | return (serr); |
3800 | } | |
3801 | if (terr) { | |
3802 | zfs_dirent_unlock(sdl); | |
3558fd73 | 3803 | iput(ZTOI(szp)); |
428870ff BB |
3804 | |
3805 | if (sdzp == tdzp) | |
3806 | rw_exit(&sdzp->z_name_lock); | |
3807 | ||
34dc7c2f BB |
3808 | if (strcmp(tnm, "..") == 0) |
3809 | terr = EINVAL; | |
0037b49e | 3810 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3811 | return (terr); |
3812 | } | |
3813 | ||
9c5167d1 NF |
3814 | /* |
3815 | * If we are using project inheritance, means if the directory has | |
3816 | * ZFS_PROJINHERIT set, then its descendant directories will inherit | |
3817 | * not only the project ID, but also the ZFS_PROJINHERIT flag. Under | |
3818 | * such case, we only allow renames into our tree when the project | |
3819 | * IDs are the same. | |
3820 | */ | |
3821 | if (tdzp->z_pflags & ZFS_PROJINHERIT && | |
3822 | tdzp->z_projid != szp->z_projid) { | |
3823 | error = SET_ERROR(EXDEV); | |
3824 | goto out; | |
3825 | } | |
3826 | ||
34dc7c2f BB |
3827 | /* |
3828 | * Must have write access at the source to remove the old entry | |
3829 | * and write access at the target to create the new entry. | |
3830 | * Note that if target and source are the same, this can be | |
3831 | * done in a single check. | |
3832 | */ | |
3833 | ||
149e873a | 3834 | if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))) |
34dc7c2f BB |
3835 | goto out; |
3836 | ||
3558fd73 | 3837 | if (S_ISDIR(ZTOI(szp)->i_mode)) { |
34dc7c2f BB |
3838 | /* |
3839 | * Check to make sure rename is valid. | |
3840 | * Can't do a move like this: /usr/a/b to /usr/a/b/c/d | |
3841 | */ | |
149e873a | 3842 | if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl))) |
34dc7c2f BB |
3843 | goto out; |
3844 | } | |
3845 | ||
3846 | /* | |
3847 | * Does target exist? | |
3848 | */ | |
3849 | if (tzp) { | |
3850 | /* | |
3851 | * Source and target must be the same type. | |
3852 | */ | |
3558fd73 BB |
3853 | if (S_ISDIR(ZTOI(szp)->i_mode)) { |
3854 | if (!S_ISDIR(ZTOI(tzp)->i_mode)) { | |
2e528b49 | 3855 | error = SET_ERROR(ENOTDIR); |
34dc7c2f BB |
3856 | goto out; |
3857 | } | |
3858 | } else { | |
3558fd73 | 3859 | if (S_ISDIR(ZTOI(tzp)->i_mode)) { |
2e528b49 | 3860 | error = SET_ERROR(EISDIR); |
34dc7c2f BB |
3861 | goto out; |
3862 | } | |
3863 | } | |
3864 | /* | |
3865 | * POSIX dictates that when the source and target | |
3866 | * entries refer to the same file object, rename | |
3867 | * must do nothing and exit without error. | |
3868 | */ | |
3869 | if (szp->z_id == tzp->z_id) { | |
3870 | error = 0; | |
3871 | goto out; | |
3872 | } | |
3873 | } | |
3874 | ||
0037b49e | 3875 | tx = dmu_tx_create(zfsvfs->z_os); |
428870ff BB |
3876 | dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); |
3877 | dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); | |
34dc7c2f BB |
3878 | dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); |
3879 | dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); | |
428870ff BB |
3880 | if (sdzp != tdzp) { |
3881 | dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); | |
3882 | zfs_sa_upgrade_txholds(tx, tdzp); | |
3883 | } | |
3884 | if (tzp) { | |
3885 | dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); | |
3886 | zfs_sa_upgrade_txholds(tx, tzp); | |
3887 | } | |
3888 | ||
3889 | zfs_sa_upgrade_txholds(tx, szp); | |
0037b49e | 3890 | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); |
0735ecb3 | 3891 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
3892 | if (error) { |
3893 | if (zl != NULL) | |
3894 | zfs_rename_unlock(&zl); | |
3895 | zfs_dirent_unlock(sdl); | |
3896 | zfs_dirent_unlock(tdl); | |
428870ff BB |
3897 | |
3898 | if (sdzp == tdzp) | |
3899 | rw_exit(&sdzp->z_name_lock); | |
3900 | ||
fb5f0bc8 | 3901 | if (error == ERESTART) { |
e8b96c60 | 3902 | waited = B_TRUE; |
34dc7c2f BB |
3903 | dmu_tx_wait(tx); |
3904 | dmu_tx_abort(tx); | |
ea7e86d8 BB |
3905 | iput(ZTOI(szp)); |
3906 | if (tzp) | |
3907 | iput(ZTOI(tzp)); | |
34dc7c2f BB |
3908 | goto top; |
3909 | } | |
3910 | dmu_tx_abort(tx); | |
ea7e86d8 BB |
3911 | iput(ZTOI(szp)); |
3912 | if (tzp) | |
3913 | iput(ZTOI(tzp)); | |
0037b49e | 3914 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3915 | return (error); |
3916 | } | |
3917 | ||
3918 | if (tzp) /* Attempt to remove the existing target */ | |
3919 | error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); | |
3920 | ||
3921 | if (error == 0) { | |
3922 | error = zfs_link_create(tdl, szp, tx, ZRENAMING); | |
3923 | if (error == 0) { | |
428870ff | 3924 | szp->z_pflags |= ZFS_AV_MODIFIED; |
9c5167d1 NF |
3925 | if (tdzp->z_pflags & ZFS_PROJINHERIT) |
3926 | szp->z_pflags |= ZFS_PROJINHERIT; | |
34dc7c2f | 3927 | |
0037b49e | 3928 | error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), |
428870ff | 3929 | (void *)&szp->z_pflags, sizeof (uint64_t), tx); |
c99c9001 | 3930 | ASSERT0(error); |
34dc7c2f | 3931 | |
428870ff BB |
3932 | error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); |
3933 | if (error == 0) { | |
3934 | zfs_log_rename(zilog, tx, TX_RENAME | | |
572e2857 BB |
3935 | (flags & FIGNORECASE ? TX_CI : 0), sdzp, |
3936 | sdl->dl_name, tdzp, tdl->dl_name, szp); | |
428870ff BB |
3937 | } else { |
3938 | /* | |
3939 | * At this point, we have successfully created | |
3940 | * the target name, but have failed to remove | |
3941 | * the source name. Since the create was done | |
3942 | * with the ZRENAMING flag, there are | |
3943 | * complications; for one, the link count is | |
3944 | * wrong. The easiest way to deal with this | |
3945 | * is to remove the newly created target, and | |
3946 | * return the original error. This must | |
3947 | * succeed; fortunately, it is very unlikely to | |
3948 | * fail, since we just created it. | |
3949 | */ | |
3950 | VERIFY3U(zfs_link_destroy(tdl, szp, tx, | |
3951 | ZRENAMING, NULL), ==, 0); | |
3952 | } | |
34dc7c2f BB |
3953 | } |
3954 | } | |
3955 | ||
3956 | dmu_tx_commit(tx); | |
3957 | out: | |
3958 | if (zl != NULL) | |
3959 | zfs_rename_unlock(&zl); | |
3960 | ||
3961 | zfs_dirent_unlock(sdl); | |
3962 | zfs_dirent_unlock(tdl); | |
3963 | ||
960e08fe | 3964 | zfs_inode_update(sdzp); |
428870ff BB |
3965 | if (sdzp == tdzp) |
3966 | rw_exit(&sdzp->z_name_lock); | |
3967 | ||
960e08fe BB |
3968 | if (sdzp != tdzp) |
3969 | zfs_inode_update(tdzp); | |
428870ff | 3970 | |
960e08fe | 3971 | zfs_inode_update(szp); |
3558fd73 | 3972 | iput(ZTOI(szp)); |
960e08fe BB |
3973 | if (tzp) { |
3974 | zfs_inode_update(tzp); | |
3558fd73 | 3975 | iput(ZTOI(tzp)); |
960e08fe | 3976 | } |
34dc7c2f | 3977 | |
0037b49e | 3978 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 3979 | zil_commit(zilog, 0); |
428870ff | 3980 | |
0037b49e | 3981 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
3982 | return (error); |
3983 | } | |
3984 | ||
3985 | /* | |
3986 | * Insert the indicated symbolic reference entry into the directory. | |
3987 | * | |
3558fd73 | 3988 | * IN: dip - Directory to contain new symbolic link. |
34dc7c2f BB |
3989 | * link - Name for new symlink entry. |
3990 | * vap - Attributes of new entry. | |
3991 | * target - Target path of new symlink. | |
3558fd73 | 3992 | * |
34dc7c2f | 3993 | * cr - credentials of caller. |
34dc7c2f BB |
3994 | * flags - case flags |
3995 | * | |
d3cc8b15 | 3996 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
3997 | * |
3998 | * Timestamps: | |
3558fd73 | 3999 | * dip - ctime|mtime updated |
34dc7c2f BB |
4000 | */ |
4001 | /*ARGSUSED*/ | |
e5c39b95 | 4002 | int |
3558fd73 BB |
4003 | zfs_symlink(struct inode *dip, char *name, vattr_t *vap, char *link, |
4004 | struct inode **ipp, cred_t *cr, int flags) | |
34dc7c2f | 4005 | { |
3558fd73 | 4006 | znode_t *zp, *dzp = ITOZ(dip); |
34dc7c2f BB |
4007 | zfs_dirlock_t *dl; |
4008 | dmu_tx_t *tx; | |
0037b49e | 4009 | zfsvfs_t *zfsvfs = ITOZSB(dip); |
34dc7c2f | 4010 | zilog_t *zilog; |
428870ff | 4011 | uint64_t len = strlen(link); |
34dc7c2f BB |
4012 | int error; |
4013 | int zflg = ZNEW; | |
9babb374 BB |
4014 | zfs_acl_ids_t acl_ids; |
4015 | boolean_t fuid_dirtied; | |
428870ff | 4016 | uint64_t txtype = TX_SYMLINK; |
e8b96c60 | 4017 | boolean_t waited = B_FALSE; |
34dc7c2f | 4018 | |
3558fd73 | 4019 | ASSERT(S_ISLNK(vap->va_mode)); |
34dc7c2f | 4020 | |
32dec7bd | 4021 | if (name == NULL) |
4022 | return (SET_ERROR(EINVAL)); | |
4023 | ||
0037b49e | 4024 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 4025 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 4026 | zilog = zfsvfs->z_log; |
34dc7c2f | 4027 | |
0037b49e | 4028 | if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), |
34dc7c2f | 4029 | NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 4030 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4031 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
4032 | } |
4033 | if (flags & FIGNORECASE) | |
4034 | zflg |= ZCILOOK; | |
34dc7c2f BB |
4035 | |
4036 | if (len > MAXPATHLEN) { | |
0037b49e | 4037 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4038 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
4039 | } |
4040 | ||
428870ff BB |
4041 | if ((error = zfs_acl_ids_create(dzp, 0, |
4042 | vap, cr, NULL, &acl_ids)) != 0) { | |
0037b49e | 4043 | ZFS_EXIT(zfsvfs); |
428870ff BB |
4044 | return (error); |
4045 | } | |
4046 | top: | |
3558fd73 BB |
4047 | *ipp = NULL; |
4048 | ||
34dc7c2f BB |
4049 | /* |
4050 | * Attempt to lock directory; fail if entry already exists. | |
4051 | */ | |
4052 | error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); | |
4053 | if (error) { | |
428870ff | 4054 | zfs_acl_ids_free(&acl_ids); |
0037b49e | 4055 | ZFS_EXIT(zfsvfs); |
428870ff BB |
4056 | return (error); |
4057 | } | |
4058 | ||
149e873a | 4059 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
428870ff BB |
4060 | zfs_acl_ids_free(&acl_ids); |
4061 | zfs_dirent_unlock(dl); | |
0037b49e | 4062 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4063 | return (error); |
4064 | } | |
4065 | ||
9c5167d1 | 4066 | if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, ZFS_DEFAULT_PROJID)) { |
9babb374 BB |
4067 | zfs_acl_ids_free(&acl_ids); |
4068 | zfs_dirent_unlock(dl); | |
0037b49e | 4069 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4070 | return (SET_ERROR(EDQUOT)); |
9babb374 | 4071 | } |
0037b49e BB |
4072 | tx = dmu_tx_create(zfsvfs->z_os); |
4073 | fuid_dirtied = zfsvfs->z_fuid_dirty; | |
34dc7c2f | 4074 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); |
34dc7c2f | 4075 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
428870ff BB |
4076 | dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + |
4077 | ZFS_SA_BASE_ATTR_SIZE + len); | |
4078 | dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); | |
0037b49e | 4079 | if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { |
428870ff BB |
4080 | dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, |
4081 | acl_ids.z_aclp->z_acl_bytes); | |
4082 | } | |
9babb374 | 4083 | if (fuid_dirtied) |
0037b49e | 4084 | zfs_fuid_txhold(zfsvfs, tx); |
0735ecb3 | 4085 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
4086 | if (error) { |
4087 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 4088 | if (error == ERESTART) { |
e8b96c60 | 4089 | waited = B_TRUE; |
34dc7c2f BB |
4090 | dmu_tx_wait(tx); |
4091 | dmu_tx_abort(tx); | |
4092 | goto top; | |
4093 | } | |
428870ff | 4094 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f | 4095 | dmu_tx_abort(tx); |
0037b49e | 4096 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4097 | return (error); |
4098 | } | |
4099 | ||
34dc7c2f BB |
4100 | /* |
4101 | * Create a new object for the symlink. | |
428870ff | 4102 | * for version 4 ZPL datsets the symlink will be an SA attribute |
34dc7c2f | 4103 | */ |
428870ff | 4104 | zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); |
9babb374 | 4105 | |
428870ff | 4106 | if (fuid_dirtied) |
0037b49e | 4107 | zfs_fuid_sync(zfsvfs, tx); |
34dc7c2f | 4108 | |
572e2857 | 4109 | mutex_enter(&zp->z_lock); |
428870ff | 4110 | if (zp->z_is_sa) |
0037b49e | 4111 | error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), |
428870ff BB |
4112 | link, len, tx); |
4113 | else | |
4114 | zfs_sa_symlink(zp, link, len, tx); | |
572e2857 | 4115 | mutex_exit(&zp->z_lock); |
34dc7c2f | 4116 | |
428870ff | 4117 | zp->z_size = len; |
0037b49e | 4118 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), |
428870ff | 4119 | &zp->z_size, sizeof (zp->z_size), tx); |
34dc7c2f BB |
4120 | /* |
4121 | * Insert the new object into the directory. | |
4122 | */ | |
4f301661 | 4123 | (void) zfs_link_create(dl, zp, tx, ZNEW); |
9babb374 | 4124 | |
4f301661 TH |
4125 | if (flags & FIGNORECASE) |
4126 | txtype |= TX_CI; | |
4127 | zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); | |
4128 | ||
4129 | zfs_inode_update(dzp); | |
4130 | zfs_inode_update(zp); | |
960e08fe | 4131 | |
9babb374 | 4132 | zfs_acl_ids_free(&acl_ids); |
34dc7c2f BB |
4133 | |
4134 | dmu_tx_commit(tx); | |
4135 | ||
4136 | zfs_dirent_unlock(dl); | |
4137 | ||
4f301661 | 4138 | *ipp = ZTOI(zp); |
34dc7c2f | 4139 | |
4f301661 TH |
4140 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
4141 | zil_commit(zilog, 0); | |
428870ff | 4142 | |
0037b49e | 4143 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4144 | return (error); |
4145 | } | |
4146 | ||
4147 | /* | |
4148 | * Return, in the buffer contained in the provided uio structure, | |
3558fd73 | 4149 | * the symbolic path referred to by ip. |
34dc7c2f | 4150 | * |
8b4f9a2d BB |
4151 | * IN: ip - inode of symbolic link |
4152 | * uio - structure to contain the link path. | |
4153 | * cr - credentials of caller. | |
34dc7c2f BB |
4154 | * |
4155 | * RETURN: 0 if success | |
4156 | * error code if failure | |
4157 | * | |
4158 | * Timestamps: | |
3558fd73 | 4159 | * ip - atime updated |
34dc7c2f BB |
4160 | */ |
4161 | /* ARGSUSED */ | |
e5c39b95 | 4162 | int |
8b4f9a2d | 4163 | zfs_readlink(struct inode *ip, uio_t *uio, cred_t *cr) |
34dc7c2f | 4164 | { |
3558fd73 | 4165 | znode_t *zp = ITOZ(ip); |
0037b49e | 4166 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
4167 | int error; |
4168 | ||
0037b49e | 4169 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
4170 | ZFS_VERIFY_ZP(zp); |
4171 | ||
572e2857 | 4172 | mutex_enter(&zp->z_lock); |
428870ff | 4173 | if (zp->z_is_sa) |
8b4f9a2d | 4174 | error = sa_lookup_uio(zp->z_sa_hdl, |
0037b49e | 4175 | SA_ZPL_SYMLINK(zfsvfs), uio); |
428870ff | 4176 | else |
8b4f9a2d | 4177 | error = zfs_sa_readlink(zp, uio); |
572e2857 | 4178 | mutex_exit(&zp->z_lock); |
34dc7c2f | 4179 | |
0037b49e | 4180 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4181 | return (error); |
4182 | } | |
4183 | ||
4184 | /* | |
3558fd73 | 4185 | * Insert a new entry into directory tdip referencing sip. |
34dc7c2f | 4186 | * |
3558fd73 BB |
4187 | * IN: tdip - Directory to contain new entry. |
4188 | * sip - inode of new entry. | |
34dc7c2f BB |
4189 | * name - name of new entry. |
4190 | * cr - credentials of caller. | |
34dc7c2f BB |
4191 | * |
4192 | * RETURN: 0 if success | |
4193 | * error code if failure | |
4194 | * | |
4195 | * Timestamps: | |
3558fd73 BB |
4196 | * tdip - ctime|mtime updated |
4197 | * sip - ctime updated | |
34dc7c2f BB |
4198 | */ |
4199 | /* ARGSUSED */ | |
e5c39b95 | 4200 | int |
da5e151f BB |
4201 | zfs_link(struct inode *tdip, struct inode *sip, char *name, cred_t *cr, |
4202 | int flags) | |
34dc7c2f | 4203 | { |
3558fd73 | 4204 | znode_t *dzp = ITOZ(tdip); |
34dc7c2f | 4205 | znode_t *tzp, *szp; |
0037b49e | 4206 | zfsvfs_t *zfsvfs = ITOZSB(tdip); |
34dc7c2f BB |
4207 | zilog_t *zilog; |
4208 | zfs_dirlock_t *dl; | |
4209 | dmu_tx_t *tx; | |
34dc7c2f BB |
4210 | int error; |
4211 | int zf = ZNEW; | |
428870ff | 4212 | uint64_t parent; |
572e2857 | 4213 | uid_t owner; |
e8b96c60 | 4214 | boolean_t waited = B_FALSE; |
ace1eae8 CC |
4215 | boolean_t is_tmpfile = 0; |
4216 | uint64_t txg; | |
4217 | #ifdef HAVE_TMPFILE | |
4218 | is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE)); | |
4219 | #endif | |
3558fd73 | 4220 | ASSERT(S_ISDIR(tdip->i_mode)); |
34dc7c2f | 4221 | |
32dec7bd | 4222 | if (name == NULL) |
4223 | return (SET_ERROR(EINVAL)); | |
4224 | ||
0037b49e | 4225 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 4226 | ZFS_VERIFY_ZP(dzp); |
0037b49e | 4227 | zilog = zfsvfs->z_log; |
34dc7c2f | 4228 | |
428870ff BB |
4229 | /* |
4230 | * POSIX dictates that we return EPERM here. | |
4231 | * Better choices include ENOTSUP or EISDIR. | |
4232 | */ | |
3558fd73 | 4233 | if (S_ISDIR(sip->i_mode)) { |
0037b49e | 4234 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4235 | return (SET_ERROR(EPERM)); |
428870ff BB |
4236 | } |
4237 | ||
812e91a7 MT |
4238 | szp = ITOZ(sip); |
4239 | ZFS_VERIFY_ZP(szp); | |
4240 | ||
9c5167d1 NF |
4241 | /* |
4242 | * If we are using project inheritance, means if the directory has | |
4243 | * ZFS_PROJINHERIT set, then its descendant directories will inherit | |
4244 | * not only the project ID, but also the ZFS_PROJINHERIT flag. Under | |
4245 | * such case, we only allow hard link creation in our tree when the | |
4246 | * project IDs are the same. | |
4247 | */ | |
4248 | if (dzp->z_pflags & ZFS_PROJINHERIT && dzp->z_projid != szp->z_projid) { | |
4249 | ZFS_EXIT(zfsvfs); | |
4250 | return (SET_ERROR(EXDEV)); | |
4251 | } | |
4252 | ||
812e91a7 MT |
4253 | /* |
4254 | * We check i_sb because snapshots and the ctldir must have different | |
4255 | * super blocks. | |
4256 | */ | |
c0ebc844 | 4257 | if (sip->i_sb != tdip->i_sb || zfsctl_is_node(sip)) { |
0037b49e | 4258 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4259 | return (SET_ERROR(EXDEV)); |
34dc7c2f | 4260 | } |
428870ff | 4261 | |
428870ff BB |
4262 | /* Prevent links to .zfs/shares files */ |
4263 | ||
0037b49e | 4264 | if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), |
428870ff | 4265 | &parent, sizeof (uint64_t))) != 0) { |
0037b49e | 4266 | ZFS_EXIT(zfsvfs); |
428870ff BB |
4267 | return (error); |
4268 | } | |
0037b49e BB |
4269 | if (parent == zfsvfs->z_shares_dir) { |
4270 | ZFS_EXIT(zfsvfs); | |
2e528b49 | 4271 | return (SET_ERROR(EPERM)); |
428870ff BB |
4272 | } |
4273 | ||
0037b49e | 4274 | if (zfsvfs->z_utf8 && u8_validate(name, |
34dc7c2f | 4275 | strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { |
0037b49e | 4276 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4277 | return (SET_ERROR(EILSEQ)); |
34dc7c2f BB |
4278 | } |
4279 | if (flags & FIGNORECASE) | |
4280 | zf |= ZCILOOK; | |
4281 | ||
34dc7c2f BB |
4282 | /* |
4283 | * We do not support links between attributes and non-attributes | |
4284 | * because of the potential security risk of creating links | |
4285 | * into "normal" file space in order to circumvent restrictions | |
4286 | * imposed in attribute space. | |
4287 | */ | |
428870ff | 4288 | if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { |
0037b49e | 4289 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4290 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
4291 | } |
4292 | ||
0037b49e BB |
4293 | owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid), |
4294 | cr, ZFS_OWNER); | |
572e2857 | 4295 | if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) { |
0037b49e | 4296 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4297 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
4298 | } |
4299 | ||
149e873a | 4300 | if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { |
0037b49e | 4301 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4302 | return (error); |
4303 | } | |
4304 | ||
428870ff | 4305 | top: |
34dc7c2f BB |
4306 | /* |
4307 | * Attempt to lock directory; fail if entry already exists. | |
4308 | */ | |
4309 | error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); | |
4310 | if (error) { | |
0037b49e | 4311 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4312 | return (error); |
4313 | } | |
4314 | ||
0037b49e | 4315 | tx = dmu_tx_create(zfsvfs->z_os); |
428870ff | 4316 | dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); |
34dc7c2f | 4317 | dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); |
ace1eae8 | 4318 | if (is_tmpfile) |
0037b49e | 4319 | dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); |
ace1eae8 | 4320 | |
428870ff BB |
4321 | zfs_sa_upgrade_txholds(tx, szp); |
4322 | zfs_sa_upgrade_txholds(tx, dzp); | |
0735ecb3 | 4323 | error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); |
34dc7c2f BB |
4324 | if (error) { |
4325 | zfs_dirent_unlock(dl); | |
fb5f0bc8 | 4326 | if (error == ERESTART) { |
e8b96c60 | 4327 | waited = B_TRUE; |
34dc7c2f BB |
4328 | dmu_tx_wait(tx); |
4329 | dmu_tx_abort(tx); | |
4330 | goto top; | |
4331 | } | |
4332 | dmu_tx_abort(tx); | |
0037b49e | 4333 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4334 | return (error); |
4335 | } | |
ace1eae8 CC |
4336 | /* unmark z_unlinked so zfs_link_create will not reject */ |
4337 | if (is_tmpfile) | |
4338 | szp->z_unlinked = 0; | |
34dc7c2f BB |
4339 | error = zfs_link_create(dl, szp, tx, 0); |
4340 | ||
4341 | if (error == 0) { | |
4342 | uint64_t txtype = TX_LINK; | |
ace1eae8 CC |
4343 | /* |
4344 | * tmpfile is created to be in z_unlinkedobj, so remove it. | |
4345 | * Also, we don't log in ZIL, be cause all previous file | |
4346 | * operation on the tmpfile are ignored by ZIL. Instead we | |
4347 | * always wait for txg to sync to make sure all previous | |
4348 | * operation are sync safe. | |
4349 | */ | |
4350 | if (is_tmpfile) { | |
0037b49e BB |
4351 | VERIFY(zap_remove_int(zfsvfs->z_os, |
4352 | zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0); | |
ace1eae8 CC |
4353 | } else { |
4354 | if (flags & FIGNORECASE) | |
4355 | txtype |= TX_CI; | |
4356 | zfs_log_link(zilog, tx, txtype, dzp, szp, name); | |
4357 | } | |
4358 | } else if (is_tmpfile) { | |
4359 | /* restore z_unlinked since when linking failed */ | |
4360 | szp->z_unlinked = 1; | |
34dc7c2f | 4361 | } |
ace1eae8 | 4362 | txg = dmu_tx_get_txg(tx); |
34dc7c2f BB |
4363 | dmu_tx_commit(tx); |
4364 | ||
4365 | zfs_dirent_unlock(dl); | |
4366 | ||
0037b49e | 4367 | if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 4368 | zil_commit(zilog, 0); |
428870ff | 4369 | |
ace1eae8 | 4370 | if (is_tmpfile) |
0037b49e | 4371 | txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg); |
ace1eae8 | 4372 | |
960e08fe BB |
4373 | zfs_inode_update(dzp); |
4374 | zfs_inode_update(szp); | |
0037b49e | 4375 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4376 | return (error); |
4377 | } | |
4378 | ||
3c0e5c0f | 4379 | static void |
119a394a | 4380 | zfs_putpage_commit_cb(void *arg) |
3c0e5c0f BB |
4381 | { |
4382 | struct page *pp = arg; | |
4383 | ||
119a394a | 4384 | ClearPageError(pp); |
3c0e5c0f BB |
4385 | end_page_writeback(pp); |
4386 | } | |
4387 | ||
34dc7c2f | 4388 | /* |
3c0e5c0f BB |
4389 | * Push a page out to disk, once the page is on stable storage the |
4390 | * registered commit callback will be run as notification of completion. | |
34dc7c2f | 4391 | * |
3c0e5c0f BB |
4392 | * IN: ip - page mapped for inode. |
4393 | * pp - page to push (page is locked) | |
4394 | * wbc - writeback control data | |
34dc7c2f BB |
4395 | * |
4396 | * RETURN: 0 if success | |
4397 | * error code if failure | |
4398 | * | |
3c0e5c0f BB |
4399 | * Timestamps: |
4400 | * ip - ctime|mtime updated | |
34dc7c2f BB |
4401 | */ |
4402 | /* ARGSUSED */ | |
3c0e5c0f BB |
4403 | int |
4404 | zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc) | |
34dc7c2f | 4405 | { |
3c0e5c0f | 4406 | znode_t *zp = ITOZ(ip); |
0037b49e | 4407 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
3c0e5c0f BB |
4408 | loff_t offset; |
4409 | loff_t pgoff; | |
4c837f0d BB |
4410 | unsigned int pglen; |
4411 | rl_t *rl; | |
3c0e5c0f BB |
4412 | dmu_tx_t *tx; |
4413 | caddr_t va; | |
4414 | int err = 0; | |
4415 | uint64_t mtime[2], ctime[2]; | |
4416 | sa_bulk_attr_t bulk[3]; | |
4417 | int cnt = 0; | |
21a96fb6 | 4418 | struct address_space *mapping; |
3c0e5c0f | 4419 | |
0037b49e | 4420 | ZFS_ENTER(zfsvfs); |
4c837f0d | 4421 | ZFS_VERIFY_ZP(zp); |
d164b209 | 4422 | |
3c0e5c0f BB |
4423 | ASSERT(PageLocked(pp)); |
4424 | ||
d1d7e268 MK |
4425 | pgoff = page_offset(pp); /* Page byte-offset in file */ |
4426 | offset = i_size_read(ip); /* File length in bytes */ | |
8b1899d3 BB |
4427 | pglen = MIN(PAGE_SIZE, /* Page length in bytes */ |
4428 | P2ROUNDUP(offset, PAGE_SIZE)-pgoff); | |
3c0e5c0f BB |
4429 | |
4430 | /* Page is beyond end of file */ | |
4431 | if (pgoff >= offset) { | |
4432 | unlock_page(pp); | |
0037b49e | 4433 | ZFS_EXIT(zfsvfs); |
3c0e5c0f BB |
4434 | return (0); |
4435 | } | |
4436 | ||
4437 | /* Truncate page length to end of file */ | |
4438 | if (pgoff + pglen > offset) | |
4439 | pglen = offset - pgoff; | |
4440 | ||
4441 | #if 0 | |
34dc7c2f | 4442 | /* |
3c0e5c0f BB |
4443 | * FIXME: Allow mmap writes past its quota. The correct fix |
4444 | * is to register a page_mkwrite() handler to count the page | |
4445 | * against its quota when it is about to be dirtied. | |
34dc7c2f | 4446 | */ |
9c5167d1 NF |
4447 | if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, |
4448 | KUID_TO_SUID(ip->i_uid)) || | |
4449 | zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, | |
4450 | KGID_TO_SGID(ip->i_gid)) || | |
4451 | (zp->z_projid != ZFS_DEFAULT_PROJID && | |
4452 | zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT, | |
4453 | zp->z_projid))) { | |
9babb374 | 4454 | err = EDQUOT; |
9babb374 | 4455 | } |
3c0e5c0f BB |
4456 | #endif |
4457 | ||
d958324f BB |
4458 | /* |
4459 | * The ordering here is critical and must adhere to the following | |
4460 | * rules in order to avoid deadlocking in either zfs_read() or | |
4461 | * zfs_free_range() due to a lock inversion. | |
4462 | * | |
4463 | * 1) The page must be unlocked prior to acquiring the range lock. | |
4464 | * This is critical because zfs_read() calls find_lock_page() | |
4465 | * which may block on the page lock while holding the range lock. | |
4466 | * | |
4467 | * 2) Before setting or clearing write back on a page the range lock | |
4468 | * must be held in order to prevent a lock inversion with the | |
4469 | * zfs_free_range() function. | |
21a96fb6 CC |
4470 | * |
4471 | * This presents a problem because upon entering this function the | |
4472 | * page lock is already held. To safely acquire the range lock the | |
4473 | * page lock must be dropped. This creates a window where another | |
4474 | * process could truncate, invalidate, dirty, or write out the page. | |
4475 | * | |
4476 | * Therefore, after successfully reacquiring the range and page locks | |
4477 | * the current page state is checked. In the common case everything | |
4478 | * will be as is expected and it can be written out. However, if | |
4479 | * the page state has changed it must be handled accordingly. | |
d958324f | 4480 | */ |
21a96fb6 CC |
4481 | mapping = pp->mapping; |
4482 | redirty_page_for_writepage(wbc, pp); | |
d958324f | 4483 | unlock_page(pp); |
21a96fb6 | 4484 | |
d88895a0 | 4485 | rl = zfs_range_lock(&zp->z_range_lock, pgoff, pglen, RL_WRITER); |
21a96fb6 CC |
4486 | lock_page(pp); |
4487 | ||
4488 | /* Page mapping changed or it was no longer dirty, we're done */ | |
4489 | if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) { | |
4490 | unlock_page(pp); | |
4491 | zfs_range_unlock(rl); | |
0037b49e | 4492 | ZFS_EXIT(zfsvfs); |
21a96fb6 CC |
4493 | return (0); |
4494 | } | |
4495 | ||
4496 | /* Another process started write block if required */ | |
4497 | if (PageWriteback(pp)) { | |
4498 | unlock_page(pp); | |
4499 | zfs_range_unlock(rl); | |
4500 | ||
4501 | if (wbc->sync_mode != WB_SYNC_NONE) | |
4502 | wait_on_page_writeback(pp); | |
4503 | ||
0037b49e | 4504 | ZFS_EXIT(zfsvfs); |
21a96fb6 CC |
4505 | return (0); |
4506 | } | |
4507 | ||
4508 | /* Clear the dirty flag the required locks are held */ | |
4509 | if (!clear_page_dirty_for_io(pp)) { | |
4510 | unlock_page(pp); | |
4511 | zfs_range_unlock(rl); | |
0037b49e | 4512 | ZFS_EXIT(zfsvfs); |
21a96fb6 CC |
4513 | return (0); |
4514 | } | |
4515 | ||
4516 | /* | |
4517 | * Counterpart for redirty_page_for_writepage() above. This page | |
4518 | * was in fact not skipped and should not be counted as if it were. | |
4519 | */ | |
4520 | wbc->pages_skipped--; | |
3c0e5c0f | 4521 | set_page_writeback(pp); |
21a96fb6 | 4522 | unlock_page(pp); |
3c0e5c0f | 4523 | |
0037b49e | 4524 | tx = dmu_tx_create(zfsvfs->z_os); |
3c0e5c0f | 4525 | dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen); |
428870ff BB |
4526 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
4527 | zfs_sa_upgrade_txholds(tx, zp); | |
d958324f | 4528 | |
fb5f0bc8 | 4529 | err = dmu_tx_assign(tx, TXG_NOWAIT); |
34dc7c2f | 4530 | if (err != 0) { |
3c0e5c0f | 4531 | if (err == ERESTART) |
34dc7c2f | 4532 | dmu_tx_wait(tx); |
3c0e5c0f | 4533 | |
34dc7c2f | 4534 | dmu_tx_abort(tx); |
119a394a ED |
4535 | __set_page_dirty_nobuffers(pp); |
4536 | ClearPageError(pp); | |
4537 | end_page_writeback(pp); | |
4c837f0d | 4538 | zfs_range_unlock(rl); |
0037b49e | 4539 | ZFS_EXIT(zfsvfs); |
3c0e5c0f | 4540 | return (err); |
34dc7c2f BB |
4541 | } |
4542 | ||
dde471ef | 4543 | va = kmap(pp); |
8b1899d3 | 4544 | ASSERT3U(pglen, <=, PAGE_SIZE); |
0037b49e | 4545 | dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx); |
dde471ef | 4546 | kunmap(pp); |
34dc7c2f | 4547 | |
0037b49e BB |
4548 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); |
4549 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); | |
4550 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL, | |
4551 | &zp->z_pflags, 8); | |
428870ff | 4552 | |
d3aa3ea9 BB |
4553 | /* Preserve the mtime and ctime provided by the inode */ |
4554 | ZFS_TIME_ENCODE(&ip->i_mtime, mtime); | |
4555 | ZFS_TIME_ENCODE(&ip->i_ctime, ctime); | |
4556 | zp->z_atime_dirty = 0; | |
4557 | zp->z_seq++; | |
4558 | ||
4559 | err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx); | |
4560 | ||
0037b49e | 4561 | zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0, |
119a394a | 4562 | zfs_putpage_commit_cb, pp); |
45d1cae3 | 4563 | dmu_tx_commit(tx); |
d3aa3ea9 | 4564 | |
4c837f0d | 4565 | zfs_range_unlock(rl); |
34dc7c2f | 4566 | |
119a394a ED |
4567 | if (wbc->sync_mode != WB_SYNC_NONE) { |
4568 | /* | |
4569 | * Note that this is rarely called under writepages(), because | |
4570 | * writepages() normally handles the entire commit for | |
4571 | * performance reasons. | |
4572 | */ | |
0037b49e | 4573 | zil_commit(zfsvfs->z_log, zp->z_id); |
2b286136 | 4574 | } |
3c0e5c0f | 4575 | |
0037b49e | 4576 | ZFS_EXIT(zfsvfs); |
3c0e5c0f | 4577 | return (err); |
34dc7c2f BB |
4578 | } |
4579 | ||
8780c539 BB |
4580 | /* |
4581 | * Update the system attributes when the inode has been dirtied. For the | |
023699cd | 4582 | * moment we only update the mode, atime, mtime, and ctime. |
8780c539 BB |
4583 | */ |
4584 | int | |
4585 | zfs_dirty_inode(struct inode *ip, int flags) | |
4586 | { | |
4587 | znode_t *zp = ITOZ(ip); | |
0037b49e | 4588 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
8780c539 | 4589 | dmu_tx_t *tx; |
023699cd MM |
4590 | uint64_t mode, atime[2], mtime[2], ctime[2]; |
4591 | sa_bulk_attr_t bulk[4]; | |
704cd075 | 4592 | int error = 0; |
8780c539 BB |
4593 | int cnt = 0; |
4594 | ||
0037b49e | 4595 | if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os)) |
c944be5d BB |
4596 | return (0); |
4597 | ||
0037b49e | 4598 | ZFS_ENTER(zfsvfs); |
8780c539 BB |
4599 | ZFS_VERIFY_ZP(zp); |
4600 | ||
704cd075 CC |
4601 | #ifdef I_DIRTY_TIME |
4602 | /* | |
4603 | * This is the lazytime semantic indroduced in Linux 4.0 | |
4604 | * This flag will only be called from update_time when lazytime is set. | |
4605 | * (Note, I_DIRTY_SYNC will also set if not lazytime) | |
4606 | * Fortunately mtime and ctime are managed within ZFS itself, so we | |
4607 | * only need to dirty atime. | |
4608 | */ | |
4609 | if (flags == I_DIRTY_TIME) { | |
4610 | zp->z_atime_dirty = 1; | |
4611 | goto out; | |
4612 | } | |
4613 | #endif | |
4614 | ||
0037b49e | 4615 | tx = dmu_tx_create(zfsvfs->z_os); |
8780c539 BB |
4616 | |
4617 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); | |
4618 | zfs_sa_upgrade_txholds(tx, zp); | |
4619 | ||
4620 | error = dmu_tx_assign(tx, TXG_WAIT); | |
4621 | if (error) { | |
4622 | dmu_tx_abort(tx); | |
4623 | goto out; | |
4624 | } | |
4625 | ||
4626 | mutex_enter(&zp->z_lock); | |
704cd075 CC |
4627 | zp->z_atime_dirty = 0; |
4628 | ||
0037b49e BB |
4629 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); |
4630 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16); | |
4631 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); | |
4632 | SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); | |
8780c539 | 4633 | |
023699cd | 4634 | /* Preserve the mode, mtime and ctime provided by the inode */ |
8780c539 BB |
4635 | ZFS_TIME_ENCODE(&ip->i_atime, atime); |
4636 | ZFS_TIME_ENCODE(&ip->i_mtime, mtime); | |
4637 | ZFS_TIME_ENCODE(&ip->i_ctime, ctime); | |
023699cd MM |
4638 | mode = ip->i_mode; |
4639 | ||
4640 | zp->z_mode = mode; | |
8780c539 BB |
4641 | |
4642 | error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx); | |
4643 | mutex_exit(&zp->z_lock); | |
4644 | ||
4645 | dmu_tx_commit(tx); | |
4646 | out: | |
0037b49e | 4647 | ZFS_EXIT(zfsvfs); |
8780c539 BB |
4648 | return (error); |
4649 | } | |
8780c539 | 4650 | |
34dc7c2f BB |
4651 | /*ARGSUSED*/ |
4652 | void | |
c0d35759 | 4653 | zfs_inactive(struct inode *ip) |
34dc7c2f | 4654 | { |
c0d35759 | 4655 | znode_t *zp = ITOZ(ip); |
0037b49e | 4656 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
0df9673f | 4657 | uint64_t atime[2]; |
34dc7c2f | 4658 | int error; |
cafbd2ac | 4659 | int need_unlock = 0; |
34dc7c2f | 4660 | |
cafbd2ac | 4661 | /* Only read lock if we haven't already write locked, e.g. rollback */ |
0037b49e | 4662 | if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) { |
cafbd2ac | 4663 | need_unlock = 1; |
0037b49e | 4664 | rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); |
cafbd2ac | 4665 | } |
c0d35759 | 4666 | if (zp->z_sa_hdl == NULL) { |
cafbd2ac | 4667 | if (need_unlock) |
0037b49e | 4668 | rw_exit(&zfsvfs->z_teardown_inactive_lock); |
c0d35759 | 4669 | return; |
34dc7c2f BB |
4670 | } |
4671 | ||
4672 | if (zp->z_atime_dirty && zp->z_unlinked == 0) { | |
0037b49e | 4673 | dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); |
34dc7c2f | 4674 | |
428870ff BB |
4675 | dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); |
4676 | zfs_sa_upgrade_txholds(tx, zp); | |
34dc7c2f BB |
4677 | error = dmu_tx_assign(tx, TXG_WAIT); |
4678 | if (error) { | |
4679 | dmu_tx_abort(tx); | |
4680 | } else { | |
0df9673f | 4681 | ZFS_TIME_ENCODE(&ip->i_atime, atime); |
34dc7c2f | 4682 | mutex_enter(&zp->z_lock); |
0037b49e | 4683 | (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), |
0df9673f | 4684 | (void *)&atime, sizeof (atime), tx); |
34dc7c2f BB |
4685 | zp->z_atime_dirty = 0; |
4686 | mutex_exit(&zp->z_lock); | |
4687 | dmu_tx_commit(tx); | |
4688 | } | |
4689 | } | |
4690 | ||
4691 | zfs_zinactive(zp); | |
cafbd2ac | 4692 | if (need_unlock) |
0037b49e | 4693 | rw_exit(&zfsvfs->z_teardown_inactive_lock); |
34dc7c2f BB |
4694 | } |
4695 | ||
4696 | /* | |
4697 | * Bounds-check the seek operation. | |
4698 | * | |
3558fd73 | 4699 | * IN: ip - inode seeking within |
34dc7c2f BB |
4700 | * ooff - old file offset |
4701 | * noffp - pointer to new file offset | |
4702 | * ct - caller context | |
4703 | * | |
4704 | * RETURN: 0 if success | |
4705 | * EINVAL if new offset invalid | |
4706 | */ | |
4707 | /* ARGSUSED */ | |
3558fd73 | 4708 | int |
9623f736 | 4709 | zfs_seek(struct inode *ip, offset_t ooff, offset_t *noffp) |
34dc7c2f | 4710 | { |
3558fd73 | 4711 | if (S_ISDIR(ip->i_mode)) |
34dc7c2f BB |
4712 | return (0); |
4713 | return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); | |
4714 | } | |
4715 | ||
34dc7c2f | 4716 | /* |
dde471ef | 4717 | * Fill pages with data from the disk. |
34dc7c2f BB |
4718 | */ |
4719 | static int | |
dde471ef | 4720 | zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages) |
34dc7c2f | 4721 | { |
d1d7e268 | 4722 | znode_t *zp = ITOZ(ip); |
0037b49e | 4723 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
d1d7e268 | 4724 | objset_t *os; |
dde471ef | 4725 | struct page *cur_pp; |
d1d7e268 MK |
4726 | u_offset_t io_off, total; |
4727 | size_t io_len; | |
4728 | loff_t i_size; | |
4729 | unsigned page_idx; | |
4730 | int err; | |
34dc7c2f | 4731 | |
0037b49e | 4732 | os = zfsvfs->z_os; |
8b1899d3 | 4733 | io_len = nr_pages << PAGE_SHIFT; |
dde471ef PJ |
4734 | i_size = i_size_read(ip); |
4735 | io_off = page_offset(pl[0]); | |
4736 | ||
4737 | if (io_off + io_len > i_size) | |
4738 | io_len = i_size - io_off; | |
34dc7c2f BB |
4739 | |
4740 | /* | |
dde471ef | 4741 | * Iterate over list of pages and read each page individually. |
34dc7c2f | 4742 | */ |
dde471ef | 4743 | page_idx = 0; |
34dc7c2f | 4744 | for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { |
d164b209 BB |
4745 | caddr_t va; |
4746 | ||
540c3927 | 4747 | cur_pp = pl[page_idx++]; |
dde471ef | 4748 | va = kmap(cur_pp); |
9babb374 BB |
4749 | err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, |
4750 | DMU_READ_PREFETCH); | |
dde471ef | 4751 | kunmap(cur_pp); |
34dc7c2f | 4752 | if (err) { |
b128c09f BB |
4753 | /* convert checksum errors into IO errors */ |
4754 | if (err == ECKSUM) | |
2e528b49 | 4755 | err = SET_ERROR(EIO); |
34dc7c2f BB |
4756 | return (err); |
4757 | } | |
34dc7c2f | 4758 | } |
d164b209 | 4759 | |
34dc7c2f BB |
4760 | return (0); |
4761 | } | |
4762 | ||
4763 | /* | |
dde471ef | 4764 | * Uses zfs_fillpage to read data from the file and fill the pages. |
34dc7c2f | 4765 | * |
dde471ef PJ |
4766 | * IN: ip - inode of file to get data from. |
4767 | * pl - list of pages to read | |
4768 | * nr_pages - number of pages to read | |
34dc7c2f | 4769 | * |
d3cc8b15 | 4770 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
4771 | * |
4772 | * Timestamps: | |
4773 | * vp - atime updated | |
4774 | */ | |
4775 | /* ARGSUSED */ | |
dde471ef PJ |
4776 | int |
4777 | zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages) | |
34dc7c2f | 4778 | { |
dde471ef | 4779 | znode_t *zp = ITOZ(ip); |
0037b49e | 4780 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
dde471ef | 4781 | int err; |
d164b209 | 4782 | |
d164b209 BB |
4783 | if (pl == NULL) |
4784 | return (0); | |
34dc7c2f | 4785 | |
0037b49e | 4786 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
4787 | ZFS_VERIFY_ZP(zp); |
4788 | ||
dde471ef | 4789 | err = zfs_fillpage(ip, pl, nr_pages); |
34dc7c2f | 4790 | |
0037b49e | 4791 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4792 | return (err); |
4793 | } | |
4794 | ||
4795 | /* | |
e2e7aa2d | 4796 | * Check ZFS specific permissions to memory map a section of a file. |
34dc7c2f | 4797 | * |
e2e7aa2d BB |
4798 | * IN: ip - inode of the file to mmap |
4799 | * off - file offset | |
4800 | * addrp - start address in memory region | |
4801 | * len - length of memory region | |
4802 | * vm_flags- address flags | |
34dc7c2f | 4803 | * |
e2e7aa2d BB |
4804 | * RETURN: 0 if success |
4805 | * error code if failure | |
34dc7c2f BB |
4806 | */ |
4807 | /*ARGSUSED*/ | |
e2e7aa2d BB |
4808 | int |
4809 | zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len, | |
4810 | unsigned long vm_flags) | |
34dc7c2f | 4811 | { |
e2e7aa2d | 4812 | znode_t *zp = ITOZ(ip); |
0037b49e | 4813 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f | 4814 | |
0037b49e | 4815 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
4816 | ZFS_VERIFY_ZP(zp); |
4817 | ||
e2e7aa2d | 4818 | if ((vm_flags & VM_WRITE) && (zp->z_pflags & |
428870ff | 4819 | (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { |
0037b49e | 4820 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4821 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
4822 | } |
4823 | ||
e2e7aa2d | 4824 | if ((vm_flags & (VM_READ | VM_EXEC)) && |
428870ff | 4825 | (zp->z_pflags & ZFS_AV_QUARANTINED)) { |
0037b49e | 4826 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4827 | return (SET_ERROR(EACCES)); |
34dc7c2f BB |
4828 | } |
4829 | ||
34dc7c2f | 4830 | if (off < 0 || len > MAXOFFSET_T - off) { |
0037b49e | 4831 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4832 | return (SET_ERROR(ENXIO)); |
34dc7c2f BB |
4833 | } |
4834 | ||
0037b49e | 4835 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4836 | return (0); |
4837 | } | |
4838 | ||
3558fd73 BB |
4839 | /* |
4840 | * convoff - converts the given data (start, whence) to the | |
4841 | * given whence. | |
4842 | */ | |
4843 | int | |
4844 | convoff(struct inode *ip, flock64_t *lckdat, int whence, offset_t offset) | |
4845 | { | |
5484965a | 4846 | vattr_t vap; |
3558fd73 BB |
4847 | int error; |
4848 | ||
4849 | if ((lckdat->l_whence == 2) || (whence == 2)) { | |
d95a5980 | 4850 | if ((error = zfs_getattr(ip, &vap, 0, CRED()))) |
3558fd73 BB |
4851 | return (error); |
4852 | } | |
4853 | ||
4854 | switch (lckdat->l_whence) { | |
4855 | case 1: | |
4856 | lckdat->l_start += offset; | |
4857 | break; | |
4858 | case 2: | |
5484965a | 4859 | lckdat->l_start += vap.va_size; |
3558fd73 BB |
4860 | /* FALLTHRU */ |
4861 | case 0: | |
4862 | break; | |
4863 | default: | |
2e528b49 | 4864 | return (SET_ERROR(EINVAL)); |
3558fd73 BB |
4865 | } |
4866 | ||
4867 | if (lckdat->l_start < 0) | |
2e528b49 | 4868 | return (SET_ERROR(EINVAL)); |
3558fd73 BB |
4869 | |
4870 | switch (whence) { | |
4871 | case 1: | |
4872 | lckdat->l_start -= offset; | |
4873 | break; | |
4874 | case 2: | |
5484965a | 4875 | lckdat->l_start -= vap.va_size; |
3558fd73 BB |
4876 | /* FALLTHRU */ |
4877 | case 0: | |
4878 | break; | |
4879 | default: | |
2e528b49 | 4880 | return (SET_ERROR(EINVAL)); |
3558fd73 BB |
4881 | } |
4882 | ||
4883 | lckdat->l_whence = (short)whence; | |
4884 | return (0); | |
4885 | } | |
4886 | ||
34dc7c2f BB |
4887 | /* |
4888 | * Free or allocate space in a file. Currently, this function only | |
4889 | * supports the `F_FREESP' command. However, this command is somewhat | |
4890 | * misnamed, as its functionality includes the ability to allocate as | |
4891 | * well as free space. | |
4892 | * | |
3558fd73 | 4893 | * IN: ip - inode of file to free data in. |
34dc7c2f BB |
4894 | * cmd - action to take (only F_FREESP supported). |
4895 | * bfp - section of file to free/alloc. | |
4896 | * flag - current file open mode flags. | |
4897 | * offset - current file offset. | |
4898 | * cr - credentials of caller [UNUSED]. | |
34dc7c2f | 4899 | * |
d3cc8b15 | 4900 | * RETURN: 0 on success, error code on failure. |
34dc7c2f BB |
4901 | * |
4902 | * Timestamps: | |
3558fd73 | 4903 | * ip - ctime|mtime updated |
34dc7c2f BB |
4904 | */ |
4905 | /* ARGSUSED */ | |
e5c39b95 | 4906 | int |
3558fd73 BB |
4907 | zfs_space(struct inode *ip, int cmd, flock64_t *bfp, int flag, |
4908 | offset_t offset, cred_t *cr) | |
34dc7c2f | 4909 | { |
3558fd73 | 4910 | znode_t *zp = ITOZ(ip); |
0037b49e | 4911 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
4912 | uint64_t off, len; |
4913 | int error; | |
4914 | ||
0037b49e | 4915 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
4916 | ZFS_VERIFY_ZP(zp); |
4917 | ||
34dc7c2f | 4918 | if (cmd != F_FREESP) { |
0037b49e | 4919 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4920 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
4921 | } |
4922 | ||
f3c9dca0 MT |
4923 | /* |
4924 | * Callers might not be able to detect properly that we are read-only, | |
4925 | * so check it explicitly here. | |
4926 | */ | |
0037b49e BB |
4927 | if (zfs_is_readonly(zfsvfs)) { |
4928 | ZFS_EXIT(zfsvfs); | |
f3c9dca0 MT |
4929 | return (SET_ERROR(EROFS)); |
4930 | } | |
4931 | ||
3558fd73 | 4932 | if ((error = convoff(ip, bfp, 0, offset))) { |
0037b49e | 4933 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4934 | return (error); |
4935 | } | |
4936 | ||
4937 | if (bfp->l_len < 0) { | |
0037b49e | 4938 | ZFS_EXIT(zfsvfs); |
2e528b49 | 4939 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
4940 | } |
4941 | ||
aec69371 ED |
4942 | /* |
4943 | * Permissions aren't checked on Solaris because on this OS | |
4944 | * zfs_space() can only be called with an opened file handle. | |
4945 | * On Linux we can get here through truncate_range() which | |
4946 | * operates directly on inodes, so we need to check access rights. | |
4947 | */ | |
4948 | if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) { | |
0037b49e | 4949 | ZFS_EXIT(zfsvfs); |
aec69371 ED |
4950 | return (error); |
4951 | } | |
4952 | ||
34dc7c2f BB |
4953 | off = bfp->l_start; |
4954 | len = bfp->l_len; /* 0 means from off to end of file */ | |
4955 | ||
b128c09f | 4956 | error = zfs_freesp(zp, off, len, flag, TRUE); |
34dc7c2f | 4957 | |
0037b49e | 4958 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
4959 | return (error); |
4960 | } | |
4961 | ||
4962 | /*ARGSUSED*/ | |
e5c39b95 | 4963 | int |
3558fd73 | 4964 | zfs_fid(struct inode *ip, fid_t *fidp) |
34dc7c2f | 4965 | { |
3558fd73 | 4966 | znode_t *zp = ITOZ(ip); |
0037b49e | 4967 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f | 4968 | uint32_t gen; |
428870ff | 4969 | uint64_t gen64; |
34dc7c2f BB |
4970 | uint64_t object = zp->z_id; |
4971 | zfid_short_t *zfid; | |
428870ff | 4972 | int size, i, error; |
34dc7c2f | 4973 | |
0037b49e | 4974 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 4975 | ZFS_VERIFY_ZP(zp); |
428870ff | 4976 | |
0037b49e | 4977 | if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), |
428870ff | 4978 | &gen64, sizeof (uint64_t))) != 0) { |
0037b49e | 4979 | ZFS_EXIT(zfsvfs); |
428870ff BB |
4980 | return (error); |
4981 | } | |
4982 | ||
4983 | gen = (uint32_t)gen64; | |
34dc7c2f | 4984 | |
9b77d1c9 | 4985 | size = SHORT_FID_LEN; |
34dc7c2f BB |
4986 | |
4987 | zfid = (zfid_short_t *)fidp; | |
4988 | ||
4989 | zfid->zf_len = size; | |
4990 | ||
4991 | for (i = 0; i < sizeof (zfid->zf_object); i++) | |
4992 | zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); | |
4993 | ||
4994 | /* Must have a non-zero generation number to distinguish from .zfs */ | |
4995 | if (gen == 0) | |
4996 | gen = 1; | |
4997 | for (i = 0; i < sizeof (zfid->zf_gen); i++) | |
4998 | zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); | |
4999 | ||
0037b49e | 5000 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
5001 | return (0); |
5002 | } | |
5003 | ||
34dc7c2f | 5004 | /*ARGSUSED*/ |
e5c39b95 | 5005 | int |
3558fd73 | 5006 | zfs_getsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr) |
34dc7c2f | 5007 | { |
3558fd73 | 5008 | znode_t *zp = ITOZ(ip); |
0037b49e | 5009 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
5010 | int error; |
5011 | boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; | |
5012 | ||
0037b49e | 5013 | ZFS_ENTER(zfsvfs); |
34dc7c2f BB |
5014 | ZFS_VERIFY_ZP(zp); |
5015 | error = zfs_getacl(zp, vsecp, skipaclchk, cr); | |
0037b49e | 5016 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
5017 | |
5018 | return (error); | |
5019 | } | |
5020 | ||
5021 | /*ARGSUSED*/ | |
e5c39b95 | 5022 | int |
3558fd73 | 5023 | zfs_setsecattr(struct inode *ip, vsecattr_t *vsecp, int flag, cred_t *cr) |
34dc7c2f | 5024 | { |
3558fd73 | 5025 | znode_t *zp = ITOZ(ip); |
0037b49e | 5026 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
34dc7c2f BB |
5027 | int error; |
5028 | boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; | |
0037b49e | 5029 | zilog_t *zilog = zfsvfs->z_log; |
34dc7c2f | 5030 | |
0037b49e | 5031 | ZFS_ENTER(zfsvfs); |
34dc7c2f | 5032 | ZFS_VERIFY_ZP(zp); |
428870ff | 5033 | |
34dc7c2f | 5034 | error = zfs_setacl(zp, vsecp, skipaclchk, cr); |
428870ff | 5035 | |
0037b49e | 5036 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) |
572e2857 | 5037 | zil_commit(zilog, 0); |
428870ff | 5038 | |
0037b49e | 5039 | ZFS_EXIT(zfsvfs); |
34dc7c2f BB |
5040 | return (error); |
5041 | } | |
5042 | ||
3558fd73 | 5043 | #ifdef HAVE_UIO_ZEROCOPY |
428870ff BB |
5044 | /* |
5045 | * Tunable, both must be a power of 2. | |
5046 | * | |
5047 | * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf | |
5048 | * zcr_blksz_max: if set to less than the file block size, allow loaning out of | |
3558fd73 | 5049 | * an arcbuf for a partial block read |
428870ff BB |
5050 | */ |
5051 | int zcr_blksz_min = (1 << 10); /* 1K */ | |
5052 | int zcr_blksz_max = (1 << 17); /* 128K */ | |
5053 | ||
5054 | /*ARGSUSED*/ | |
5055 | static int | |
3558fd73 | 5056 | zfs_reqzcbuf(struct inode *ip, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr) |
428870ff | 5057 | { |
3558fd73 | 5058 | znode_t *zp = ITOZ(ip); |
0037b49e BB |
5059 | zfsvfs_t *zfsvfs = ITOZSB(ip); |
5060 | int max_blksz = zfsvfs->z_max_blksz; | |
428870ff BB |
5061 | uio_t *uio = &xuio->xu_uio; |
5062 | ssize_t size = uio->uio_resid; | |
5063 | offset_t offset = uio->uio_loffset; | |
5064 | int blksz; | |
5065 | int fullblk, i; | |
5066 | arc_buf_t *abuf; | |
5067 | ssize_t maxsize; | |
5068 | int preamble, postamble; | |
5069 | ||
5070 | if (xuio->xu_type != UIOTYPE_ZEROCOPY) | |
2e528b49 | 5071 | return (SET_ERROR(EINVAL)); |
428870ff | 5072 | |
0037b49e | 5073 | ZFS_ENTER(zfsvfs); |
428870ff BB |
5074 | ZFS_VERIFY_ZP(zp); |
5075 | switch (ioflag) { | |
5076 | case UIO_WRITE: | |
5077 | /* | |
5078 | * Loan out an arc_buf for write if write size is bigger than | |
5079 | * max_blksz, and the file's block size is also max_blksz. | |
5080 | */ | |
5081 | blksz = max_blksz; | |
5082 | if (size < blksz || zp->z_blksz != blksz) { | |
0037b49e | 5083 | ZFS_EXIT(zfsvfs); |
2e528b49 | 5084 | return (SET_ERROR(EINVAL)); |
428870ff BB |
5085 | } |
5086 | /* | |
5087 | * Caller requests buffers for write before knowing where the | |
5088 | * write offset might be (e.g. NFS TCP write). | |
5089 | */ | |
5090 | if (offset == -1) { | |
5091 | preamble = 0; | |
5092 | } else { | |
5093 | preamble = P2PHASE(offset, blksz); | |
5094 | if (preamble) { | |
5095 | preamble = blksz - preamble; | |
5096 | size -= preamble; | |
5097 | } | |
5098 | } | |
5099 | ||
5100 | postamble = P2PHASE(size, blksz); | |
5101 | size -= postamble; | |
5102 | ||
5103 | fullblk = size / blksz; | |
5104 | (void) dmu_xuio_init(xuio, | |
5105 | (preamble != 0) + fullblk + (postamble != 0)); | |
428870ff BB |
5106 | |
5107 | /* | |
5108 | * Have to fix iov base/len for partial buffers. They | |
5109 | * currently represent full arc_buf's. | |
5110 | */ | |
5111 | if (preamble) { | |
5112 | /* data begins in the middle of the arc_buf */ | |
5113 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
5114 | blksz); | |
5115 | ASSERT(abuf); | |
5116 | (void) dmu_xuio_add(xuio, abuf, | |
5117 | blksz - preamble, preamble); | |
5118 | } | |
5119 | ||
5120 | for (i = 0; i < fullblk; i++) { | |
5121 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
5122 | blksz); | |
5123 | ASSERT(abuf); | |
5124 | (void) dmu_xuio_add(xuio, abuf, 0, blksz); | |
5125 | } | |
5126 | ||
5127 | if (postamble) { | |
5128 | /* data ends in the middle of the arc_buf */ | |
5129 | abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), | |
5130 | blksz); | |
5131 | ASSERT(abuf); | |
5132 | (void) dmu_xuio_add(xuio, abuf, 0, postamble); | |
5133 | } | |
5134 | break; | |
5135 | case UIO_READ: | |
5136 | /* | |
5137 | * Loan out an arc_buf for read if the read size is larger than | |
5138 | * the current file block size. Block alignment is not | |
5139 | * considered. Partial arc_buf will be loaned out for read. | |
5140 | */ | |
5141 | blksz = zp->z_blksz; | |
5142 | if (blksz < zcr_blksz_min) | |
5143 | blksz = zcr_blksz_min; | |
5144 | if (blksz > zcr_blksz_max) | |
5145 | blksz = zcr_blksz_max; | |
5146 | /* avoid potential complexity of dealing with it */ | |
5147 | if (blksz > max_blksz) { | |
0037b49e | 5148 | ZFS_EXIT(zfsvfs); |
2e528b49 | 5149 | return (SET_ERROR(EINVAL)); |
428870ff BB |
5150 | } |
5151 | ||
5152 | maxsize = zp->z_size - uio->uio_loffset; | |
5153 | if (size > maxsize) | |
5154 | size = maxsize; | |
5155 | ||
3558fd73 | 5156 | if (size < blksz) { |
0037b49e | 5157 | ZFS_EXIT(zfsvfs); |
2e528b49 | 5158 | return (SET_ERROR(EINVAL)); |
428870ff BB |
5159 | } |
5160 | break; | |
5161 | default: | |
0037b49e | 5162 | ZFS_EXIT(zfsvfs); |
2e528b49 | 5163 | return (SET_ERROR(EINVAL)); |
428870ff BB |
5164 | } |
5165 | ||
5166 | uio->uio_extflg = UIO_XUIO; | |
5167 | XUIO_XUZC_RW(xuio) = ioflag; | |
0037b49e | 5168 | ZFS_EXIT(zfsvfs); |
428870ff BB |
5169 | return (0); |
5170 | } | |
5171 | ||
5172 | /*ARGSUSED*/ | |
5173 | static int | |
3558fd73 | 5174 | zfs_retzcbuf(struct inode *ip, xuio_t *xuio, cred_t *cr) |
428870ff BB |
5175 | { |
5176 | int i; | |
5177 | arc_buf_t *abuf; | |
5178 | int ioflag = XUIO_XUZC_RW(xuio); | |
5179 | ||
5180 | ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); | |
5181 | ||
5182 | i = dmu_xuio_cnt(xuio); | |
5183 | while (i-- > 0) { | |
5184 | abuf = dmu_xuio_arcbuf(xuio, i); | |
5185 | /* | |
5186 | * if abuf == NULL, it must be a write buffer | |
5187 | * that has been returned in zfs_write(). | |
5188 | */ | |
5189 | if (abuf) | |
5190 | dmu_return_arcbuf(abuf); | |
5191 | ASSERT(abuf || ioflag == UIO_WRITE); | |
5192 | } | |
5193 | ||
5194 | dmu_xuio_fini(xuio); | |
5195 | return (0); | |
5196 | } | |
3558fd73 | 5197 | #endif /* HAVE_UIO_ZEROCOPY */ |
c409e464 BB |
5198 | |
5199 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
f298b24d BB |
5200 | EXPORT_SYMBOL(zfs_open); |
5201 | EXPORT_SYMBOL(zfs_close); | |
5202 | EXPORT_SYMBOL(zfs_read); | |
5203 | EXPORT_SYMBOL(zfs_write); | |
5204 | EXPORT_SYMBOL(zfs_access); | |
5205 | EXPORT_SYMBOL(zfs_lookup); | |
5206 | EXPORT_SYMBOL(zfs_create); | |
5207 | EXPORT_SYMBOL(zfs_tmpfile); | |
5208 | EXPORT_SYMBOL(zfs_remove); | |
5209 | EXPORT_SYMBOL(zfs_mkdir); | |
5210 | EXPORT_SYMBOL(zfs_rmdir); | |
5211 | EXPORT_SYMBOL(zfs_readdir); | |
5212 | EXPORT_SYMBOL(zfs_fsync); | |
5213 | EXPORT_SYMBOL(zfs_getattr); | |
5214 | EXPORT_SYMBOL(zfs_getattr_fast); | |
5215 | EXPORT_SYMBOL(zfs_setattr); | |
5216 | EXPORT_SYMBOL(zfs_rename); | |
5217 | EXPORT_SYMBOL(zfs_symlink); | |
5218 | EXPORT_SYMBOL(zfs_readlink); | |
5219 | EXPORT_SYMBOL(zfs_link); | |
5220 | EXPORT_SYMBOL(zfs_inactive); | |
5221 | EXPORT_SYMBOL(zfs_space); | |
5222 | EXPORT_SYMBOL(zfs_fid); | |
5223 | EXPORT_SYMBOL(zfs_getsecattr); | |
5224 | EXPORT_SYMBOL(zfs_setsecattr); | |
5225 | EXPORT_SYMBOL(zfs_getpage); | |
5226 | EXPORT_SYMBOL(zfs_putpage); | |
5227 | EXPORT_SYMBOL(zfs_dirty_inode); | |
5228 | EXPORT_SYMBOL(zfs_map); | |
5229 | ||
02730c33 | 5230 | /* CSTYLED */ |
a966c564 K |
5231 | module_param(zfs_delete_blocks, ulong, 0644); |
5232 | MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async"); | |
c409e464 BB |
5233 | module_param(zfs_read_chunk_size, long, 0644); |
5234 | MODULE_PARM_DESC(zfs_read_chunk_size, "Bytes to read per chunk"); | |
5235 | #endif |