This repository has been archived by the owner on Feb 16, 2020. It is now read-only.
forked from dustin/mac-zfs
/
zfs_vnops.c
6316 lines (5697 loc) · 149 KB
/
zfs_vnops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Portions Copyright 2007-2008 Apple Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Portions Copyright 2007 Jeremy Teo */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/systm.h>
#ifdef __APPLE__
#include <sys/kernel.h>
#include <sys/fcntl.h>
#include <sys/vnode.h>
#include <sys/vnode_if.h>
#include <sys/stat.h>
#include <sys/ucred.h>
#include <sys/unistd.h>
#include <sys/xattr.h>
#include <sys/zfs_context.h>
#else
#include <sys/sysmacros.h>
#include <sys/resource.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/kmem.h>
#include <sys/taskq.h>
#include <sys/uio.h>
#include <sys/vmsystm.h>
#include <sys/atomic.h>
#include <sys/vm.h>
#include <vm/seg_vn.h>
#include <vm/pvn.h>
#include <vm/as.h>
#include <sys/mman.h>
#include <sys/pathname.h>
#include <sys/cmn_err.h>
#include <sys/errno.h>
#include <sys/unistd.h>
#endif /* __APPLE__ */
#include <sys/zfs_vfsops.h>
#include <sys/zfs_dir.h>
#include <sys/zfs_acl.h>
#include <sys/zfs_ioctl.h>
#include <sys/fs/zfs.h>
#include <sys/dmu.h>
#include <sys/spa.h>
#include <sys/txg.h>
#include <sys/dbuf.h>
#include <sys/zap.h>
#include <sys/dirent.h>
#ifdef __APPLE__
#include <sys/zfs_ctldir.h>
#include <sys/zfs_rlock.h>
#include <sys/unistd.h>
#include <sys/utfconv.h>
#include <sys/ubc.h>
#else
#include <sys/policy.h>
#include <sys/sunddi.h>
#include <sys/filio.h>
#include "fs/fs_subr.h"
#include <sys/zfs_ctldir.h>
#include <sys/dnlc.h>
#include <sys/zfs_rlock.h>
#endif /* __APPLE__ */
/*
* Programming rules.
*
* Each vnode op performs some logical unit of work. To do this, the ZPL must
* properly lock its in-core state, create a DMU transaction, do the work,
* record this work in the intent log (ZIL), commit the DMU transaction,
* and wait the the intent log to commit if it's is a synchronous operation.
* Morover, the vnode ops must work in both normal and log replay context.
* The ordering of events is important to avoid deadlocks and references
* to freed memory. The example below illustrates the following Big Rules:
*
* (1) A check must be made in each zfs thread for a mounted file system.
* This is done avoiding races using ZFS_ENTER(zfsvfs).
* A ZFS_EXIT(zfsvfs) is needed before all returns.
*
* (2) VN_RELE() should always be the last thing except for zil_commit()
* (if necessary) and ZFS_EXIT(). This is for 3 reasons:
* First, if it's the last reference, the vnode/znode
* can be freed, so the zp may point to freed memory. Second, the last
* reference will call zfs_zinactive(), which may induce a lot of work --
* pushing cached pages (which acquires range locks) and syncing out
* cached atime changes. Third, zfs_zinactive() may require a new tx,
* which could deadlock the system if you were already holding one.
*
* (3) All range locks must be grabbed before calling dmu_tx_assign(),
* as they can span dmu_tx_assign() calls.
*
* (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign().
* In normal operation, this will be TXG_NOWAIT. During ZIL replay,
* it will be a specific txg. Either way, dmu_tx_assign() never blocks.
* This is critical because we don't want to block while holding locks.
* Note, in particular, that if a lock is sometimes acquired before
* the tx assigns, and sometimes after (e.g. z_lock), then failing to
* use a non-blocking assign can deadlock the system. The scenario:
*
* Thread A has grabbed a lock before calling dmu_tx_assign().
* Thread B is in an already-assigned tx, and blocks for this lock.
* Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open()
* forever, because the previous txg can't quiesce until B's tx commits.
*
* If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT,
* then drop all locks, call dmu_tx_wait(), and try again.
*
* (5) If the operation succeeded, generate the intent log entry for it
* before dropping locks. This ensures that the ordering of events
* in the intent log matches the order in which they actually occurred.
*
* (6) At the end of each vnode op, the DMU tx must always commit,
* regardless of whether there were any errors.
*
* (7) After dropping all locks, invoke zil_commit(zilog, seq, foid)
* to ensure that synchronous semantics are provided when necessary.
*
* In general, this is how things should be ordered in each vnode op:
*
* ZFS_ENTER(zfsvfs); // exit if unmounted
* top:
* zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD())
* rw_enter(...); // grab any other locks you need
* tx = dmu_tx_create(...); // get DMU tx
* dmu_tx_hold_*(); // hold each object you might modify
* error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign
* if (error) {
* rw_exit(...); // drop locks
* zfs_dirent_unlock(dl); // unlock directory entry
* VN_RELE(...); // release held vnodes
* if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) {
* dmu_tx_wait(tx);
* dmu_tx_abort(tx);
* goto top;
* }
* dmu_tx_abort(tx); // abort DMU tx
* ZFS_EXIT(zfsvfs); // finished in zfs
* return (error); // really out of space
* }
* error = do_real_work(); // do whatever this VOP does
* if (error == 0)
* zfs_log_*(...); // on success, make ZIL entry
* dmu_tx_commit(tx); // commit DMU tx -- error or not
* rw_exit(...); // drop locks
* zfs_dirent_unlock(dl); // unlock directory entry
* VN_RELE(...); // release held vnodes
* zil_commit(zilog, seq, foid); // synchronous when necessary
* ZFS_EXIT(zfsvfs); // finished in zfs
* return (error); // done, report error
*/
#ifdef __APPLE__
typedef int vcexcl_t;
enum vcexcl { NONEXCL, EXCL };
static int zfs_getsecattr(znode_t *, kauth_acl_t *, cred_t *);
static int zfs_setsecattr(znode_t *, kauth_acl_t, cred_t *);
int zfs_obtain_xattr(znode_t *, const char *, mode_t, cred_t *,
struct vnode **, int);
static int zfs_vnop_fsync(struct vnop_fsync_args *ap);
#endif /* __APPLE__ */
#ifdef __APPLE__
static int
zfs_vnop_open(struct vnop_open_args *ap)
{
return (0);
}
static int
zfs_vnop_close(struct vnop_close_args *ap)
{
return (0);
}
#endif /* __APPLE__ */
#ifndef __APPLE__
/* ARGSUSED */
static int
zfs_open(vnode_t **vpp, int flag, cred_t *cr)
{
znode_t *zp = VTOZ(*vpp);
/* Keep a count of the synchronous opens in the znode */
if (flag & (FSYNC | FDSYNC))
atomic_inc_32(&zp->z_sync_cnt);
return (0);
}
#endif /* !__APPLE__ */
#ifndef __APPLE__
/* ARGSUSED */
static int
zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr)
{
znode_t *zp = VTOZ(vp);
/* Decrement the synchronous opens in the znode */
if ((flag & (FSYNC | FDSYNC)) && (count == 1))
atomic_dec_32(&zp->z_sync_cnt);
/*
* Clean up any locks held by this process on the vp.
*/
cleanlocks(vp, ddi_get_pid(), 0);
cleanshares(vp, ddi_get_pid());
return (0);
}
#endif /* !__APPLE__ */
#ifdef __APPLE__
/*
* Spotlight specific fcntl()'s
*/
#define SPOTLIGHT_GET_MOUNT_TIME (FCNTL_FS_SPECIFIC_BASE + 0x00002)
#define SPOTLIGHT_GET_UNMOUNT_TIME (FCNTL_FS_SPECIFIC_BASE + 0x00003)
#endif /* __APPLE */
#ifdef __APPLE__
static int
zfs_vnop_ioctl(struct vnop_ioctl_args *ap)
{
znode_t *zp = VTOZ(ap->a_vp);
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
user_addr_t useraddr = CAST_USER_ADDR_T(ap->a_data);
int error;
ZFS_ENTER(zfsvfs);
switch (ap->a_command) {
case F_FULLFSYNC: {
struct vnop_fsync_args fsync_args;
fsync_args.a_vp = ap->a_vp;
fsync_args.a_waitfor = MNT_WAIT;
fsync_args.a_context = ap->a_context;
if ((error = zfs_vnop_fsync(&fsync_args)))
break;
if (zfsvfs->z_log != NULL)
zil_commit(zfsvfs->z_log, UINT64_MAX, 0);
else
txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), 0);
break;
}
case SPOTLIGHT_GET_MOUNT_TIME:
error = copyout(&zfsvfs->z_mount_time, useraddr,
sizeof (zfsvfs->z_mount_time));
break;
case SPOTLIGHT_GET_UNMOUNT_TIME:
error = copyout(&zfsvfs->z_last_unmount_time, useraddr,
sizeof (zfsvfs->z_last_unmount_time));
break;
default:
error = ENOTTY;
}
ZFS_EXIT(zfsvfs);
return (error);
}
#endif /* __APPLE */
#ifndef __APPLE__
/*
* Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and
* data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter.
*/
static int
zfs_holey(vnode_t *vp, int cmd, offset_t *off)
{
znode_t *zp = VTOZ(vp);
uint64_t noff = (uint64_t)*off; /* new offset */
uint64_t file_sz;
int error;
boolean_t hole;
file_sz = zp->z_phys->zp_size;
if (noff >= file_sz) {
return (ENXIO);
}
if (cmd == _FIO_SEEK_HOLE)
hole = B_TRUE;
else
hole = B_FALSE;
error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff);
/* end of file? */
if ((error == ESRCH) || (noff > file_sz)) {
/*
* Handle the virtual hole at the end of file.
*/
if (hole) {
*off = file_sz;
return (0);
}
return (ENXIO);
}
if (noff < *off)
return (error);
*off = noff;
return (error);
}
/* ARGSUSED */
static int
zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred,
int *rvalp)
{
offset_t off;
int error;
zfsvfs_t *zfsvfs;
switch (com) {
case _FIOFFS:
return (zfs_sync(vp->v_vfsp, 0, cred));
/*
* The following two ioctls are used by bfu. Faking out,
* necessary to avoid bfu errors.
*/
case _FIOGDIO:
case _FIOSDIO:
return (0);
case _FIO_SEEK_DATA:
case _FIO_SEEK_HOLE:
if (ddi_copyin((void *)data, &off, sizeof (off), flag))
return (EFAULT);
zfsvfs = VTOZ(vp)->z_zfsvfs;
ZFS_ENTER(zfsvfs);
/* offset parameter is in/out */
error = zfs_holey(vp, com, &off);
ZFS_EXIT(zfsvfs);
if (error)
return (error);
if (ddi_copyout(&off, (void *)data, sizeof (off), flag))
return (EFAULT);
return (0);
}
return (ENOTTY);
}
#endif /* !__APPLE__ */
/*
* When a file is memory mapped, we must keep the IO data synchronized
* between the DMU cache and the memory mapped pages. What this means:
*
* On Write: If we find a memory mapped page, we write to *both*
* the page and the dmu buffer.
*
* NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
* the file is memory mapped.
*/
static int
#ifdef __APPLE__
mappedwrite(struct vnode *vp, int nbytes, struct uio *uio, dmu_tx_t *tx)
#else
mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx)
#endif /* __APPLE__ */
{
znode_t *zp = VTOZ(vp);
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
int len = nbytes;
int error = 0;
#ifdef __APPLE__
vm_offset_t vaddr;
upl_t upl;
upl_page_info_t *pl = NULL;
off_t upl_start;
int upl_size;
int upl_page;
off_t off;
#else
int64_t start, off;
#endif /* __APPLE__ */
#ifdef __APPLE__
upl_start = uio_offset(uio);
off = upl_start & (PAGE_SIZE - 1);
upl_start &= ~PAGE_MASK;
upl_size = (off + nbytes + (PAGE_SIZE - 1)) & ~PAGE_MASK;
/*
* Create a UPL for the current range and map its
* page list into the kernel virtual address space.
*/
if ( ubc_create_upl(vp, upl_start, upl_size, &upl, NULL,
UPL_FILE_IO | UPL_SET_LITE) == KERN_SUCCESS ) {
pl = ubc_upl_pageinfo(upl);
ubc_upl_map(upl, &vaddr);
}
for (upl_page = 0; len > 0; ++upl_page)
#else
start = uio->uio_loffset;
off = start & PAGEOFFSET;
for (start &= PAGEMASK; len > 0; start += PAGESIZE)
#endif /* __APPLE__ */
{ // for loop
uint64_t bytes = MIN(PAGESIZE - off, len);
#ifdef __APPLE__
uint64_t woff = uio_offset(uio);
#else
page_t *pp;
uint64_t woff = uio->uio_loffset;
#endif /* __APPLE__ */
/*
* We don't want a new page to "appear" in the middle of
* the file update (because it may not get the write
* update data), so we grab a lock to block
* zfs_getpage().
*/
rw_enter(&zp->z_map_lock, RW_WRITER);
#ifdef __APPLE__
if (pl && upl_valid_page(pl, upl_page)) {
rw_exit(&zp->z_map_lock);
uio_setrw(uio, UIO_WRITE);
error = uiomove((caddr_t)vaddr + off, bytes, uio);
if (error == 0) {
dmu_write(zfsvfs->z_os, zp->z_id,
woff, bytes, (caddr_t)vaddr + off, tx);
/*
* We don't need a ubc_upl_commit_range()
* here since the dmu_write() effectively
* pushed this page to disk.
*/
} else {
/*
* page is now in an unknown state so dump it.
*/
ubc_upl_abort_range(upl, upl_start, PAGESIZE,
UPL_ABORT_DUMP_PAGES);
}
} // else below
#else
if (pp = page_lookup(vp, start, SE_SHARED)) {
caddr_t va;
rw_exit(&zp->z_map_lock);
va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L);
error = uiomove(va+off, bytes, UIO_WRITE, uio);
if (error == 0) {
dmu_write(zfsvfs->z_os, zp->z_id,
woff, bytes, va+off, tx);
}
ppmapout(va);
page_unlock(pp);
} // else below
#endif /* __APPLE__ */
else {
error = dmu_write_uio(zfsvfs->z_os, zp->z_id,
uio, bytes, tx);
rw_exit(&zp->z_map_lock);
}
#ifdef __APPLE__
vaddr += PAGE_SIZE;
upl_start += PAGE_SIZE;
#endif /* __APPLE__ */
len -= bytes;
off = 0;
if (error)
break;
}
#ifdef __APPLE__
/*
* Unmap the page list and free the UPL.
*/
if (pl) {
(void) ubc_upl_unmap(upl);
/*
* We want to abort here since due to dmu_write()
* we effectively didn't dirty any pages.
*/
(void) ubc_upl_abort(upl, UPL_ABORT_FREE_ON_EMPTY);
}
#endif /* __APPLE__ */
return (error);
}
/*
* When a file is memory mapped, we must keep the IO data synchronized
* between the DMU cache and the memory mapped pages. What this means:
*
* On Read: We "read" preferentially from memory mapped pages,
* else we default from the dmu buffer.
*
* NOTE: We will always "break up" the IO into PAGESIZE uiomoves when
* the file is memory mapped.
*/
static int
#ifdef __APPLE__
mappedread(struct vnode *vp, int nbytes, struct uio *uio)
#else
mappedread(vnode_t *vp, int nbytes, uio_t *uio)
#endif /* __APPLE__ */
{
znode_t *zp = VTOZ(vp);
objset_t *os = zp->z_zfsvfs->z_os;
int len = nbytes;
int error = 0;
#ifdef __APPLE__
vm_offset_t vaddr;
upl_t upl;
upl_page_info_t *pl = NULL;
off_t upl_start;
int upl_size;
int upl_page;
off_t off;
#else
int64_t start, off;
#endif /* __APPLE__ */
#ifdef __APPLE__
upl_start = uio_offset(uio);
off = upl_start & PAGE_MASK;
upl_start &= ~PAGE_MASK;
upl_size = (off + nbytes + (PAGE_SIZE - 1)) & ~PAGE_MASK;
/*
* Create a UPL for the current range and map its
* page list into the kernel virtual address space.
*/
if ( ubc_create_upl(vp, upl_start, upl_size, &upl, NULL,
UPL_FILE_IO | UPL_SET_LITE) == KERN_SUCCESS ) {
pl = ubc_upl_pageinfo(upl);
ubc_upl_map(upl, &vaddr);
}
for (upl_page = 0; len > 0; ++upl_page)
#else
start = uio->uio_loffset;
off = start & PAGEOFFSET;
for (start &= PAGEMASK; len > 0; start += PAGESIZE)
#endif /* __APPLE__ */
{ // for loop
uint64_t bytes = MIN(PAGE_SIZE - off, len);
#ifdef __APPLE__
if (pl && upl_valid_page(pl, upl_page)) {
uio_setrw(uio, UIO_READ);
error = uiomove((caddr_t)vaddr + off, bytes, uio);
} else {
error = dmu_read_uio(os, zp->z_id, uio, bytes);
}
vaddr += PAGE_SIZE;
#else
page_t *pp;
if (pp = page_lookup(vp, start, SE_SHARED)) {
caddr_t va;
va = ppmapin(pp, PROT_READ, (caddr_t)-1L);
error = uiomove(va + off, bytes, UIO_READ, uio);
ppmapout(va);
page_unlock(pp);
} else {
error = dmu_read_uio(os, zp->z_id, uio, bytes);
}
#endif /* __APPLE__ */
len -= bytes;
off = 0;
if (error)
break;
}
#ifdef __APPLE__
/*
* Unmap the page list and free the UPL.
*/
if (pl) {
(void) ubc_upl_unmap(upl);
(void) ubc_upl_abort(upl, UPL_ABORT_FREE_ON_EMPTY);
}
#endif /* __APPLE__ */
return (error);
}
#ifdef __APPLE__
uint_t zfs_read_chunk_size = MAX_UPL_TRANSFER * PAGE_SIZE; /* Tunable */
#else
offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */
#endif /* __APPLE__ */
/*
* Read bytes from specified file into supplied buffer.
* MacOSX uses 'struct vnop read args', whereas OpenSolaris passes values as args
*
* IN: vp - vnode of file to be read from.
* uio - structure supplying read location, range info,
* and return buffer.
* ioflag - SYNC flags; used to provide FRSYNC semantics.
* cr - credentials of caller.
*
* OUT: uio - updated offset and range, buffer filled.
*
* RETURN: 0 if success
* error code if failure
*
* Side Effects:
* vp - atime updated if byte count > 0
*/
/* ARGSUSED */
static int
#ifdef __APPLE__
zfs_vnop_read(struct vnop_read_args *ap)
#else
zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
#endif /* __APPLE__ */
{
#ifdef __APPLE__
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
int ioflag = ap->a_ioflag;
#endif
znode_t *zp = VTOZ(vp);
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
objset_t *os = zfsvfs->z_os;
ssize_t n, nbytes;
int error;
rl_t *rl;
ZFS_ENTER(zfsvfs);
/*
* Validate file offset
*/
#ifdef __APPLE__
if (uio_offset(uio) < (offset_t)0)
#else
if (uio->uio_loffset < (offset_t)0)
#endif /* __APPLE__ */
{
ZFS_EXIT(zfsvfs);
return (EINVAL);
}
/*
* Fasttrack empty reads
*/
#ifdef __APPLE__
if (uio_resid(uio) == 0)
#else
if (uio->uio_resid == 0)
#endif /* __APPLE__ */
{
ZFS_EXIT(zfsvfs);
return (0);
}
/*
* Note: In Mac OS X, mandatory lock checking occurs up in VFS layer.
* Check for mandatory locks
*/
#ifndef __APPLE__
if (MANDMODE((mode_t)zp->z_phys->zp_mode)) {
if (error = chklock(vp, FREAD,
uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) {
ZFS_EXIT(zfsvfs);
return (error);
}
}
#endif /* !__APPLE__ */
/*
* If we're in FRSYNC mode, sync out this znode before reading it.
*/
if (ioflag & FRSYNC)
zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id);
/*
* Lock the range against changes.
*/
#ifdef __APPLE__
rl = zfs_range_lock(zp, uio_offset(uio), uio_resid(uio), RL_READER);
#else
rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER);
#endif /* __APPLE__ */
/*
* If we are reading past end-of-file we can skip
* to the end; but we might still need to set atime.
*/
#ifdef __APPLE__
if (uio_offset(uio) >= zp->z_phys->zp_size)
#else
if (uio->uio_loffset >= zp->z_phys->zp_size)
#endif /* __APPLE__ */
{
error = 0;
goto out;
}
#ifdef __APPLE__
ASSERT(uio_offset(uio) < zp->z_phys->zp_size);
n = MIN(uio_resid(uio), zp->z_phys->zp_size - uio_offset(uio));
#else
ASSERT(uio->uio_loffset < zp->z_phys->zp_size);
n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset);
#endif /* __APPLE__ */
while (n > 0) {
#ifdef __APPLE__
nbytes = MIN(n, zfs_read_chunk_size -
P2PHASE(uio_offset(uio), zfs_read_chunk_size));
#else
nbytes = MIN(n, zfs_read_chunk_size -
P2PHASE(uio->uio_loffset, zfs_read_chunk_size));
#endif /* __APPLE__ */
if (vn_has_cached_data(vp))
error = mappedread(vp, nbytes, uio);
else
error = dmu_read_uio(os, zp->z_id, uio, nbytes);
if (error)
break;
n -= nbytes;
}
out:
zfs_range_unlock(rl);
ZFS_ACCESSTIME_STAMP(zfsvfs, zp);
ZFS_EXIT(zfsvfs);
return (error);
}
#ifndef __APPLE__
// Prefault-writing isn't supported get - zfs_prefault_write is a no-op in zfs_context
/*
* Fault in the pages of the first n bytes specified by the uio structure.
* 1 byte in each page is touched and the uio struct is unmodified.
* Any error will exit this routine as this is only a best
* attempt to get the pages resident. This is a copy of ufs_trans_touch().
*/
static void
zfs_prefault_write(ssize_t n, struct uio *uio)
{
struct iovec *iov;
ulong_t cnt, incr;
caddr_t p;
uint8_t tmp;
iov = uio->uio_iov;
while (n) {
cnt = MIN(iov->iov_len, n);
if (cnt == 0) {
/* empty iov entry */
iov++;
continue;
}
n -= cnt;
/*
* touch each page in this segment.
*/
p = iov->iov_base;
while (cnt) {
switch (uio->uio_segflg) {
case UIO_USERSPACE:
case UIO_USERISPACE:
if (fuword8(p, &tmp))
return;
break;
case UIO_SYSSPACE:
if (kcopy(p, &tmp, 1))
return;
break;
}
incr = MIN(cnt, PAGESIZE);
p += incr;
cnt -= incr;
}
/*
* touch the last byte in case it straddles a page.
*/
p--;
switch (uio->uio_segflg) {
case UIO_USERSPACE:
case UIO_USERISPACE:
if (fuword8(p, &tmp))
return;
break;
case UIO_SYSSPACE:
if (kcopy(p, &tmp, 1))
return;
break;
}
iov++;
}
}
#endif /* !__APPLE__ */
/*
* Write the bytes to a file.
*
* IN: vp - vnode of file to be written to.
* uio - structure supplying write location, range info,
* and data buffer.
* ioflag - FAPPEND flag set if in append mode.
* cr - credentials of caller.
*
* OUT: uio - updated offset and range.
*
* RETURN: 0 if success
* error code if failure
*
* Timestamps:
* vp - ctime|mtime updated if byte count > 0
*/
/* ARGSUSED */
static int
#ifdef __APPLE__
zfs_vnop_write(struct vnop_write_args *ap)
#else
zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct)
#endif /* __APPLE__ */
{
#ifdef __APPLE__
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
int ioflag = ap->a_ioflag;
cred_t *cr = (cred_t *)vfs_context_ucred(ap->a_context);
#endif /* __APPLE__ */
znode_t *zp = VTOZ(vp);
#ifdef __APPLE__
rlim64_t limit = MAXOFFSET_T;
ssize_t start_resid = uio_resid(uio);
#else
rlim64_t limit = uio->uio_llimit;
ssize_t start_resid = uio->uio_resid;
#endif /* __APPLE__ */
ssize_t tx_bytes;
uint64_t end_size;
dmu_tx_t *tx;
zfsvfs_t *zfsvfs = zp->z_zfsvfs;
zilog_t *zilog = zfsvfs->z_log;
offset_t woff;
ssize_t n, nbytes;
rl_t *rl;
int max_blksz = zfsvfs->z_max_blksz;
int error;
/*
* Fasttrack empty write
*/
n = start_resid;
if (n == 0)
return (0);
if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
limit = MAXOFFSET_T;
ZFS_ENTER(zfsvfs);
/*
* Pre-fault the pages to ensure slow (eg NFS) pages
* don't hold up txg.
*/
zfs_prefault_write(n, uio);
/*
* If in append mode, set the io offset pointer to eof.
*
* Note: OSX uses IO_APPEND flag in order to indicate to
* append to a file as opposed to Solaris which uses the
* FAPPEND ioflag
*/
#ifdef __APPLE__
if (ioflag & IO_APPEND)
#else
if (ioflag & FAPPEND)
#endif /* __APPLE__ */
{
/*
* Range lock for a file append:
* The value for the start of range will be determined by
* zfs_range_lock() (to guarantee append semantics).
* If this write will cause the block size to increase,
* zfs_range_lock() will lock the entire file, so we must
* later reduce the range after we grow the block size.
*/
rl = zfs_range_lock(zp, 0, n, RL_APPEND);
if (rl->r_len == UINT64_MAX) {
/* overlocked, zp_size can't change */
#ifdef __APPLE__
woff = zp->z_phys->zp_size;
#else
woff = uio->uio_loffset = zp->z_phys->zp_size;
#endif /* __APPLE__ */
} else {
#ifdef __APPLE__
woff = rl->r_off;
#else
woff = uio->uio_loffset = rl->r_off;
#endif /* __APPLE__ */
}
#ifdef __APPLE__
uio_setoffset(uio, woff);
#endif
} else {
#ifdef __APPLE__
woff = uio_offset(uio);
#else
woff = uio->uio_loffset;
#endif /* __APPLE__ */
/*
* Validate file offset
*/
if (woff < 0) {
ZFS_EXIT(zfsvfs);
return (EINVAL);
}
/*
* If we need to grow the block size then zfs_range_lock()
* will lock a wider range than we request here.
* Later after growing the block size we reduce the range.
*/
rl = zfs_range_lock(zp, woff, n, RL_WRITER);
}
if (woff >= limit) {
zfs_range_unlock(rl);
ZFS_EXIT(zfsvfs);
return (EFBIG);
}
if ((woff + n) > limit || woff > (limit - n))
n = limit - woff;
/*
* Note: In Mac OS X, mandatory lock checking occurs up in VFS layer.
* Check for mandatory locks
*/
#ifndef __APPLE__
if (MANDMODE((mode_t)zp->z_phys->zp_mode) &&
(error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) {
zfs_range_unlock(rl);
ZFS_EXIT(zfsvfs);
return (error);
}
#endif /* !__APPLE__ */
end_size = MAX(zp->z_phys->zp_size, woff + n);
/*
* Write the file in reasonable size chunks. Each chunk is written
* in a separate transaction; this keeps the intent log records small
* and allows us to do more fine-grained space accounting.
*/
while (n > 0) {
/*
* Start a transaction.