summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/cachefs/cachefs_dir.c
blob: d77b4ab36ae290c17865edf4bc3ed2cae920cd12 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
/*
 * 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.
 */
#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/cred.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <sys/vfs.h>
#include <sys/vnode.h>
#include <sys/pathname.h>
#include <sys/uio.h>
#include <sys/tiuser.h>
#include <sys/sysmacros.h>
#include <sys/kmem.h>
#include <sys/ioctl.h>
#include <sys/statvfs.h>
#include <sys/errno.h>
#include <sys/debug.h>
#include <sys/cmn_err.h>
#include <sys/utsname.h>
#include <sys/modctl.h>
#include <sys/dirent.h>
#include <sys/fbuf.h>
#include <rpc/types.h>
#include <vm/seg.h>
#include <vm/faultcode.h>
#include <vm/hat.h>
#include <vm/seg_map.h>
#include <sys/fs/cachefs_fs.h>
#include <sys/fs/cachefs_dir.h>
#include <sys/fs/cachefs_log.h>

/* forward declarations */
static int cachefs_dir_getentrys(struct cnode *, u_offset_t, u_offset_t *,
    uint_t *, uint_t, caddr_t, int *);
static int cachefs_dir_stuff(cnode_t *dcp, uint_t count, caddr_t buf,
    vnode_t *frontvp, u_offset_t *offsetp, u_offset_t *fsizep);
static int cachefs_dir_extend(cnode_t *, u_offset_t *, int incr_frontblks);
static int cachefs_dir_fill_common(cnode_t *dcp, cred_t *cr,
    vnode_t *frontvp, vnode_t *backvp, u_offset_t *frontsize);
static int cachefs_dir_complete(fscache_t *fscp, vnode_t *backvp,
    vnode_t *frontvp, cred_t *cr, int acltoo);



/*
 * cachefs_dir_look() called mainly by lookup (and create), looks up the cached
 * directory for an entry and returns the information there. If the directory
 * entry doesn't exist return ENOENT, if it is incomplete, return EINVAL.
 * Should only call this routine if the dir is populated.
 * Returns ENOTDIR if dir gets nuked because of front file problems.
 */
int
cachefs_dir_look(cnode_t *dcp, char *nm, fid_t *cookiep, uint_t *flagp,
    u_offset_t *d_offsetp, cfs_cid_t *cidp)
{
	int error;
	struct vattr va;
	u_offset_t blockoff = 0LL;
	uint_t offset = 0; /* offset inside the block of size MAXBSIZE */
	vnode_t *dvp;
	struct fscache *fscp = C_TO_FSCACHE(dcp);
	cachefscache_t *cachep = fscp->fs_cache;
	int nmlen;
	struct fbuf *fbp;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_look: ENTER dcp %p nm %s\n", (void *)dcp,
									nm);
#endif
	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT(dcp->c_metadata.md_flags & MD_POPULATED);
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
		error = ENOTDIR;
		goto out;
	}

	dvp = dcp->c_frontvp;
	va.va_mask = AT_SIZE;		/* XXX should save dir size */
	error = VOP_GETATTR(dvp, &va, 0, kcred, NULL);
	if (error) {
		cachefs_inval_object(dcp);
		error = ENOTDIR;
		goto out;
	}

	ASSERT(va.va_size != 0LL);
	nmlen = (int)strlen(nm);
	while (blockoff < va.va_size) {
		offset = 0;
		error =
		    fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
		if (error)
			goto out;

		while (offset < MAXBSIZE && (blockoff + offset) < va.va_size) {
			struct c_dirent *dep;

			dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr +
								offset);
			if ((dep->d_flag & CDE_VALID) &&
				(nmlen == dep->d_namelen) &&
				strcmp(dep->d_name, nm) == 0) {
				if (dep->d_flag & CDE_COMPLETE) {
					if (cookiep) {
						CACHEFS_FID_COPY(&dep->d_cookie,
							cookiep);
					}
					if (flagp)
						*flagp = dep->d_flag;
					error = 0;
				} else {
					error = EINVAL;
				}
				if (cidp)
					*cidp = dep->d_id;
				if (d_offsetp)
					*d_offsetp = offset + blockoff;
				fbrelse(fbp, S_OTHER);
				goto out;
			}
			ASSERT(dep->d_length != 0);
			offset += dep->d_length;
		}
		fbrelse(fbp, S_OTHER);
		blockoff += MAXBSIZE;
	}
	error = ENOENT;

