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