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