out:
	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_RFDIR))
		cachefs_log_rfdir(cachep, error, fscp->fs_cfsvfsp,
		    &dcp->c_metadata.md_cookie, dcp->c_id.cid_fileno, 0);
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("c_dir_look: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * creates a new directory and populates it with "." and ".."
 */
int
cachefs_dir_new(cnode_t *dcp, cnode_t *cp)
{
	int		error = 0;
	struct c_dirent	*dep;
	u_offset_t	size;
	int len;
	struct fbuf	*fbp;
#ifdef CFSDEBUG
	struct vattr	va;

	CFS_DEBUG(CFSDEBUG_DIR)
		printf("c_dir_new: ENTER dcp %p cp %p\n", (void *)dcp,
							(void *)cp);
#endif

	ASSERT(MUTEX_HELD(&cp->c_statelock));
	ASSERT(CTOV(cp)->v_type == VDIR);
	ASSERT((cp->c_flags & CN_ASYNC_POPULATE) == 0);
	ASSERT(CFS_ISFS_BACKFS_NFSV4(C_TO_FSCACHE(dcp)) == 0);

	if (cp->c_frontvp == NULL) {
		error = cachefs_getfrontfile(cp);
		if (error)
			goto out;
	}

#ifdef CFSDEBUG
	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(cp->c_frontvp, &va, 0, kcred, NULL);
	if (error)
		goto out;
	ASSERT(va.va_size == 0);
#endif

	/*
	 * Extend the directory by one MAXBSIZE chunk
	 */
	size = 0LL;
	error = cachefs_dir_extend(cp, &size, 1);
	if (error != 0)
		goto out;
	error = fbread(cp->c_frontvp, (offset_t)0, MAXBSIZE, S_OTHER, &fbp);
	if (error)
		goto out;

	/*
	 * Insert "." and ".."
	 */
	len = (int)CDE_SIZE(".");
	dep = (struct c_dirent *)fbp->fb_addr;
	dep->d_length = len;
	dep->d_offset = (offset_t)len;
	dep->d_flag = CDE_VALID | CDE_COMPLETE;
	CACHEFS_FID_COPY(&cp->c_cookie, &dep->d_cookie);
	dep->d_id = cp->c_id;
	dep->d_namelen = 1;
	bcopy(".", dep->d_name, 2);

	dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + len);
	dep->d_length = MAXBSIZE - len;
	dep->d_offset = MAXBSIZE;
	dep->d_flag = CDE_VALID | CDE_COMPLETE;
	CACHEFS_FID_COPY(&dcp->c_cookie, &dep->d_cookie);
	dep->d_id = dcp->c_id;
	dep->d_namelen = 2;
	bcopy("..", dep->d_name, 3);

	(void) fbdwrite(fbp);
#ifdef INVALREADDIR
	cp->c_metadata.md_flags |= MD_POPULATED | MD_INVALREADDIR;
#else
	cp->c_metadata.md_flags |= MD_POPULATED;
#endif
	cp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
out:
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_new: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * cachefs_dir_enter adds a new directory entry. Takes as input a fid,
 * fileno and a sync flag. Most of the time, the caller is content with the
 * write to the (front) directory being done async. The exception being - for
 * local files, we should make sure that the directory entry is made
 * synchronously. That is notified by the caller.
 * 		issync == 0 || issync == SM_ASYNC !
 *
 * The new entry is inserted at the end, so that we can generate local offsets
 * which are compatible with the backfs offsets (which are used when
 * disconnected.
 */
int
cachefs_dir_enter(cnode_t *dcp, char *nm, fid_t *cookiep, cfs_cid_t *cidp,
    int issync)
{
	struct vattr	va;
	int offset;
	u_offset_t blockoff = 0LL;
	u_offset_t	prev_offset;
	int		error = 0;
	vnode_t		*dvp;
	struct c_dirent	*dep;
	uint_t		esize;
	u_offset_t	dirsize;
	struct fbuf	*fbp;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("c_dir_enter: ENTER dcp %p nm %s dirflg %x\n",
			(void *)dcp, nm, dcp->c_metadata.md_flags);
#endif

	ASSERT(MUTEX_HELD(&dcp->c_statelock));
	ASSERT(dcp->c_metadata.md_flags & MD_POPULATED);
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);
	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT(issync == 0 || issync == SM_ASYNC);
	ASSERT(strlen(nm) <= MAXNAMELEN);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
		error = ENOTDIR;
		goto out;
	}
	dvp = dcp->c_frontvp;

	/*
	 * Get the current EOF for the directory(data file)
	 */
	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(dvp, &va, 0, kcred, NULL);
	if (error) {
		cachefs_inval_object(dcp);
		error = ENOTDIR;
		goto out;
	}

	/*
	 * Get the last block of the directory
	 */
	dirsize = va.va_size;
	ASSERT(dirsize != 0LL);
	ASSERT(!(dirsize & MAXBOFFSET));
	ASSERT(dirsize <= MAXOFF_T);
	blockoff = dirsize - MAXBSIZE;
	error = fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
	if (error)
		goto out;

	/*
	 * Find the last entry
	 */
	offset = 0;
	prev_offset = blockoff;
	for (;;) {
		dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + offset);
		if (offset + dep->d_length == MAXBSIZE)
			break;
		prev_offset = dep->d_offset;
		offset += dep->d_length;
		ASSERT(offset < MAXBSIZE);
	}
	esize = C_DIRSIZ(dep);

	if (dep->d_length - esize >= CDE_SIZE(nm)) {
		/*
		 * It has room. If the entry is not valid, we can just use
		 * it. Otherwise, we need to adjust its length and offset
		 */
#ifdef CFSDEBUG
		CFS_DEBUG(CFSDEBUG_DIR) {
			if (prev_offset >= dep->d_offset) {
				printf("cachefs_dir_enter: looks like "
				    "we might fail the assert\n");
				printf("addr %p, offset %x, "
				    "prev_offset %llx, dep->d_offset %llx\n",
				    (void *)fbp->fb_addr, offset, prev_offset,
				    dep->d_offset);
				offset = 0;
				prev_offset = blockoff;
				for (;;) {
					dep = (struct c_dirent *)
					    ((uintptr_t)fbp->fb_addr + offset);
					printf("offset %x, prev_offset %llx\n",
					    offset, prev_offset);
					printf("dep->d_offset %llx, "
					    "dep->d_length %x\n",
					    dep->d_offset, dep->d_length);
					if (offset + dep->d_length == MAXBSIZE)
						break;
					prev_offset = dep->d_offset;
					offset += dep->d_length;
				}
			}
		}
#endif /* CFSDEBUG */

		if (offset)
			ASSERT(prev_offset < dep->d_offset);
		if (dep->d_flag & CDE_VALID) {
			dep->d_length = esize;
			dep->d_offset = prev_offset + (u_offset_t)esize;
			dep = (struct c_dirent *)((uintptr_t)dep + esize);
		}
		dep->d_length = (int)((offset_t)MAXBSIZE -
				((uintptr_t)dep - (uintptr_t)fbp->fb_addr));
	} else {
		/*
		 * No room - so extend the file by one more
		 * MAXBSIZE chunk, and fit the entry there.
		 */
		fbrelse(fbp, S_OTHER);
		error = cachefs_dir_extend(dcp, &dirsize, 1);
		if (error != 0)
			goto out;
		error =
		    fbread(dvp, (offset_t)va.va_size, MAXBSIZE, S_OTHER, &fbp);
		if (error)
			goto out;
		dep = (struct c_dirent *)fbp->fb_addr;
		dep->d_length = MAXBSIZE;
	}

	/*
	 * Fill in the rest of the new entry
	 */
	dep->d_offset = dirsize;
	dep->d_flag = CDE_VALID;
	if (cookiep) {
		dep->d_flag |= CDE_COMPLETE;
		CACHEFS_FID_COPY(cookiep, &dep->d_cookie);
	}
	dep->d_id = *cidp;
	dep->d_namelen = (ushort_t)strlen(nm);
	(void) bcopy(nm, dep->d_name, dep->d_namelen + 1);

