summaryrefslogtreecommitdiffstats
path: root/support/junction/nfs.c
blob: 73e3533bbc8b7bcdbf3c4672aa1204c121e702f9 (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
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
/**
 * @file support/junction/nfs.c
 * @brief Create, delete, and read NFS junctions on the local file system
 */

/*
 * Copyright 2011, 2018 Oracle.  All rights reserved.
 *
 * This file is part of nfs-utils.
 *
 * nfs-utils is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2.0 as
 * published by the Free Software Foundation.
 *
 * nfs-utils is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License version 2.0 for more details.
 *
 * You should have received a copy of the GNU General Public License
 * version 2.0 along with nfs-utils.  If not, see:
 *
 *	http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

/*
 * An NFS junction is a list of NFS FSLs, represented in a well-formed XML
 * document:
 *
 * <?xml version="1.0" encoding="UTF-8"?>
 * <junction>
 *   <savedmode bits="1777" />
 *   <fileset>
 *     <location>
 *       <host name="fileserver.example.net" port="2049" />
 *       <path>
 *         <component>foo</component>
 *         <component>bar</component>
 *         <component>baz</component>
 *       </path>
 *       <currency>-1</currency>
 *       <genflags writable="false" going="false" split="true" />
 *       <transflags rdma="true" />
 *       <class simul="0" handle="0" fileid="0"
 *              writever="0" change="0" readdir="0" />
 *       <read rank="0" order="0" />
 *       <write rank="0" order="0" />
 *       <flags varsub="false" />
 *       <validfor>0</validfor>
 *     </location>
 *
 *     ....
 *
 *   </fileset>
 * </junction>
 *
 * NFS junction XML is stored in an extended attribute called
 * "trusted.junction.nfs".   The parent object is a directory.
 *
 * To help file servers discover junctions efficiently, the directory
 * has no execute bits, and the sticky bit is set.  In addition, an
 * extended attribute called "trusted.junction.type" is added.  The
 * contents are ignored in user space.
 *
 * Finally, for pre-existing directories that are converted to
 * junctions, their mode bits are saved in an extended attribute called
 * "trusted.junction.mode".  When the junction data is removed, the
 * directory's mode bits are restored from this information.
 */

#include <sys/types.h>
#include <sys/stat.h>

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <rpcsvc/nfs_prot.h>

#include "junction.h"
#include "junction-internal.h"
#include "xlog.h"

/**
 * Tag name of NFS location element of a junction XML document
 */
#define NFS_XML_LOCATION_TAG		(const xmlChar *)"location"

/**
 * Tag name of host child element of an NFS location element
 */
#define NFS_XML_HOST_TAG		(const xmlChar *)"host"

/**
 * Name of hostname attribute of a host element
 */
#define NFS_XML_HOST_NAME_ATTR		(const xmlChar *)"name"

/**
 * Name of IP port attribute of a host element
 */
#define NFS_XML_HOST_PORT_ATTR		(const xmlChar *)"port"

/**
 * Tag name of path child element of an NFS location element
 */
#define NFS_XML_PATH_TAG		(const xmlChar *)"path"

/**
 * Tag name of component child element of a path element
 */
#define NFS_XML_COMPONENT_TAG		(const xmlChar *)"component"

/**
 * Tag name of currency child element of an NFS location element
 */
#define NFS_XML_CURRENCY_TAG		(const xmlChar *)"currency"

/**
 * Tag name of genflags child element of an NFS location element
 */
#define NFS_XML_GENFLAGS_TAG		(const xmlChar *)"genflags"

/**
 * Name of writable attribute of a genflags element
 */
#define NFS_XML_GENFLAGS_WRITABLE_ATTR	(const xmlChar *)"writable"

/**
 * Name of going attribute of a genflags element
 */
#define NFS_XML_GENFLAGS_GOING_ATTR	(const xmlChar *)"going"

/**
 * Name of split attribute of a genflags element
 */
#define	NFS_XML_GENFLAGS_SPLIT_ATTR	(const xmlChar *)"split"

/**
 * Tag name of transflags child element of an NFS location element
 */
#define NFS_XML_TRANSFLAGS_TAG		(const xmlChar *)"transflags"

/**
 * Name of rdma attribute of a transflags element
 */
#define NFS_XML_TRANSFLAGS_RDMA_ATTR	(const xmlChar *)"rdma"

/**
 * Tag name of class child element of an NFS location element
 */
#define NFS_XML_CLASS_TAG		(const xmlChar *)"class"

/**
 * Name of simul attribute of a class element
 */
#define NFS_XML_CLASS_SIMUL_ATTR	(const xmlChar *)"simul"

/**
 * Name of handle attribute of a class element
 */
#define NFS_XML_CLASS_HANDLE_ATTR	(const xmlChar *)"handle"

/**
 * Name of fileid attribute of a class element
 */
#define NFS_XML_CLASS_FILEID_ATTR	(const xmlChar *)"fileid"

/**
 * Name of writever attribute of a class element
 */
#define NFS_XML_CLASS_WRITEVER_ATTR	(const xmlChar *)"writever"

/**
 * Name of change attribute of a class element
 */
#define NFS_XML_CLASS_CHANGE_ATTR	(const xmlChar *)"change"

/**
 * Name of readdir attribute of a class element
 */
#define NFS_XML_CLASS_READDIR_ATTR	(const xmlChar *)"readdir"

/**
 * Tag name of read child element of an NFS location element
 */
#define NFS_XML_READ_TAG		(const xmlChar *)"read"

/**
 * Name of rank attribute of a read element
 */
#define NFS_XML_READ_RANK_ATTR		(const xmlChar *)"rank"

/**
 * Name of order attribute of a read element
 */
#define NFS_XML_READ_ORDER_ATTR		(const xmlChar *)"order"

/**
 * Tag name of write attribute of an NFS location element
 */
#define NFS_XML_WRITE_TAG		(const xmlChar *)"write"

/**
 * Name of rank attribute of a write element
 */
#define NFS_XML_WRITE_RANK_ATTR		(const xmlChar *)"rank"

/**
 * Name of order attribute of a write element
 */
#define NFS_XML_WRITE_ORDER_ATTR	(const xmlChar *)"order"

/**
 * Tag name of flags child element of an NFS location element
 */
#define NFS_XML_FLAGS_TAG		(const xmlChar *)"flags"

/**
 * Name of varsub attribute of a flags element
 */
#define NFS_XML_FLAGS_VARSUB_ATTR	(const xmlChar *)"varsub"

/**
 * Tag name of a validfor child element of an NFS location element
 */
#define NFS_XML_VALIDFOR_TAG		(const xmlChar *)"validfor"

/**
 * XPath path to NFS location elements in a junction document
 */
#define NFS_XML_LOCATION_XPATH		(const xmlChar *)	\
						"/junction/fileset/location"


/**
 * Remove all NFS-related xattrs from a directory
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
static FedFsStatus
nfs_remove_locations(const char *pathname)
{
	FedFsStatus retval;
	int fd;

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_remove_xattr(fd, pathname, JUNCTION_XATTR_NAME_NFS);

	(void)close(fd);
	return retval;
}

/**
 * Add a "host" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_host_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	uint16_t port = fsloc->nfl_hostport;
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_HOST_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add host element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	xmlSetProp(new, NFS_XML_HOST_NAME_ATTR,
			(const xmlChar *)fsloc->nfl_hostname);
	if (port != NFS_PORT && port != 0)
		junction_xml_set_int_attribute(new, NFS_XML_HOST_PORT_ATTR,
									port);

	return FEDFS_OK;
}

/**
 * Add a "path" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_path_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;
	int i;

	new = xmlNewTextChild(parent, NULL, NFS_XML_PATH_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add path element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	for (i = 0; fsloc->nfl_rootpath[i] != NULL; i++) {
		xmlNodePtr component;

		component = xmlNewTextChild(new , NULL,
						NFS_XML_COMPONENT_TAG,
						(const xmlChar *)
						fsloc->nfl_rootpath[i]);
		if (component == NULL) {
			xlog(D_GENERAL, "%s: Failed to add component "
					"element for %s",
				__func__, pathname);
			return FEDFS_ERR_SVRFAULT;
		}
	}

	return FEDFS_OK;
}

/**
 * Add a "currency" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_currency_xml(__attribute__((unused)) const char *pathname,
		xmlNodePtr parent, struct nfs_fsloc *fsloc)
{
	if (junction_xml_set_int_content(parent, NFS_XML_CURRENCY_TAG,
						fsloc->nfl_currency) == NULL)
		return FEDFS_ERR_SVRFAULT;
	return FEDFS_OK;
}

/**
 * Add a "genflags" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_genflags_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_GENFLAGS_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add genflags element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_bool_attribute(new, NFS_XML_GENFLAGS_WRITABLE_ATTR,
					fsloc->nfl_genflags.nfl_writable);
	junction_xml_set_bool_attribute(new, NFS_XML_GENFLAGS_GOING_ATTR,
					fsloc->nfl_genflags.nfl_going);
	junction_xml_set_bool_attribute(new, NFS_XML_GENFLAGS_SPLIT_ATTR,
					fsloc->nfl_genflags.nfl_split);

	return FEDFS_OK;
}

/**
 * Add a "transflags" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_transflags_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_TRANSFLAGS_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add transflags element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_bool_attribute(new, NFS_XML_TRANSFLAGS_RDMA_ATTR,
					fsloc->nfl_transflags.nfl_rdma);

	return FEDFS_OK;
}

/**
 * Add a "class" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_class_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_CLASS_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add class element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_int_attribute(new, NFS_XML_CLASS_SIMUL_ATTR,
						fsloc->nfl_info.nfl_simul);
	junction_xml_set_int_attribute(new, NFS_XML_CLASS_HANDLE_ATTR,
						fsloc->nfl_info.nfl_handle);
	junction_xml_set_int_attribute(new, NFS_XML_CLASS_FILEID_ATTR,
						fsloc->nfl_info.nfl_fileid);
	junction_xml_set_int_attribute(new, NFS_XML_CLASS_WRITEVER_ATTR,
						fsloc->nfl_info.nfl_writever);
	junction_xml_set_int_attribute(new, NFS_XML_CLASS_CHANGE_ATTR,
						fsloc->nfl_info.nfl_change);
	junction_xml_set_int_attribute(new, NFS_XML_CLASS_READDIR_ATTR,
						fsloc->nfl_info.nfl_readdir);

	return FEDFS_OK;
}

/**
 * Add a "read" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_read_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_READ_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add read element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_int_attribute(new, NFS_XML_READ_RANK_ATTR,
					fsloc->nfl_info.nfl_readrank);
	junction_xml_set_int_attribute(new, NFS_XML_READ_ORDER_ATTR,
					fsloc->nfl_info.nfl_readorder);

	return FEDFS_OK;
}

/**
 * Add a "write" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_write_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_WRITE_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add write element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_int_attribute(new, NFS_XML_WRITE_RANK_ATTR,
					fsloc->nfl_info.nfl_writerank);
	junction_xml_set_int_attribute(new, NFS_XML_WRITE_ORDER_ATTR,
					fsloc->nfl_info.nfl_writeorder);

	return FEDFS_OK;
}

/**
 * Add a "flags" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_flags_xml(const char *pathname, xmlNodePtr parent,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr new;

	new = xmlNewTextChild(parent, NULL, NFS_XML_FLAGS_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add flags element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	junction_xml_set_bool_attribute(new, NFS_XML_FLAGS_VARSUB_ATTR,
					fsloc->nfl_flags.nfl_varsub);

	return FEDFS_OK;
}

/**
 * Add a "validfor" child to a "location" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param parent parent element to which to add "host" child
 * @param fsloc NFS location containing host information to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_validfor_xml(__attribute__((unused)) const char *pathname,
		xmlNodePtr parent, struct nfs_fsloc *fsloc)
{
	if (junction_xml_set_int_content(parent, NFS_XML_VALIDFOR_TAG,
						fsloc->nfl_validfor) == NULL)
		return FEDFS_ERR_SVRFAULT;
	return FEDFS_OK;
}

/**
 * Construct and add one "location" element to a "fileset"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param fileset fileset element of junction XML parse tree
 * @param fsloc one NFS location to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_location_xml(const char *pathname, xmlNodePtr fileset,
		struct nfs_fsloc *fsloc)
{
	FedFsStatus retval;
	xmlNodePtr new;

	new = xmlNewTextChild(fileset, NULL, NFS_XML_LOCATION_TAG, NULL);
	if (new == NULL) {
		xlog(D_GENERAL, "%s: Failed to add location element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	retval = nfs_location_host_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_path_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_currency_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_genflags_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_transflags_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_class_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_read_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_write_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_location_flags_xml(pathname, new, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	return nfs_location_validfor_xml(pathname, new, fsloc);
}

/**
 * Construct and add a "fileset" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param root root element of junction XML parse tree
 * @param fslocs list of NFS locations to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_fileset_xml(const char *pathname, xmlNodePtr root,
		struct nfs_fsloc *fslocs)
{
	struct nfs_fsloc *next;
	xmlNodePtr fileset;
	FedFsStatus retval;

	fileset = xmlNewTextChild(root, NULL, JUNCTION_XML_FILESET_TAG, NULL);
	if (fileset == NULL) {
		xlog(D_GENERAL, "%s: Failed to add fileset element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	for (next = fslocs; next != NULL; next = next->nfl_next) {
		retval = nfs_location_xml(pathname, fileset, next);
		if (retval != FEDFS_OK)
			return retval;
	}

	return FEDFS_OK;
}

/**
 * Construct a "savedmode" element
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param root root element of XML document tree
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_savedmode_xml(const char *pathname, xmlNodePtr root)
{
	xmlNodePtr savedmode;
	FedFsStatus retval;
	mode_t mode;
	char buf[8];

	retval = junction_get_mode(pathname, &mode);
	if (retval != FEDFS_OK)
		return retval;

	savedmode = xmlNewTextChild(root, NULL, JUNCTION_XML_SAVEDMODE_TAG, NULL);
	if (savedmode == NULL) {
		xlog(D_GENERAL, "%s: Failed to add savedmode element for %s\n",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	(void)snprintf(buf, sizeof(buf), "%o", ALLPERMS & mode);
	xmlSetProp(savedmode, JUNCTION_XML_MODEBITS_ATTR, (const xmlChar *)buf);

	return FEDFS_OK;
}

/**
 * Construct NFS junction XML document from list of NFS locations
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param doc an XML parse tree in which to construct the junction XML document
 * @param fslocs list of NFS locations to add
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_junction_xml(const char *pathname, xmlDocPtr doc,
		struct nfs_fsloc *fslocs)
{
	FedFsStatus retval;
	xmlNodePtr root;

	root = xmlNewNode(NULL, JUNCTION_XML_ROOT_TAG);
	if (root == NULL) {
		xlog(D_GENERAL, "%s: Failed to create root element for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}
	(void)xmlDocSetRootElement(doc, root);

	retval = nfs_savedmode_xml(pathname, root);
	if (retval != FEDFS_OK)
		return retval;

	return nfs_fileset_xml(pathname, root, fslocs);
}

/**
 * Write NFS locations information into an NFS junction extended attribute
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param doc an empty XML parse tree in which to construct the junction XML document
 * @param fslocs list of NFS locations to add
 * @return a FedFsStatus code
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
static FedFsStatus
nfs_write_junction(const char *pathname, xmlDocPtr doc,
		struct nfs_fsloc *fslocs)
{
	FedFsStatus retval;

	retval = nfs_junction_xml(pathname, doc, fslocs);
	if (retval != FEDFS_OK)
		return retval;

	return junction_xml_write(pathname, JUNCTION_XATTR_NAME_NFS, doc);
}

/**
 * Store NFS locations information into a junction object
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param fslocs list of NFS locations to add
 * @return a FedFsStatus code
 *
 * @note Access to trusted attributes requires CAP_SYS_ADMIN.
 */
static FedFsStatus
nfs_store_locations(const char *pathname, struct nfs_fsloc *fslocs)
{
	FedFsStatus retval;
	xmlDocPtr doc;

	doc = xmlNewDoc((xmlChar *)"1.0");
	if (doc == NULL) {
		xlog(D_GENERAL, "%s: Failed to create XML doc for %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	retval = nfs_write_junction(pathname, doc, fslocs);

	xmlFreeDoc(doc);
	return retval;
}

/**
 * Add NFS junction information to a pre-existing object
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param fslocs list of NFS locations to add
 * @return a FedFsStatus code
 *
 * An error occurs if the object referred to by "pathname" does not
 * exist or contains existing junction data.
 */
FedFsStatus
nfs_add_junction(const char *pathname, struct nfs_fsloc *fslocs)
{
	FedFsStatus retval;

	if (fslocs == NULL)
		return FEDFS_ERR_INVAL;

	retval = nfs_is_prejunction(pathname);
	if (retval != FEDFS_ERR_NOTJUNCT)
		return retval;

	retval = nfs_store_locations(pathname, fslocs);
	if (retval != FEDFS_OK)
		goto out_err;

	retval = junction_save_mode(pathname);
	if (retval != FEDFS_OK)
		goto out_err;

	return retval;

out_err:
	(void)nfs_remove_locations(pathname);
	return retval;
}

/**
 * Remove NFS junction information from an object
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 *
 * An error occurs if the object referred to by "pathname" does not
 * exist or does not contain NFS junction data.
 */
FedFsStatus
nfs_delete_junction(const char *pathname)
{
	FedFsStatus retval;

	retval = nfs_is_junction(pathname);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_restore_mode(pathname);
	if (retval != FEDFS_OK)
		return retval;

	return nfs_remove_locations(pathname);
}

/**
 * Parse the first "host" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_host(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	FedFsStatus retval;
	xmlChar *hostname;
	xmlNodePtr node;
	int hostport;

	retval = FEDFS_ERR_NOTJUNCT;
	node = junction_xml_find_child_by_name(location, NFS_XML_HOST_TAG);
	if (node == NULL)
		return retval;

	hostname = xmlGetProp(node, NFS_XML_HOST_NAME_ATTR);
	if (!junction_xml_get_int_attribute(node, NFS_XML_HOST_PORT_ATTR,
							&hostport))
		fsloc->nfl_hostport = NFS_PORT;
	else {
		if (hostport < 1 || hostport > UINT16_MAX) {
			xlog(D_GENERAL, "%s: Bad port attribute on %s",
				__func__, pathname);
			goto out;
		}
		fsloc->nfl_hostport = (uint16_t)hostport;
	}
	if (hostname == NULL) {
		xlog(D_GENERAL, "%s: No hostname attribute on %s",
			__func__, pathname);
		goto out;
	}
	fsloc->nfl_hostname = strdup((const char *)hostname);
	if (fsloc->nfl_hostname == NULL) {
		retval = FEDFS_ERR_SVRFAULT;
		goto out;
	}

	retval = FEDFS_OK;

out:
	xmlFree(hostname);
	return retval;
}

/**
 * Parse the first "path" child of "location" into a path array
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_path(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node, component;
	unsigned int count;
	xmlChar *value;
	char **result;

	node = junction_xml_find_child_by_name(location, NFS_XML_PATH_TAG);
	if (node == NULL)
		return FEDFS_ERR_NOTJUNCT;

	count = 0;
	for (component = node->children;
	     component != NULL;
	     component = component->next) {
		if (!junction_xml_match_node_name(component,
						NFS_XML_COMPONENT_TAG))
			continue;
		value = xmlNodeGetContent(component);
		if (junction_xml_is_empty(value)) {
			xlog(D_GENERAL, "%s: Bad pathname component in %s",
				__func__, pathname);
			return FEDFS_ERR_NOTJUNCT;
		}
		xmlFree(value);
		count++;
	}
	xlog(D_GENERAL, "%s: Found %u component(s)", __func__, count);

	if (count == 0) {
		xlog(D_GENERAL, "%s: Zero-component pathname", __func__);
		fsloc->nfl_rootpath = (char **)calloc(1, sizeof(char *));
		if (fsloc->nfl_rootpath == NULL)
			return FEDFS_ERR_SVRFAULT;
		fsloc->nfl_rootpath[0] = NULL;
		return FEDFS_OK;
	}

	result = calloc(count + 1, sizeof(char *));
	if (result == NULL)
		return FEDFS_ERR_SVRFAULT;

	count = 0;
	for (component = node->children;
	     component != NULL;
	     component = component->next) {
		if (!junction_xml_match_node_name(component,
						NFS_XML_COMPONENT_TAG))
			continue;
		value = xmlNodeGetContent(component);
		result[count] = strdup((const char *)value);
		xmlFree(value);
		if (result[count] == NULL) {
			nfs_free_string_array(result);
			return FEDFS_ERR_SVRFAULT;
		}
		count++;
	}

	fsloc->nfl_rootpath = result;
	return FEDFS_OK;
}

/**
 * Parse the first "currency" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_currency(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_CURRENCY_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_int_content(node, &fsloc->nfl_currency))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid currency element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "genflags" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_genflags(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_GENFLAGS_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_bool_attribute(node,
					NFS_XML_GENFLAGS_WRITABLE_ATTR,
					&fsloc->nfl_genflags.nfl_writable))
		goto out_err;
	if (!junction_xml_get_bool_attribute(node,
					NFS_XML_GENFLAGS_GOING_ATTR,
					&fsloc->nfl_genflags.nfl_going))
		goto out_err;
	if (!junction_xml_get_bool_attribute(node,
					NFS_XML_GENFLAGS_SPLIT_ATTR,
					&fsloc->nfl_genflags.nfl_split))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid genflags element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "transflags" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_transflags(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_TRANSFLAGS_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_bool_attribute(node,
					NFS_XML_TRANSFLAGS_RDMA_ATTR,
					&fsloc->nfl_transflags.nfl_rdma))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid transflags element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "class" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_class(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_CLASS_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_SIMUL_ATTR,
					&fsloc->nfl_info.nfl_simul))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_HANDLE_ATTR,
					&fsloc->nfl_info.nfl_handle))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_FILEID_ATTR,
					&fsloc->nfl_info.nfl_fileid))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_WRITEVER_ATTR,
					&fsloc->nfl_info.nfl_writever))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_WRITEVER_ATTR,
					&fsloc->nfl_info.nfl_writever))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_CHANGE_ATTR,
					&fsloc->nfl_info.nfl_change))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_CLASS_READDIR_ATTR,
					&fsloc->nfl_info.nfl_readdir))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid class element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "read" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_read(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_READ_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_READ_RANK_ATTR,
					&fsloc->nfl_info.nfl_readrank))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_READ_ORDER_ATTR,
					&fsloc->nfl_info.nfl_readorder))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid read element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "write" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_write(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_WRITE_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_WRITE_RANK_ATTR,
					&fsloc->nfl_info.nfl_writerank))
		goto out_err;
	if (!junction_xml_get_u8_attribute(node,
					NFS_XML_WRITE_ORDER_ATTR,
					&fsloc->nfl_info.nfl_writeorder))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid write element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "flags" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_flags(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_FLAGS_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_bool_attribute(node,
					NFS_XML_FLAGS_VARSUB_ATTR,
					&fsloc->nfl_flags.nfl_varsub))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid flags element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse the first "validfor" child of "location"
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 */
static FedFsStatus
nfs_parse_location_validfor(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	xmlNodePtr node;

	node = junction_xml_find_child_by_name(location, NFS_XML_VALIDFOR_TAG);
	if (node == NULL)
		goto out_err;

	if (!junction_xml_get_int_content(node, &fsloc->nfl_validfor))
		goto out_err;

	return FEDFS_OK;

out_err:
	xlog(D_GENERAL, "%s: Missing or invalid validfor element in %s",
		__func__, pathname);
	return FEDFS_ERR_NOTJUNCT;
}

/**
 * Parse children of NFS location element in an NFS junction
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc a blank nfs_fsloc to fill in
 * @return a FedFsStatus code
 *
 * All children are required only-once elements, and may appear in any order.
 * Extraneous or repeated elements are ignored for now.
 */
static FedFsStatus
nfs_parse_location_children(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc *fsloc)
{
	FedFsStatus retval;

	retval = nfs_parse_location_host(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_path(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_currency(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_genflags(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_transflags(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_class(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_read(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_write(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	retval = nfs_parse_location_flags(pathname, location, fsloc);
	if (retval != FEDFS_OK)
		return retval;
	return nfs_parse_location_validfor(pathname, location, fsloc);
}

/**
 * Parse NFS location element in an NFS junction
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param location XML parse tree containing fileset location element
 * @param fsloc OUT: a single NFS location item
 * @return a FedFsStatus code
 *
 * If nfs_parse_location() returns FEDFS_OK, caller must free the returned
 * location with nfs_free_location().
 */
static FedFsStatus
nfs_parse_node(const char *pathname, xmlNodePtr location,
		struct nfs_fsloc **fsloc)
{
	struct nfs_fsloc *tmp;
	FedFsStatus retval;

	tmp = nfs_new_location();
	if (tmp == NULL)
		return FEDFS_ERR_SVRFAULT;

	retval = nfs_parse_location_children(pathname, location, tmp);
	if (retval != FEDFS_OK)
		nfs_free_location(tmp);
	else
		*fsloc = tmp;
	return retval;
}

/**
 * Build list of NFS locations from a nodeset
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param nodeset XML nodeset containing "location" elements
 * @param fslocs OUT: pointer to a list of NFS locations
 * @return a FedFsStatus code
 *
 * If nfs_parse_nodeset() returns FEDFS_OK, caller must free the returned
 * list of locations with nfs_free_locations().
 */
static FedFsStatus
nfs_parse_nodeset(const char *pathname, xmlNodeSetPtr nodeset,
		struct nfs_fsloc **fslocs)
{
	struct nfs_fsloc *location, *result = NULL;
	FedFsStatus retval;
	int i;

	if (xmlXPathNodeSetIsEmpty(nodeset)) {
		xlog(D_GENERAL, "%s: No fileset locations found in %s",
			__func__, pathname);
		return FEDFS_ERR_NOTJUNCT;
	}

	for (i = 0; i < nodeset->nodeNr; i++) {
		xmlNodePtr node = nodeset->nodeTab[i];

		retval = nfs_parse_node(pathname, node, &location);
		if (retval != FEDFS_OK) {
			nfs_free_locations(result);
			return retval;
		}

		if (result == NULL)
			result = location;
		else
			result->nfl_next = location;
	}

	*fslocs = result;
	return FEDFS_OK;
}

/**
 * Parse fileset location information from junction XML
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param context XML path context containing junction XML
 * @param fslocs OUT: pointer to a list of NFS locations
 * @return a FedFsStatus code
 *
 * If nfs_parse_context() returns FEDFS_OK, caller must free the returned
 * list of locations with nfs_free_locations().
 */
static FedFsStatus
nfs_parse_context(const char *pathname, xmlXPathContextPtr context,
		struct nfs_fsloc **fslocs)
{
	xmlXPathObjectPtr object;
	FedFsStatus retval;

	object = xmlXPathEvalExpression(NFS_XML_LOCATION_XPATH, context);
	if (object == NULL) {
		xlog(D_GENERAL, "%s: Failed to evaluate XML in %s",
			__func__, pathname);
		return FEDFS_ERR_NOTJUNCT;
	}

	retval = nfs_parse_nodeset(pathname, object->nodesetval, fslocs);

	xmlXPathFreeObject(object);
	return retval;
}

/**
 * Parse NFS locations information from junction XML
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param doc XML parse tree containing junction XML document
 * @param fslocs OUT: pointer to a list of NFS locations
 * @return a FedFsStatus code
 *
 * If nfs_parse_xml() returns FEDFS_OK, caller must free the returned
 * list of locations with nfs_free_locations().
 */
static FedFsStatus
nfs_parse_xml(const char *pathname, xmlDocPtr doc, struct nfs_fsloc **fslocs)
{
	xmlXPathContextPtr context;
	FedFsStatus retval;

	context = xmlXPathNewContext(doc);
	if (context == NULL) {
		xlog(D_GENERAL, "%s: Failed to create XPath context from %s",
			__func__, pathname);
		return FEDFS_ERR_SVRFAULT;
	}

	retval = nfs_parse_context(pathname, context, fslocs);

	xmlXPathFreeContext(context);
	return retval;
}

/**
 * Retrieve list of NFS locations from an NFS junction
 *
 * @param pathname NUL-terminated C string containing pathname of a junction
 * @param fslocs OUT: pointer to a list of NFS locations
 * @return a FedFsStatus code
 *
 * If nfs_get_locations() returns FEDFS_OK, caller must free the returned
 * list of locations with nfs_free_locations().
 */
FedFsStatus
nfs_get_locations(const char *pathname, struct nfs_fsloc **fslocs)
{
	FedFsStatus retval;
	xmlDocPtr doc;

	if (fslocs == NULL)
		return FEDFS_ERR_INVAL;

	retval = junction_xml_parse(pathname, JUNCTION_XATTR_NAME_NFS, &doc);
	if (retval != FEDFS_OK)
		return retval;

	retval = nfs_parse_xml(pathname, doc, fslocs);

	xmlFreeDoc(doc);
	return retval;
}

/**
 * Predicate: does "pathname" refer to an object that can become an NFS junction?
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 *
 * Return values:
 *	FEDFS_ERR_NOTJUNCT:	"pathname" refers to an object that can be
 *				made into a NFS junction
 *	FEDFS_ERR_EXIST:	"pathname" refers to something that is
 *				already a junction
 *	FEDFS_ERR_INVAL:	"pathname" does not exist
 *	Other:			Some error occurred, "pathname" not
 *				investigated
 */
FedFsStatus
nfs_is_prejunction(const char *pathname)
{
	FedFsStatus retval;
	int fd;

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_is_directory(fd, pathname);
	if (retval != FEDFS_OK)
		goto out_close;

	retval = junction_is_sticky_bit_set(fd, pathname);
	switch (retval) {
	case FEDFS_ERR_NOTJUNCT:
		break;
	case FEDFS_OK:
		goto out_exist;
	default:
		goto out_close;
	}

	retval = junction_is_xattr_present(fd, pathname, JUNCTION_XATTR_NAME_NFS);
	switch (retval) {
	case FEDFS_ERR_NOTJUNCT:
		break;
	case FEDFS_OK:
		goto out_exist;
	default:
		goto out_close;
	}

out_close:
	(void)close(fd);
	return retval;
out_exist:
	retval = FEDFS_ERR_EXIST;
	goto out_close;
}

/**
 * Verify that junction contains NFS junction XML
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 *
 * Return values:
 *	FEDFS_OK:		"pathname" refers to an NFS junction
 *	FEDFS_ERR_NOTJUNCT:	"pathname" refers to something that is
 *				not an NFS junction
 *	FEDFS_ERR_INVAL:	"pathname" does not exist
 *	Other:			Some error occurred, "pathname" not
 *				investigated
 *
 * NB: This is an expensive test.  However, it is only done if the object
 * actually has a junction extended attribute, meaning it should be done
 * rarely.  If this is really a problem, we can make the XML test cheaper.
 */
static FedFsStatus
nfs_is_junction_xml(const char *pathname)
{
	struct nfs_fsloc *fslocs = NULL;
	FedFsStatus retval;
	xmlDocPtr doc;

	retval = junction_xml_parse(pathname, JUNCTION_XATTR_NAME_NFS, &doc);
	if (retval != FEDFS_OK)
		return retval;

	retval = nfs_parse_xml(pathname, doc, &fslocs);
	nfs_free_locations(fslocs);

	xmlFreeDoc(doc);
	return retval;
}

/**
 * Predicate: does "pathname" refer to an NFS junction?
 *
 * @param pathname NUL-terminated C string containing pathname of a directory
 * @return a FedFsStatus code
 *
 * Return values:
 *	FEDFS_OK:		"pathname" refers to an NFS junction
 *	FEDFS_ERR_NOTJUNCT:	"pathname" refers to an object that is
 *				not a junction
 *	FEDFS_ERR_INVAL:	"pathname" does not exist
 *	Other:			Some error occurred, "pathname" not
 *				investigated
 */
FedFsStatus
nfs_is_junction(const char *pathname)
{
	FedFsStatus retval;
	int fd;

	retval = junction_open_path(pathname, &fd);
	if (retval != FEDFS_OK)
		return retval;

	retval = junction_is_directory(fd, pathname);
	if (retval != FEDFS_OK)
		goto out_close;

	retval = junction_is_sticky_bit_set(fd, pathname);
	if (retval != FEDFS_OK)
		goto out_close;

	retval = junction_is_xattr_present(fd, pathname, JUNCTION_XATTR_NAME_NFS);
	if (retval != FEDFS_OK)
		goto out_close;

	(void)close(fd);

	return nfs_is_junction_xml(pathname);

out_close:
	(void)close(fd);
	return retval;
}