-
Notifications
You must be signed in to change notification settings - Fork 131
/
JdbcFs.java
1693 lines (1484 loc) · 59.2 KB
/
JdbcFs.java
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
/*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library 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 Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.chimera;
import static com.google.common.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.dcache.acl.enums.AceFlags.DIRECTORY_INHERIT_ACE;
import static org.dcache.acl.enums.AceFlags.FILE_INHERIT_ACE;
import static org.dcache.acl.enums.AceFlags.INHERIT_ONLY_ACE;
import static org.dcache.chimera.FileSystemProvider.StatCacheOption.NO_STAT;
import static org.dcache.chimera.FileSystemProvider.StatCacheOption.STAT;
import static org.dcache.util.ByteUnit.EiB;
import static org.dcache.util.SqlHelper.tryToClose;
import com.google.common.base.Throwables;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.io.ByteSource;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import diskCacheV111.util.RetentionPolicy;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import javax.sql.DataSource;
import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.dcache.acl.ACE;
import org.dcache.acl.enums.RsType;
import org.dcache.chimera.posix.Stat;
import org.dcache.chimera.quota.QuotaHandler;
import org.dcache.chimera.store.InodeStorageInformation;
import org.dcache.util.Checksum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.NonTransientDataAccessResourceException;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionException;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.TransactionSystemException;
import org.springframework.transaction.support.DefaultTransactionDefinition;
/**
* JDBC-FS is THE building block of Chimera. It's an abstraction layer, which allows to build
* filesystem on top of a RDBMS.
*
* @Immutable
* @Threadsafe
*/
public class JdbcFs implements FileSystemProvider, LeaderLatchListener {
/**
* common error message for unimplemented
*/
private static final String NOT_IMPL =
"this operation is unsupported for this "
+ "file system; please install a dCache-aware "
+ "implementation of the file system interface";
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(JdbcFs.class);
/**
* the number of pnfs levels. Level zero associated with file real content, which is not our
* regular case.
*/
public static final int LEVELS_NUMBER = 7;
/**
* SQL query engine
*/
private final FsSqlDriver _sqlDriver;
/**
* Database connection pool
*/
private final DataSource _dbConnectionsPool;
private final PlatformTransactionManager _tx;
private final TransactionDefinition _txDefinition = new DefaultTransactionDefinition();
/*
* A dummy constant key force bay cache interface. the value doesn't
* matter - only that it's the same value every time
*/
private static final Integer DUMMY_KEY = 0;
/**
* Cache value of FsStat
*/
private final Executor _fsStatUpdateExecutor =
Executors.newSingleThreadExecutor(
new ThreadFactoryBuilder()
.setNameFormat("fsstat-updater-thread-%d")
.build()
);
private final LoadingCache<Object, FsStat> _fsStatCache
= CacheBuilder.newBuilder()
.refreshAfterWrite(100, TimeUnit.MILLISECONDS)
.build(
CacheLoader.asyncReloading(new CacheLoader<Object, FsStat>() {
@Override
public FsStat load(Object k) throws Exception {
return JdbcFs.this.getFsStat0();
}
}
, _fsStatUpdateExecutor));
/* The PNFS ID to inode number mapping will never change while dCache is running.
*/
protected final Cache<String, Long> _inoCache =
CacheBuilder.newBuilder()
.maximumSize(100000)
.build();
/* The inode number to PNFS ID mapping will never change while dCache is running.
*/
protected final Cache<Long, String> _idCache =
CacheBuilder.newBuilder()
.maximumSize(100000)
.build();
private QuotaHandler _quota;
/**
* current fs id
*/
private final int _fsId;
/**
* available space
*/
static final long AVAILABLE_SPACE = EiB.toBytes(1L);
/**
* total files
*/
static final long TOTAL_FILES = 62914560L;
/**
* maximal length of an object name in a directory.
*/
private static final int MAX_NAME_LEN = 255;
/**
* switch quota check on/off
*/
private boolean _quotaEnabled;
/**
* default retention policy
*/
private RetentionPolicy _defaultRetentionPolicy;
private final ScheduledExecutorService maintenanceTaskExecutor = Executors.newSingleThreadScheduledExecutor(
new ThreadFactoryBuilder()
.setNameFormat("chimera-maintenance-executor-%d")
.build()
);
private ScheduledFuture<?> maintenanceTask;
public JdbcFs(DataSource dataSource, PlatformTransactionManager txManager)
throws SQLException, ChimeraFsException {
this(dataSource, txManager, 0);
}
public JdbcFs(DataSource dataSource, PlatformTransactionManager txManager, int id)
throws SQLException, ChimeraFsException {
_dbConnectionsPool = dataSource;
_fsId = id;
_tx = txManager;
// try to get database dialect specific query engine
_sqlDriver = FsSqlDriver.getDriverInstance(dataSource);
}
public void setQuota(QuotaHandler quota) {
_quota = quota;
}
public void setQuotaEnabled(boolean enabled) {
_quotaEnabled = enabled;
if (_quotaEnabled) {
_quota.scheduleRefreshQuota();
}
}
public void setDefaultRetentionPolicy(RetentionPolicy rp) {
_defaultRetentionPolicy = rp;
}
private FsInode getWormID() throws ChimeraFsException {
return this.path2inode("/admin/etc/config");
}
private <T> T inTransaction(FallibleTransactionCallback<T> callback)
throws ChimeraFsException {
TransactionStatus status = _tx.getTransaction(_txDefinition);
T result;
try {
result = callback.doInTransaction(status);
_tx.commit(status);
} catch (ChimeraFsException e) {
rollbackOnException(status, e);
throw e;
} catch (NonTransientDataAccessResourceException e) {
rollbackOnException(status, e);
throw new BackEndErrorChimeraFsException(e.getMessage(), e);
} catch (DataAccessException e) {
rollbackOnException(status, e);
throw new ChimeraFsException(e.getMessage(), e);
} catch (Exception e) {
rollbackOnException(status, e);
throw e;
}
return result;
}
/**
* Perform a rollback, handling rollback exceptions properly.
*
* @param status object representing the transaction
* @param ex the thrown application exception or error
* @throws TransactionException in case of a rollback error
*/
private void rollbackOnException(TransactionStatus status, Throwable ex)
throws TransactionException {
LOGGER.debug("Initiating transaction rollback on application exception", ex);
try {
_tx.rollback(status);
} catch (TransactionSystemException e) {
LOGGER.error("Application exception overridden by rollback exception", ex);
e.initApplicationException(ex);
throw e;
} catch (RuntimeException e) {
LOGGER.error("Application exception overridden by rollback exception", ex);
throw e;
} catch (Error err) {
LOGGER.error("Application exception overridden by rollback error", ex);
throw err;
}
}
//////////////////////////////////////////////////////////
////
////
//// Fs operations
////
/////////////////////////////////////////////////////////
@Override
public FsInode createLink(String src, String dest) throws ChimeraFsException {
File file = new File(src);
return inTransaction(
status -> createLink(path2inode(file.getParent()), file.getName(), dest));
}
@Override
public FsInode createLink(FsInode parent, String name, String dest) throws ChimeraFsException {
return inTransaction(status -> createLink(parent, name, 0, 0, 0777,
dest.getBytes(StandardCharsets.UTF_8)));
}
@Override
public FsInode createLink(FsInode parent, String name, int uid, int gid, int mode, byte[] dest)
throws ChimeraFsException {
checkNameLength(name);
return inTransaction(status -> {
FsInode inode;
try {
Stat stat = parent.statCache();
int group = (stat.getMode() & UnixPermission.S_ISGID) != 0 ? stat.getGid() : gid;
inode = _sqlDriver.createFile(parent, name, uid, group, mode,
UnixPermission.S_IFLNK);
// link is a regular file where content is a reference
_sqlDriver.setInodeIo(inode, true);
_sqlDriver.write(inode, 0, 0, dest, 0, dest.length);
_sqlDriver.copyAcl(parent, inode, RsType.FILE,
EnumSet.of(INHERIT_ONLY_ACE, DIRECTORY_INHERIT_ACE, FILE_INHERIT_ACE),
EnumSet.of(FILE_INHERIT_ACE));
fillIdCaches(inode);
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(e);
}
return inode;
});
}
/**
* create a hard link
*
* @param parent inode of directory where to create
* @param inode
* @param name
* @return
* @throws ChimeraFsException
*/
@Override
public FsInode createHLink(FsInode parent, FsInode inode, String name)
throws ChimeraFsException {
checkNameLength(name);
return inTransaction(status -> {
try {
Stat parentStat = parent.stat();
if (parentStat == null) {
throw FileNotFoundChimeraFsException.of(parent);
}
if ((parentStat.getMode() & UnixPermission.F_TYPE) != UnixPermission.S_IFDIR) {
throw new NotDirChimeraException(parent);
}
Stat inodeStat = inode.stat();
if (inodeStat == null) {
throw FileNotFoundChimeraFsException.of(inode);
}
if ((inodeStat.getMode() & UnixPermission.F_TYPE) == UnixPermission.S_IFDIR) {
throw new PermissionDeniedChimeraFsException("hard link not allowed for directory");
}
_sqlDriver.createEntryInParent(parent, name, inode);
_sqlDriver.incNlink(inode);
_sqlDriver.incNlink(parent, 0);
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(e);
}
return inode;
});
}
@Override
public FsInode createFile(String path) throws ChimeraFsException {
File file = new File(path);
return inTransaction(status -> createFile(path2inode(file.getParent()), file.getName()));
}
@Override
public FsInode createFile(FsInode parent, String name) throws ChimeraFsException {
return inTransaction(status -> createFile(parent, name, 0, 0, 0644));
}
@Override
public FsInode createFileLevel(FsInode inode, int level) throws ChimeraFsException {
return inTransaction(
status -> _sqlDriver.createLevel(inode, 0, 0, 0644 | UnixPermission.S_IFREG, level));
}
@Override
public FsInode createFile(FsInode parent, String name, int owner, int group, int mode)
throws ChimeraFsException {
return createFile(parent, name, owner, group, mode, UnixPermission.S_IFREG);
}
@Override
public FsInode createFile(FsInode parent, String name, int owner, int group, int mode, int type)
throws ChimeraFsException {
if (name.startsWith(".(")) { // special files only
String[] cmd = PnfsCommandProcessor.process(name);
if (name.startsWith(".(tag)(") && (cmd.length == 2)) {
this.createTag(parent, cmd[1], owner, group, 0644);
return new FsInode_TAG(this, parent.ino(), cmd[1]);
}
if (name.startsWith(".(pset)(") || name.startsWith(".(fset)(")
|| name.startsWith(".(suri)(")) {
/**
* This is not 100% correct, as we throw exist even if
* someone tries to set attribute for a file which does not exist.
*/
throw new FileExistsChimeraFsException(name);
}
if (name.startsWith(".(use)(") && (cmd.length == 3)) {
int level = Integer.parseInt(cmd[1]);
return inTransaction(status -> {
FsInode useInode = _sqlDriver.inodeOf(parent, cmd[2], STAT);
if (useInode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[2]);
}
try {
Stat stat = useInode.statCache();
return _sqlDriver.createLevel(useInode, stat.getUid(),
stat.getGid(),
stat.getMode(), level);
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(name, e);
}
});
}
if (name.startsWith(".(access)(") && (cmd.length == 3)) {
int accessLevel = Integer.parseInt(cmd[2]);
if (accessLevel == 0) {
return id2inode(cmd[1], NO_STAT);
}
return inTransaction(status -> {
try {
FsInode accessInode = id2inode(cmd[1], STAT);
Stat stat = accessInode.statCache();
return _sqlDriver.createLevel(accessInode,
stat.getUid(), stat.getGid(),
stat.getMode(), accessLevel);
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(name, e);
}
});
}
return null;
}
checkNameLength(name);
checkArgument(UnixPermission.getType(type) != UnixPermission.S_IFDIR);
return inTransaction(status -> {
try {
Stat parentStat = parent.statCache();
if (parentStat == null) {
throw FileNotFoundChimeraFsException.of(parent);
}
if ((parentStat.getMode() & UnixPermission.F_TYPE) != UnixPermission.S_IFDIR) {
throw new NotDirChimeraException(parent);
}
int gid = (parentStat.getMode() & UnixPermission.S_ISGID) != 0 ? parentStat.getGid()
: group;
if (_quotaEnabled) {
RetentionPolicy rp = getRetentionPolicyFromParentTag(parent);
checkQuota(owner, gid, rp);
}
FsInode inode = _sqlDriver.createFile(parent, name, owner, gid, mode, type);
_sqlDriver.copyAcl(parent, inode, RsType.FILE,
EnumSet.of(INHERIT_ONLY_ACE, DIRECTORY_INHERIT_ACE, FILE_INHERIT_ACE),
EnumSet.of(FILE_INHERIT_ACE));
fillIdCaches(inode);
return inode;
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(e);
}
});
}
/**
* Create a new entry with given inode id.
*
* @param parent
* @param id
* @param name
* @param owner
* @param group
* @param mode
* @param type
* @throws ChimeraFsException
*/
@Override
public void createFileWithId(FsInode parent, String id, String name, int owner, int group,
int mode, int type) throws ChimeraFsException {
checkNameLength(name);
checkArgument((type & UnixPermission.S_IFDIR) == 0);
inTransaction(status -> {
try {
if (!parent.exists()) {
throw FileNotFoundChimeraFsException.of(parent);
}
if (!parent.isDirectory()) {
throw new NotDirChimeraException(parent);
}
Stat stat = parent.statCache();
int gid = (stat.getMode() & UnixPermission.S_ISGID) != 0 ? stat.getGid() : group;
if (_quotaEnabled) {
RetentionPolicy rp = getRetentionPolicyFromParentTag(parent);
checkQuota(owner, gid, rp);
}
FsInode inode = _sqlDriver.createFileWithId(parent, id, name, owner, gid, mode,
type);
_sqlDriver.copyAcl(parent, inode, RsType.FILE,
EnumSet.of(INHERIT_ONLY_ACE, DIRECTORY_INHERIT_ACE, FILE_INHERIT_ACE),
EnumSet.of(FILE_INHERIT_ACE));
fillIdCaches(inode);
return null;
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(e);
}
});
}
@Override
public DirectoryStreamB<ChimeraDirectoryEntry> newDirectoryStream(FsInode dir)
throws ChimeraFsException {
return _sqlDriver.newDirectoryStream(dir);
}
@Override
public DirectoryStreamB<ChimeraDirectoryEntry> virtualDirectoryStream(FsInode dir,
String labelname) throws ChimeraFsException {
return _sqlDriver.virtualDirectoryStream(dir, labelname);
}
@Override
public void remove(String path) throws ChimeraFsException {
File filePath = new File(path);
String parentPath = filePath.getParent();
if (parentPath == null) {
throw new InvalidArgumentChimeraException("Cannot delete file system root.");
}
inTransaction(status -> {
FsInode parent = path2inode(parentPath);
String name = filePath.getName();
FsInode inode = _sqlDriver.inodeOf(parent, name, STAT);
if (inode == null || !_sqlDriver.remove(parent, name, inode)) {
throw FileNotFoundChimeraFsException.ofPath(path);
}
return null;
});
}
@Override
public void remove(FsInode directory, String name, FsInode inode) throws ChimeraFsException {
inTransaction(status -> {
if (!_sqlDriver.remove(directory, name, inode)) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(directory, name);
}
return null;
});
}
@Override
public void remove(FsInode inode) throws ChimeraFsException {
inTransaction(status -> {
if (inode.type() != FsInodeType.INODE) {
// now allowed
throw new InvalidArgumentChimeraException("Not a file.");
}
if (inode.ino() == _sqlDriver.getRootInumber()) {
throw new InvalidArgumentChimeraException("Cannot delete file system root.");
}
if (!inode.exists()) {
throw FileNotFoundChimeraFsException.of(inode);
}
if (inode.isDirectory() && inode.statCache().getNlink() > 2) {
throw new DirNotEmptyChimeraFsException("Directory is not empty");
}
_sqlDriver.remove(inode);
return null;
});
}
@Override
public Stat stat(String path) throws ChimeraFsException {
return stat(path2inode(path));
}
@Override
public Stat stat(FsInode inode) throws ChimeraFsException {
return stat(inode, 0);
}
@Override
public Stat stat(FsInode inode, int level) throws ChimeraFsException {
Stat stat = _sqlDriver.stat(inode, level);
if (stat == null) {
throw FileNotFoundChimeraFsException.of(inode);
}
if (level == 0) {
_inoCache.put(stat.getId(), stat.getIno());
_idCache.put(stat.getIno(), stat.getId());
}
return stat;
}
@Override
public FsInode mkdir(String path) throws ChimeraFsException {
int li = path.lastIndexOf('/');
String file = path.substring(li + 1);
String dir = (li > 1) ? path.substring(0, li) : "/";
return inTransaction(status -> mkdir(path2inode(dir), file));
}
@Override
public FsInode mkdir(FsInode parent, String name) throws ChimeraFsException {
return mkdir(parent, name, 0, 0, 0755);
}
@Override
public FsInode mkdir(FsInode parent, String name, int owner, int group, int mode)
throws ChimeraFsException {
checkNameLength(name);
return inTransaction(status -> {
try {
if (!parent.isDirectory()) {
throw new NotDirChimeraException(parent);
}
Stat stat = parent.statCache();
int gid, perm;
if ((stat.getMode() & UnixPermission.S_ISGID) != 0) {
gid = stat.getGid();
perm = mode | UnixPermission.S_ISGID;
} else {
gid = group;
perm = mode;
}
// ensure that t_inodes and t_tags_inodes update in the same order as
// in removeDir
FsInode inode = _sqlDriver.mkdir(parent, name, owner, gid, perm);
_sqlDriver.copyTags(parent, inode);
_sqlDriver.copyAcl(parent, inode, RsType.DIR, EnumSet.of(INHERIT_ONLY_ACE),
EnumSet.of(FILE_INHERIT_ACE, DIRECTORY_INHERIT_ACE));
fillIdCaches(inode);
return inode;
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(name, e);
}
});
}
private void fillIdCaches(FsInode inode) {
Stat stat = inode.getStatCache();
if (stat != null) {
_inoCache.put(stat.getId(), stat.getIno());
_idCache.put(stat.getIno(), stat.getId());
}
}
@Override
public FsInode mkdir(FsInode parent, String name, int owner, int group, int mode,
List<ACE> acl, Map<String, byte[]> tags)
throws ChimeraFsException {
checkNameLength(name);
return inTransaction(status -> {
try {
if (!parent.isDirectory()) {
throw new NotDirChimeraException(parent);
}
Stat stat = parent.statCache();
int gid, perm;
if ((stat.getMode() & UnixPermission.S_ISGID) != 0) {
gid = stat.getGid();
perm = mode | UnixPermission.S_ISGID;
} else {
gid = group;
perm = mode;
}
FsInode inode = _sqlDriver.mkdir(parent, name, owner, gid, perm);
_sqlDriver.createTags(inode, owner, gid, perm & 0666, tags);
_sqlDriver.writeAcl(inode, RsType.DIR, acl);
fillIdCaches(inode);
return inode;
} catch (DuplicateKeyException e) {
throw new FileExistsChimeraFsException(name, e);
}
});
}
@Override
public FsInode path2inode(String path) throws ChimeraFsException {
return path2inode(path, new RootInode(this, _sqlDriver.getRootInumber()));
}
@Override
public FsInode path2inode(String path, FsInode startFrom) throws ChimeraFsException {
FsInode inode = _sqlDriver.path2inode(startFrom, path);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofPath(path);
}
fillIdCaches(inode);
return inode;
}
@Override
public String inode2id(FsInode inode) throws ChimeraFsException {
try {
return _idCache.get(inode.ino(), () -> {
String id = _sqlDriver.getId(inode);
if (id == null) {
throw FileNotFoundChimeraFsException.of(inode);
}
return id;
});
} catch (ExecutionException e) {
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
Throwables.throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
}
@Override
public FsInode id2inode(String id, StatCacheOption option) throws ChimeraFsException {
if (option == NO_STAT) {
try {
return new FsInode(this, _inoCache.get(id, () -> {
Long ino = _sqlDriver.getInumber(id);
if (ino == null) {
throw FileNotFoundChimeraFsException.ofPnfsId(id);
}
return ino;
}));
} catch (ExecutionException e) {
Throwables.throwIfInstanceOf(e.getCause(), ChimeraFsException.class);
Throwables.throwIfInstanceOf(e.getCause(), DataAccessException.class);
Throwables.throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
} else {
Stat stat = _sqlDriver.stat(id);
if (stat == null) {
throw FileNotFoundChimeraFsException.ofPnfsId(id);
}
_inoCache.put(stat.getId(), stat.getIno());
_idCache.put(stat.getIno(), stat.getId());
return new FsInode(this, stat.getIno(), FsInodeType.INODE, 0, stat);
}
}
@Override
public List<FsInode> path2inodes(String path) throws ChimeraFsException {
return path2inodes(path, new RootInode(this, _sqlDriver.getRootInumber()));
}
@Override
public List<FsInode> path2inodes(String path, FsInode startFrom)
throws ChimeraFsException {
List<FsInode> inodes = _sqlDriver.path2inodes(startFrom, path);
if (inodes.isEmpty()) {
throw FileNotFoundChimeraFsException.ofPath(path);
}
fillIdCaches(inodes.get(inodes.size() - 1));
return inodes;
}
@Override
public FsInode inodeOf(FsInode parent, String name, StatCacheOption cacheOption)
throws ChimeraFsException {
// only if it's PNFS command
if (name.startsWith(".(")) {
if (name.startsWith(".(id)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode inode = _sqlDriver.inodeOf(parent, cmd[1], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[1]);
}
return new FsInode_ID(this, inode.ino());
}
if (name.startsWith(".(use)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 3) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
try {
int level = Integer.parseInt(cmd[1]);
FsInode inode = _sqlDriver.inodeOf(parent, cmd[2], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[2]);
}
if (level <= LEVELS_NUMBER) {
stat(inode, level);
return new FsInode(this, inode.ino(), level);
} else {
// is it error or a real file?
}
} catch (NumberFormatException nfe) {
// possible wrong format...ignore
}
}
if (name.startsWith(".(access)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if ((cmd.length < 2) || (cmd.length > 3)) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
try {
int level = cmd.length == 2 ? 0 : Integer.parseInt(cmd[2]);
FsInode useInode = id2inode(cmd[1], STAT);
if (level <= LEVELS_NUMBER) {
return new FsInode(this, useInode.ino(), level);
} else {
// is it error or a real file?
}
} catch (NumberFormatException nfe) {
// possible wrong format...ignore
} catch (FileNotFoundChimeraFsException e) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
}
if (name.startsWith(".(nameof)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode nameofInode = new FsInode_NAMEOF(this, id2inode(cmd[1], NO_STAT).ino());
if (!nameofInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return nameofInode;
}
if (name.startsWith(".(const)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode constInode = new FsInode_CONST(this, parent.ino());
if (!constInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return constInode;
}
if (name.startsWith(".(parent)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode parentInode = new FsInode_PARENT(this, id2inode(cmd[1], NO_STAT).ino());
if (!parentInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return parentInode;
}
if (name.startsWith(".(pathof)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode pathofInode = new FsInode_PATHOF(this, id2inode(cmd[1], NO_STAT).ino());
if (!pathofInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return pathofInode;
}
if (name.startsWith(".(tag)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode tagInode = new FsInode_TAG(this, parent.ino(), cmd[1]);
if (!tagInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return tagInode;
}
if (name.startsWith(".(suri)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode inode = _sqlDriver.inodeOf(parent, cmd[1], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[1]);
}
return new FsInode_SURI(this, inode.ino());
}
if (name.equals(".(checksums)()")) {
return new FsInode_CKSTYP(this, parent.ino());
}
if (name.equals(".(tags)()")) {
return new FsInode_TAGS(this, parent.ino());
}
if (name.startsWith(".(pset)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length < 3) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
String[] args = new String[cmd.length - 2];
System.arraycopy(cmd, 2, args, 0, args.length);
FsInode psetInode = new FsInode_PSET(this, id2inode(cmd[1], NO_STAT).ino(), args);
if (!psetInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return psetInode;
}
if (name.equals(".(get)(cursor)")) {
FsInode pgetInode = new FsInode_PCUR(this, parent.ino());
if (!pgetInode.exists()) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
return pgetInode;
}
if (name.startsWith(".(get)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length < 3) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode inode = _sqlDriver.inodeOf(parent, cmd[1], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[1]);
}
switch (cmd[2]) {
case "locality":
return new FsInode_PLOC(this, inode.ino());
case "checksum":
case "checksums":
return new FsInode_PCRC(this, inode.ino());
case "pins":
return new FsInode_PINS(this, inode.ino());
default:
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[2]);
}
}
if (name.equals(".(config)")) {
return getWormID();
}
if (name.startsWith(".(config)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length != 2) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode inode = _sqlDriver.inodeOf(getWormID(), cmd[1], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[1]);
}
return inode;
}
if (name.startsWith(".(fset)(")) {
String[] cmd = PnfsCommandProcessor.process(name);
if (cmd.length < 3) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, name);
}
FsInode inode = _sqlDriver.inodeOf(parent, cmd[1], NO_STAT);
if (inode == null) {
throw FileNotFoundChimeraFsException.ofFileInDirectory(parent, cmd[1]);
}