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