#ifdef INVALREADDIR
	dcp->c_metadata.md_flags |= MD_INVALREADDIR;
#endif
	dcp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
	if (issync)
		(void) fbwrite(fbp);
	else
		(void) fbdwrite(fbp);
out:
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_enter: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * Quite simple, if the deleted entry is the first in the MAXBSIZE block,
 * we simply mark it invalid. Otherwise, the deleted entries d_length is
 * just added to the previous entry.
 */
int
cachefs_dir_rmentry(cnode_t *dcp, char *nm)
{
	u_offset_t blockoff = 0LL;
	int offset = 0;
	struct vattr va;
	int error = ENOENT;
	vnode_t *dvp;
	int nmlen;
	struct fbuf *fbp;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_rmentry: ENTER dcp %p nm %s\n",
		    (void *)dcp, nm);
#endif
	ASSERT(dcp->c_metadata.md_flags & MD_POPULATED);
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
		error = ENOTDIR;
		goto out;
	}
	dvp = dcp->c_frontvp;

	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT((dcp->c_flags & CN_NOCACHE) == 0);
	ASSERT(dvp != NULL);
	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(dvp, &va, 0, kcred, NULL);
	if (error) {
		cachefs_inval_object(dcp);
		error = ENOTDIR;
		goto out;
	}
	ASSERT(va.va_size != 0LL);

	nmlen = (int)strlen(nm);
	while (blockoff < va.va_size) {
		uint_t *last_len;

		offset = 0;
		last_len = NULL;
		error =
		    fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
		if (error)
			goto out;
		while (offset < MAXBSIZE && (blockoff + offset) < va.va_size) {
			struct c_dirent *dep;

			dep  = (struct c_dirent *)((uintptr_t)fbp->fb_addr +
								offset);
			if ((dep->d_flag & CDE_VALID) &&
				(nmlen == dep->d_namelen) &&
				strcmp(dep->d_name, nm) == 0) {
				/*
				 * Found the entry. If this was the first entry
				 * in the MAXBSIZE block, Mark it invalid. Else
				 * add it's length to the previous entry's
				 * length.
				 */
				if (last_len == NULL) {
					ASSERT(offset == 0);
					dep->d_flag = 0;
				} else
					*last_len += dep->d_length;
				(void) fbdwrite(fbp);
				dcp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
				goto out;
			}
			last_len = &dep->d_length;
			offset += dep->d_length;
		}
		fbrelse(fbp, S_OTHER);
		blockoff += MAXBSIZE;
	}
	error = ENOENT;

out:
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_rmentry: EXIT error = %d\n", error);
#endif
	return (error);
}

#if 0
/*
 * This function is used only in cachefs_lookup_back() routine but
 * is inside #if 0 directive in this routine. So I am keeping this
 * routine also in #if 0 directive.
 */

/*
 * This function fills in the cookie and file no of the directory entry
 * at the offset specified by offset - In other words, makes the entry
 * "complete".
 */
int
cachefs_dir_modentry(cnode_t *dcp, u_offset_t offset, fid_t *cookiep,
    cfs_cid_t *cidp)
{
	struct c_dirent *dep;
	u_offset_t blockoff = (offset & (offset_t)MAXBMASK);
	uint_t off = (uint_t)(offset & (offset_t)MAXBOFFSET);
	struct fbuf *fbp;
	vnode_t *dvp;
	int error = 0;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_modentry: ENTER dcp %p offset %lld\n",
			(void *)dcp, offset);
#endif
	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT(dcp->c_metadata.md_flags & MD_POPULATED);
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
		return;
	}
	dvp = dcp->c_frontvp;

	error = fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
	if (error)
		goto out;
	dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
	if (cookiep) {
		dep->d_flag |= CDE_COMPLETE;
		CACHEFS_FID_COPY(cookiep, &dep->d_cookie);
	}
	if (cidp)
		dep->d_id = *cidp;
	(void) fbdwrite(fbp);
	dcp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;

out:
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_modentry: EXIT\n");
#endif
	return (error);
}

#endif  /* of #if 0 */

/*
 * Called by cachefs_read_dir(). Gets a bunch if directory entries into buf and
 * packs them into buf.
 */
static int
cachefs_dir_getentrys(struct cnode *dcp, u_offset_t beg_off,
		u_offset_t *last_offp, uint_t *cntp, uint_t bufsize,
				caddr_t buf, int *eofp)
{

#define	DIR_ENDOFF	0x7fffffffLL

	struct vattr va;
	struct c_dirent *dep;
	struct fbuf *fbp = NULL;
	struct dirent64 *gdp;
	u_offset_t blockoff;
	uint_t off;
	int error;
	vnode_t *dvp = dcp->c_frontvp;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
	printf("cachefs_dir_getentrys: "
	    "ENTER dcp %p beg_off %lld mdflags %x cflags %x\n",
	    dcp, beg_off, dcp->c_metadata.md_flags, dcp->c_flags);
#endif

	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	/*
	 * blockoff has the offset of the MAXBSIZE block that contains the
	 * entry  to start with. off contains the offset relative to the
	 * begining of the MAXBSIZE block.
	 */
	if (eofp)
		*eofp = 0;
	gdp = (struct dirent64 *)buf;
	*cntp = bufsize;
	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(dvp, &va, 0, kcred, NULL);
	if (error) {
		*cntp = 0;
		*last_offp = 0;
		if (eofp)
			*eofp = 1;
		goto out;
	}
	ASSERT(va.va_size != 0LL);

	if (beg_off == DIR_ENDOFF) {
		*cntp = 0;
		*last_offp = DIR_ENDOFF;
		if (eofp)
			*eofp = 1;
		goto out;
	}

	/*
	 * locate the offset where we start reading.
	 */
	for (blockoff = 0; blockoff < va.va_size; blockoff += MAXBSIZE) {
		error =
		    fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
		if (error)
			goto out;
		dep = (struct c_dirent *)fbp->fb_addr;
		off = 0;
		while (off < MAXBSIZE && dep->d_offset <= beg_off) {
			off += dep->d_length;
			dep =
			    (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
		}
		if (off < MAXBSIZE)
			break;
		fbrelse(fbp, S_OTHER);
		fbp = NULL;
	}

	if (blockoff >= va.va_size) {
		*cntp = 0;
		*last_offp = DIR_ENDOFF;
		if (eofp)
			*eofp = 1;
		goto out;
	}

	/*
	 * Just load up the buffer with directory entries.
	 */
	for (;;) {
		uint_t size;
		int this_reclen;

		ASSERT((uintptr_t)dep < ((uintptr_t)fbp->fb_addr + MAXBSIZE));
		if (dep->d_flag & CDE_VALID) {
			this_reclen = DIRENT64_RECLEN(dep->d_namelen);
			size = C_DIRSIZ(dep);
			ASSERT(size < MAXBSIZE);
			if (this_reclen > bufsize)
				break;
			ASSERT(dep->d_namelen <= MAXNAMELEN);
			ASSERT(dep->d_offset > (*last_offp));
			gdp->d_ino = dep->d_id.cid_fileno;
			gdp->d_off = dep->d_offset;

			/* use strncpy(9f) to zero out uninitialized bytes */

			ASSERT(strlen(dep->d_name) + 1 <=
			    DIRENT64_NAMELEN(this_reclen));
			(void) strncpy(gdp->d_name, dep->d_name,
			    DIRENT64_NAMELEN(this_reclen));

			gdp->d_reclen = (ushort_t)this_reclen;
			bufsize -= this_reclen;
			gdp = (struct dirent64 *)((uintptr_t)gdp +
				gdp->d_reclen);
			*last_offp = dep->d_offset;
		}

		/*
		 * Increment the offset. If we've hit EOF, fill in
		 * the lastoff and current entries d_off field.
		 */
		off += dep->d_length;
		ASSERT(off <= MAXBSIZE);
		if ((blockoff + off) >= va.va_size) {
			*last_offp = DIR_ENDOFF;
			if (eofp)
				*eofp = 1;
			break;
		}
		/*
		 * If off == MAXBSIZE, then we need to adjust our
		 * window to the next MAXBSIZE block of the directory.
		 * Adjust blockoff, off and map it in. Also, increment
		 * the directory and buffer pointers.
		 */
		if (off == MAXBSIZE) {
			fbrelse(fbp, S_OTHER);
			fbp = NULL;
			off = 0;
			blockoff += MAXBSIZE;
			error = fbread(dvp, (offset_t)blockoff, MAXBSIZE,
								S_OTHER, &fbp);
			if (error)
				goto out;
		}
		dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
	}
	*cntp -= bufsize;
out:
	/*
	 * Release any buffer and maping that may exist.
	 */
	if (fbp)
		(void) fbrelse(fbp, S_OTHER);
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("ccachefs_dir_getentrys: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * Called by cachefs_readdir(). Fills a directory request from the cache
 */
int
cachefs_dir_read(struct cnode *dcp, struct uio *uiop, int *eofp)
{
	int error;
	uint_t count;
	uint_t size;
	caddr_t buf;
	u_offset_t next = uiop->uio_loffset;
	struct fscache *fscp = C_TO_FSCACHE(dcp);
	cachefscache_t *cachep = fscp->fs_cache;
	caddr_t chrp, end;
	dirent64_t *de;

	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT(RW_READ_HELD(&dcp->c_rwlock));

	ASSERT(next <= MAXOFF_T);
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_read: ENTER dcp %p\n", (void *)dcp);
#endif
	ASSERT((dcp->c_metadata.md_flags & (MD_FILE|MD_POPULATED)) ==
	    (MD_FILE|MD_POPULATED));
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0) {
		error = ENOTDIR;
		goto out;
	}

	size = (uint_t)uiop->uio_resid;
	buf = cachefs_kmem_alloc(size, KM_SLEEP);
	error = cachefs_dir_getentrys(dcp, next, &next, &count, size,
	    buf, eofp);
	if (error == 0 && (int)count > 0) {
		ASSERT(count <= size);
		if (fscp->fs_inum_size > 0) {
			ino64_t newinum;

			mutex_exit(&dcp->c_statelock);
			mutex_enter(&fscp->fs_fslock);
			end = (caddr_t)((uintptr_t)buf + count);
			for (chrp = buf; chrp < end; chrp += de->d_reclen) {
				de = (dirent64_t *)chrp;

				newinum = cachefs_inum_real2fake(fscp,
				    de->d_ino);
				if (newinum == 0)
					newinum = cachefs_fileno_conflict(fscp,
					    de->d_ino);
				de->d_ino = newinum;
			}
			mutex_exit(&fscp->fs_fslock);
			mutex_enter(&dcp->c_statelock);
		}
		error = uiomove(buf, count, UIO_READ, uiop);
		if (error == 0)
			uiop->uio_loffset = next;
	}
	(void) cachefs_kmem_free(buf, size);
out:
	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_RFDIR))
		cachefs_log_rfdir(cachep, error, fscp->fs_cfsvfsp,
		    &dcp->c_metadata.md_cookie, dcp->c_id.cid_fileno, 0);
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_read: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * Fully (including cookie) populates the directory from the back filesystem.
 */
int
cachefs_dir_fill(cnode_t *dcp, cred_t *cr)
{
	int error = 0;
	u_offset_t frontsize;
	struct fscache *fscp = C_TO_FSCACHE(dcp);

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_fill: ENTER dcp %p\n", (void *)dcp);
#endif
	ASSERT(CFS_ISFS_BACKFS_NFSV4(fscp) == 0);
	ASSERT(MUTEX_HELD(&dcp->c_statelock));

	/* XXX for now return success if async populate is scheduled */
	if (dcp->c_flags & CN_ASYNC_POPULATE)
		goto out;

	/* get the back vp */
	if (dcp->c_backvp == NULL) {
		error = cachefs_getbackvp(fscp, dcp);
		if (error) {
			goto out;
		}
	}

	/* get the front file vp */
	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if (dcp->c_flags & CN_NOCACHE) {
		error = ENOTDIR;
		goto out;
	}

	/* if dir was modified, toss old contents */
	if (dcp->c_metadata.md_flags & MD_INVALREADDIR) {
		cachefs_inval_object(dcp);
		if (dcp->c_flags & CN_NOCACHE) {
			error = ENOTDIR;
			goto out;
		}
	}

	error = cachefs_dir_fill_common(dcp, cr,
	    dcp->c_frontvp, dcp->c_backvp, &frontsize);
	if (error == 0)
		error = cachefs_dir_complete(fscp, dcp->c_backvp,
		    dcp->c_frontvp, cr, 0);
	if (error != 0)
		goto out;

	/*
	 * Mark the directory as not empty. Also bang the flag that says that
	 * this directory needs to be sync'ed on inactive.
	 */
	dcp->c_metadata.md_flags |= MD_POPULATED;
	dcp->c_metadata.md_flags &= ~MD_INVALREADDIR;
	dcp->c_flags |= CN_UPDATED | CN_NEED_FRONT_SYNC;
	/*LINTED alignment okay*/
	dcp->c_metadata.md_frontblks = frontsize / MAXBSIZE;

out:
	if (error) {
#ifdef CFSDEBUG
		CFS_DEBUG(CFSDEBUG_INVALIDATE)
			printf("c_dir_fill: invalidating %llu\n",
			    (u_longlong_t)dcp->c_id.cid_fileno);
#endif
		cachefs_inval_object(dcp);
	}

	return (error);
}

/*
 * Does work of populating directory.
 * Must be called while holding dcp->c_statelock
 */

static int
cachefs_dir_fill_common(cnode_t *dcp, cred_t *cr,
    vnode_t *frontvp, vnode_t *backvp, u_offset_t *frontsize)
{
	int error = 0;
	struct uio uio;
	struct iovec iov;
	caddr_t buf = NULL;
	int count;
	int eof = 0;
	u_offset_t frontoff;
	struct fscache *fscp = C_TO_FSCACHE(dcp);
	cachefscache_t *cachep = fscp->fs_cache;
#ifdef DEBUG
	int loop_count = 0;
#endif
#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_fill_common: ENTER dcp %p\n", (void *)dcp);
#endif

	ASSERT(MUTEX_HELD(&dcp->c_statelock));

	frontoff = *frontsize = 0LL;

	buf = cachefs_kmem_alloc(MAXBSIZE, KM_SLEEP);
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_segflg = UIO_SYSSPACE;
	uio.uio_fmode = 0;
	uio.uio_extflg = UIO_COPY_CACHED;
	uio.uio_loffset = 0;
	for (;;) {
#ifdef DEBUG
		loop_count++;
#endif
		/*
		 * Read in a buffer's worth of dirents and enter them in to the
		 * directory.
		 */
		uio.uio_resid = MAXBSIZE;
		iov.iov_base = buf;
		iov.iov_len = MAXBSIZE;
		(void) VOP_RWLOCK(backvp, V_WRITELOCK_FALSE, NULL);
		error = VOP_READDIR(backvp, &uio, cr, &eof, NULL, 0);
		VOP_RWUNLOCK(backvp, V_WRITELOCK_FALSE, NULL);
		if (error)
			goto out;

		/*LINTED alignment okay*/
		count = MAXBSIZE - (int)uio.uio_resid;
		ASSERT(count >= 0);
		if (count > 0) {
			if (error = cachefs_dir_stuff(dcp, count, buf,
			    frontvp, &frontoff, frontsize))
				goto out;
			ASSERT((*frontsize) != 0LL);
		}
		if (eof || count == 0)
			break;
	}

	if (*frontsize == 0LL) {
		/* keep us from caching an empty directory */
		error = EINVAL;
		goto out;
	}

out:
	if (CACHEFS_LOG_LOGGING(cachep, CACHEFS_LOG_FILLDIR))
		cachefs_log_filldir(cachep, error, fscp->fs_cfsvfsp,
		    &dcp->c_metadata.md_cookie, dcp->c_id.cid_fileno,
		    *frontsize);
	if (buf)
		cachefs_kmem_free(buf, (uint_t)MAXBSIZE);

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_fill: EXIT error = %d\n", error);
#endif
	return (error);
}

/*
 * If the directory contains only the elements "." and "..", then this returns
 * 0, otherwise returns an error.
 */
int
cachefs_dir_empty(cnode_t *dcp)
{
	struct vattr va;
	u_offset_t blockoff = 0;
	int offset;
	struct fbuf *fbp;
	int error;
	vnode_t *dvp = dcp->c_frontvp;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_dir_empty: ENTER dcp %p\n", (void *)dcp);
#endif
	ASSERT(CTOV(dcp)->v_type == VDIR);
	ASSERT(dcp->c_metadata.md_flags & MD_POPULATED);
	ASSERT((dcp->c_flags & CN_ASYNC_POPULATE) == 0);

	if (dcp->c_frontvp == NULL)
		(void) cachefs_getfrontfile(dcp);
	if ((dcp->c_metadata.md_flags & MD_POPULATED) == 0)
		return (ENOTDIR);

	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(dvp, &va, 0, kcred, NULL);
	if (error)
		return (ENOTDIR);

	ASSERT(va.va_size != 0LL);
	while (blockoff < va.va_size) {
		offset = 0;
		error =
		    fbread(dvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
		if (error)
			return (error);
		while (offset < MAXBSIZE && (blockoff + offset) < va.va_size) {
			struct c_dirent *dep;

			dep = (struct c_dirent *)((uintptr_t)fbp->fb_addr +
								offset);
			if ((dep->d_flag & CDE_VALID) &&
				((strcmp(dep->d_name, ".") != 0) &&
				(strcmp(dep->d_name, "..") != 0))) {
				(void) fbrelse(fbp, S_OTHER);
				return (0);
			}
			offset += dep->d_length;
		}
		(void) fbrelse(fbp, S_OTHER);
		blockoff += MAXBSIZE;
	}
	return (EEXIST);
}

/*
 * Called by cachefs_dir_fill() to stuff a buffer of dir entries into
 * a front file.  This is more efficient than repeated calls to
 * cachefs_dir_enter, and it also allows us to maintain entries in backfs
 * order (readdir requires that entry offsets be ascending).
 */
static int
cachefs_dir_stuff(cnode_t *dcp, uint_t count, caddr_t buf,
    vnode_t *frontvp, u_offset_t *offsetp, u_offset_t *fsizep)
{
	int error = 0;
	struct fbuf *fbp;
	struct c_dirent *cdep, *last;
	struct dirent64 *dep;
	int inblk, entsize;
	u_offset_t blockoff = (*offsetp & (offset_t)MAXBMASK);
	/*LINTED alignment okay*/
	uint_t off = (uint_t)(*offsetp & (offset_t)MAXBOFFSET);

	/*LINTED want count != 0*/
	ASSERT(count > 0);

	if (*offsetp >= *fsizep) {
		error = cachefs_dir_extend(dcp, fsizep, 0);
		if (error)
			return (error);
	}

	ASSERT(*fsizep != 0LL);
	last = NULL;
	error = fbread(frontvp, (offset_t)blockoff, MAXBSIZE, S_OTHER, &fbp);
	if (error)
		return (error);
	cdep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
	inblk = MAXBSIZE-off;
	if (*offsetp != 0) {
		ASSERT(cdep->d_length == inblk);
		inblk -= C_DIRSIZ(cdep);
		last = cdep;
		last->d_length -= inblk;
		off += last->d_length;
		cdep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
	}
	dep = (struct dirent64 *)buf;
	/*LINTED want count != 0*/
	while (count > 0) {
		if (last) {
			ASSERT(dep->d_off > last->d_offset);
		}
		entsize = (int)CDE_SIZE(dep->d_name);
		if (entsize > inblk) {
			if (last) {
				last->d_length += inblk;
			}
			(void) fbwrite(fbp);
			error = cachefs_dir_extend(dcp, fsizep, 0);
			if (error)
				return (error);
			ASSERT(*fsizep != 0LL);
			blockoff += MAXBSIZE;
			error = fbread(frontvp, (offset_t)blockoff, MAXBSIZE,
							S_OTHER, &fbp);
			if (error)
				return (error);
			off = 0;
			cdep = (struct c_dirent *)fbp->fb_addr;
			inblk = MAXBSIZE;
			last = NULL;
		}
		cdep->d_length = entsize;
		cdep->d_id.cid_fileno = dep->d_ino;
		cdep->d_id.cid_flags = 0;
		cdep->d_namelen = (ushort_t)strlen(dep->d_name);
		cdep->d_flag = CDE_VALID;
		bcopy(dep->d_name, cdep->d_name, cdep->d_namelen+1);
		cdep->d_offset = dep->d_off;
		inblk -= entsize;
		count -= dep->d_reclen;
		dep = (struct dirent64 *)((uintptr_t)dep + dep->d_reclen);
		*offsetp = blockoff + (u_offset_t)off;
		off += entsize;
		last = cdep;
		cdep = (struct c_dirent *)((uintptr_t)fbp->fb_addr + off);
	}
	if (last) {
		last->d_length += inblk;
	}
	(void) fbwrite(fbp);
	return (error);
}

static int
cachefs_dir_extend(cnode_t *dcp, u_offset_t *cursize, int incr_frontblks)
{
	struct vattr va;
	cachefscache_t *cachep = C_TO_FSCACHE(dcp)->fs_cache;
	int error = 0;
	struct fscache *fscp = VFS_TO_FSCACHE(CTOV(dcp)->v_vfsp);

	ASSERT(MUTEX_HELD(&dcp->c_statelock));
	ASSERT(((*cursize) & (MAXBSIZE-1)) == 0);

	va.va_mask = AT_SIZE;
	va.va_size = (u_offset_t)(*cursize + MAXBSIZE);
	error = cachefs_allocblocks(cachep, 1, dcp->c_metadata.md_rltype);
	if (error)
		return (error);
	error = VOP_SETATTR(dcp->c_frontvp, &va, 0, kcred, NULL);
	if (error) {
		cachefs_freeblocks(cachep, 1, dcp->c_metadata.md_rltype);
		return (error);
	}
	if (incr_frontblks)
		dcp->c_metadata.md_frontblks++;
	if (fscp->fs_cdconnected != CFS_CD_CONNECTED) {
		dcp->c_size += MAXBSIZE;
		dcp->c_attr.va_size = dcp->c_size;
	}
	*cursize += MAXBSIZE;
	ASSERT(*cursize != 0LL);
	if (incr_frontblks)
		dcp->c_flags |= CN_UPDATED;
	return (0);
}

int
cachefs_async_populate_dir(struct cachefs_populate_req *pop, cred_t *cr,
    vnode_t *backvp, vnode_t *frontvp)
{
	vnode_t *dvp = pop->cpop_vp;
	struct cnode *dcp = VTOC(dvp);
	u_offset_t frontsize;
	int error = 0;

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_async_populate_dir: ENTER dvp %p\n",
								(void *)dvp);
#endif
	ASSERT(MUTEX_HELD(&dcp->c_statelock));
	ASSERT(dvp->v_type == VDIR);
	ASSERT((dcp->c_metadata.md_flags & MD_POPULATED) == 0);
	ASSERT(dcp->c_frontvp == frontvp);
	ASSERT(dcp->c_backvp == backvp);
	ASSERT(CFS_ISFS_BACKFS_NFSV4(C_TO_FSCACHE(dcp)) == 0);

	/* if dir was modified, toss old contents */
	if (dcp->c_metadata.md_flags & MD_INVALREADDIR) {
		cachefs_inval_object(dcp);
		if (dcp->c_flags & CN_NOCACHE) {
			error = ENOTDIR;
			goto out;
		} else {
			dcp->c_metadata.md_flags &= ~MD_INVALREADDIR;
		}
	}


	error = cachefs_dir_fill_common(dcp, cr, frontvp, backvp, &frontsize);
	if (error != 0)
		goto out;
	ASSERT(frontsize != 0LL);
	mutex_exit(&dcp->c_statelock);
	/*
	 * I don't like to break lock here but cachefs_dir_complete()
	 * needs it.
	 */
	error = cachefs_dir_complete(C_TO_FSCACHE(dcp), backvp,
	    frontvp, cr, 1);
	mutex_enter(&dcp->c_statelock);
	if (error != 0)
		goto out;
	/* if went nocache while lock was dropped, get out */
	if ((dcp->c_flags & CN_NOCACHE) || (dcp->c_frontvp == NULL)) {
		error = EINVAL;
	} else {
		/* allocfile and allocblocks have already happened. */
		dcp->c_metadata.md_frontblks = frontsize / MAXBSIZE;
	}

out:

#ifdef CFSDEBUG
	CFS_DEBUG(CFSDEBUG_DIR)
		printf("cachefs_async_populate_dir: EXIT error = %d\n", error);
#endif

	return (error);
}

static int
cachefs_dir_complete(fscache_t *fscp, vnode_t *backvp, vnode_t *frontvp,
    cred_t *cr, int acltoo)
{
	struct c_dirent *dep;
	caddr_t buf = kmem_alloc(MAXBSIZE, KM_SLEEP);
	struct vattr va;
	u_offset_t blockoff;
	int offset;
	u_offset_t dir_size;
	struct fbuf *fbp;
	cnode_t *cp;
	fid_t cookie;
	vnode_t *entry_vp;
	int error = 0;

	/*
	 * note: caller better not hold a c_statelock if acltoo is set.
	 */

	va.va_mask = AT_SIZE;
	error = VOP_GETATTR(frontvp, &va, 0, cr, NULL);
	if (error)
		goto out;

	ASSERT(va.va_size != 0LL);
	dir_size = va.va_size;
	ASSERT(dir_size <= MAXOFF_T);

	for (blockoff = 0; blockoff < dir_size; blockoff += MAXBSIZE) {
		if (error = fbread(frontvp, (offset_t)blockoff,
		    MAXBSIZE, S_OTHER, &fbp))
			goto out;

		/*
		 * We cannot hold any page locks across the below VOP
		 * operations. We thus copy the directory entries into a
		 * staging buffer, and release the page lock on the directory
		 * by calling fbrelse().  Once any necessary cnodes have
		 * been created, we'll reacquire the page lock with fbread()
		 * and copy the staging buffer back into the frontvp at
		 * blockoff.
		 */
		bcopy(fbp->fb_addr, buf, MAXBSIZE);
		fbrelse(fbp, S_OTHER);

		for (offset = 0;
		    offset < MAXBSIZE &&
		    (blockoff + (u_offset_t)offset) < dir_size;
		    offset += dep->d_length) {

			dep = (struct c_dirent *)((uintptr_t)buf + offset);
			ASSERT(dep->d_length != 0);
			if ((dep->d_flag & (CDE_VALID | CDE_COMPLETE)) !=
			    CDE_VALID)
				continue;

			error = VOP_LOOKUP(backvp, dep->d_name,
			    &entry_vp, (struct pathname *)NULL, 0,
			    (vnode_t *)NULL, cr, NULL, NULL, NULL);
			if (error) {
				/* lookup on .. in / on coc gets ENOENT */
				if (error == ENOENT) {
					error = 0;
					continue;
				}
				goto out;
			}

			error = cachefs_getcookie(entry_vp, &cookie, NULL, cr,
				TRUE);
			if (error) {
#ifdef CFSDEBUG
				CFS_DEBUG(CFSDEBUG_DIR)
					printf("\t%s: getcookie error\n",
					    dep->d_name);
#endif /* CFSDEBUG */
				VN_RELE(entry_vp);
				goto out;
			}
			CACHEFS_FID_COPY(&cookie, &dep->d_cookie);
			dep->d_flag |= CDE_COMPLETE;

			if ((! acltoo) ||
			    (! cachefs_vtype_aclok(entry_vp)) ||
			    (fscp->fs_info.fi_mntflags & CFS_NOACL)) {
				VN_RELE(entry_vp);
				continue;
			}

			error = cachefs_cnode_make(&dep->d_id, fscp, &cookie,
			    NULL, entry_vp, cr, 0, &cp);
			VN_RELE(entry_vp);
			if (error != 0)
				goto out;

			ASSERT(cp != NULL);
			mutex_enter(&cp->c_statelock);

			if ((cp->c_flags & CN_NOCACHE) ||
			    (cp->c_metadata.md_flags & MD_ACL)) {
				mutex_exit(&cp->c_statelock);
				VN_RELE(CTOV(cp));
				continue;
			}

			(void) cachefs_cacheacl(cp, NULL);
			mutex_exit(&cp->c_statelock);
			VN_RELE(CTOV(cp));
		}

		/*
		 * We must now re-lock the page corresponding to the frontvp,
		 * and copy our staging buffer onto it.
		 */
		if (error = fbread(frontvp, (offset_t)blockoff,
		    MAXBSIZE, S_OTHER, &fbp))
			goto out;

		bcopy(buf, fbp->fb_addr, MAXBSIZE);
		(void) fbdwrite(fbp);
	}

out:
	kmem_free(buf, MAXBSIZE);
	return (error);
}