-
Notifications
You must be signed in to change notification settings - Fork 155
/
leo_nfs_proto3_server.erl
896 lines (797 loc) · 33.7 KB
/
leo_nfs_proto3_server.erl
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
%%======================================================================
%%
%% Leo Gateway
%%
%% Copyright (c) 2012-2015 Rakuten, Inc.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%%======================================================================
-module(leo_nfs_proto3_server).
-include("leo_gateway.hrl").
-include("leo_http.hrl").
-include_lib("leo_commons/include/leo_commons.hrl").
-include_lib("leo_redundant_manager/include/leo_redundant_manager.hrl").
-include_lib("leo_object_storage/include/leo_object_storage.hrl").
-include_lib("leo_s3_libs/include/leo_s3_bucket.hrl").
-include_lib("leo_logger/include/leo_logger.hrl").
-include("leo_nfs_proto3.hrl").
-include_lib("kernel/include/file.hrl").
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2]).
-export([nfsproc3_null_3/2,
nfsproc3_getattr_3/3,
nfsproc3_setattr_3/3,
nfsproc3_lookup_3/3,
nfsproc3_access_3/3,
nfsproc3_readlink_3/3,
nfsproc3_read_3/3,
nfsproc3_write_3/3,
nfsproc3_create_3/3,
nfsproc3_mkdir_3/3,
nfsproc3_symlink_3/3,
nfsproc3_mknod_3/3,
nfsproc3_remove_3/3,
nfsproc3_rmdir_3/3,
nfsproc3_rename_3/3,
nfsproc3_link_3/3,
nfsproc3_readdir_3/3,
nfsproc3_readdirplus_3/3,
nfsproc3_fsstat_3/3,
nfsproc3_pathconf_3/3,
nfsproc3_commit_3/3,
nfsproc3_fsinfo_3/3]).
-export([sattr_mode_to_file_info/1, sattr_uid_to_file_info/1,
sattr_gid_to_file_info/1, sattr_atime_to_file_info/1,
sattr_mtime_to_file_info/1, sattr_size_to_file_info/1]).
-record(ongoing_readdir, {
filelist :: list(#?METADATA{})
}).
-define(SIMPLENFS_WCC_EMPTY, {
{false, void},
{false, void}
}).
-define(NFS_DUMMY_FILE4S3DIR_OLD, << "$$_dir_$$" >>).
-define(NFS_DUMMY_FILE4S3DIR, << "\x00_dir_\x00" >>).
-define(NFS_READDIR_NUM_OF_RESPONSE, 10).
-define(NFS3_OK, 'NFS3_OK').
-define(NFS3_NG, 'NFS3_NG').
-define(NFS3ERR_NOENT, 'NFS3ERR_NOENT').
-define(NFS3ERR_IO, 'NFS3ERR_IO').
-define(NFS3ERR_NOTEMPTY, 'NFS3ERR_NOTEMPTY').
-define(NFS3ERR_BADHANDLE,'NFS3ERR_BADHANDLE').
-define(NFS3ERR_BADTYPE, 'NFS3ERR_BADTYPE').
-define(NFS3ERR_NOTSUPP, 'NFS3ERR_NOTSUPP').
%% ---------------------------------------------------------------------
%% API
%% ---------------------------------------------------------------------
%% @doc Called only once from a parent rpc server process to initialize this module
%% during starting a leo_storage server.
-spec(init(any()) -> {ok, any()}).
init(Args) ->
leo_nfs_state_ets:add_write_verfier(crypto:strong_rand_bytes(8)),
{ok, Args}.
handle_call(Req,_From, S) ->
?debug("handle_call", "req:~p from:~p", [Req,_From]),
{reply, [], S}.
handle_cast(Req, S) ->
?debug("handle_cast", "req:~p", [Req]),
{reply, [], S}.
handle_info(Req, S) ->
?debug("handle_info", "req:~p", [Req]),
{noreply, S}.
terminate(_Reason,_S) ->
ok.
%% ---------------------------------------------------------------------
%% API
%% ---------------------------------------------------------------------
%% @doc
nfsproc3_null_3(_Clnt, State) ->
{reply, [], State}.
%% @doc
nfsproc3_getattr_3({{UID}} = _1, Clnt, State) ->
case leo_nfs_state_ets:get_path(UID) of
{ok, Path} ->
?debug("nfsproc3_getattr_3", "path:~p client:~p", [Path, Clnt]),
case getattr(Path) of
{ok, Meta} ->
{reply, {?NFS3_OK, {Meta}}, State};
not_found ->
{reply, {?NFS3ERR_NOENT, void}, State};
{error, Reason} ->
?error("nfsproc3_getattr_3/3", [{path, Path}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, Reason}, State}
end;
_ ->
{reply, {?NFS3ERR_BADHANDLE, void}, State}
end.
%% @doc
%% @todo for now do nothing
nfsproc3_setattr_3({{FileUID}, {_Mode,
_UID,
_GID,
SattrSize,
_ATime,
_MTime},_Guard} = _1, Clnt, State) ->
?debug("nfsproc3_setattr_3", "args:~p client:~p", [_1, Clnt]),
Size = sattr_size_to_file_info(SattrSize),
case Size of
undefined ->
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY}}, State};
_ ->
{ok, Path} = leo_nfs_state_ets:get_path(FileUID),
case leo_nfs_file_handler:trim(Path, Size) of
ok ->
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY}}, State};
{error, Reason}->
?error("nfsproc3_setattr_3/3",
[{path, Path}, {size, Size}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, Reason}, State}
end
end.
%% @doc
nfsproc3_lookup_3({{{UID}, Name}} = _1, Clnt, State) ->
{ok, Dir} = leo_nfs_state_ets:get_path(UID),
Path = leo_nfs_file_handler:path_relative_to_abs(filename:join(Dir, Name)),
?debug("nfsproc3_lookup_3", "path:~p client:~p", [Path, Clnt]),
case getattr(Path) of
{ok, Meta} ->
{ok, FileUID} = leo_nfs_state_ets:add_path(Path),
{reply, {?NFS3_OK, {{FileUID},
{true, Meta},
{false, void}
}}, State};
not_found ->
{reply, {?NFS3ERR_NOENT, {{false, void}}}, State};
{error, Reason} ->
?error("nfsproc3_lookup_3/3", [{path, Path}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {{false, void}}}, State}
end.
%% @doc
nfsproc3_access_3({{UID}, AccessBitMask} = _1, Clnt, State) ->
{ok, Path} = leo_nfs_state_ets:get_path(UID),
?debug("nfsproc3_access_3", "path:~p mask:~p client:~p", [Path, AccessBitMask, Clnt]),
{reply, {?NFS3_OK, {{false, void}, %% post_op_attr for obj
AccessBitMask %% access bits(up all)
}}, State}.
%% @doc
nfsproc3_readlink_3(_1, Clnt, State) ->
?debug("nfsproc3_readlink_3", "args:~p client:~p", [_1, Clnt]),
{reply, {?NFS3ERR_NOTSUPP, {{false, void}, %% post_op_attr for obj
<< "link path" >>}
}, State}.
%% @doc
nfsproc3_read_3({{UID}, Offset, Count} =_1, Clnt, State) ->
?debug("nfsproc3_read_3", "args:~p client:~p", [_1, Clnt]),
{ok, Path} = leo_nfs_state_ets:get_path(UID),
case leo_nfs_file_handler:read(Path, Offset, Offset + Count - 1) of
{ok, Meta, Body} ->
EOF = Meta#?METADATA.dsize =:= (Offset + Count),
{reply, {?NFS3_OK, {{false, void}, %% post_op_attr for obj
Count, %% count read bytes
EOF, %% eof
Body}
}, State};
{error, Reason} ->
?error("nfsproc3_read_3/3",
[{path, Path}, {offset, Offset}, {count, Count}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {{false, void} %% post_op_attr for obj
}}, State}
end.
%% @doc
nfsproc3_write_3({{UID}, Offset, Count,_HowStable, Data} = _1, Clnt, State) ->
?debug("nfsproc3_write_3", "uid:~p offset:~p count:~p client:~p",
[UID, Offset, Count, Clnt]),
{ok, Path} = leo_nfs_state_ets:get_path(UID),
case leo_nfs_file_handler:write(Path, Offset, Offset + Count - 1, Data) of
ok ->
{ok, WriteVerf} = leo_nfs_state_ets:get_write_verfier(),
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY,
Count,
'FILE_SYNC',
WriteVerf}
}, State};
{error, Reason} ->
?error("nfsproc3_write_3/3",
[{path, Path}, {offset, Offset}, {count, Count}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY}}, State}
end.
%% @doc
nfsproc3_create_3({{{UID}, Name}, {_CreateMode,_How}} = _1, Clnt, State) ->
?debug("nfsproc3_create_3", "args:~p client:~p", [_1, Clnt]),
{ok, Dir} = leo_nfs_state_ets:get_path(UID),
Key = filename:join(Dir, Name),
case leo_gateway_rpc_handler:put(#put_req_params{path = Key,
body = ?BIN_EMPTY,
dsize = 0}) of
{ok,_}->
{ok, FileUID} = leo_nfs_state_ets:add_path(Key),
TS = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
Meta = #?METADATA{
key = Key,
dsize = 0,
timestamp = TS
},
{reply, {?NFS3_OK, {{true, {FileUID}}, %% post_op file handle
{true, meta2fattr3(Meta)}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY}
}, State};
{error, Reason} ->
?error("nfsproc3_create_3/3",
[{path, Key}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY}}, State}
end.
%% @doc
nfsproc3_mkdir_3({{{UID}, Name},_How} = _1, Clnt, State) ->
?debug("nfsproc3_mkdir_3", "args:~p client:~p", [_1, Clnt]),
{ok, Dir} = leo_nfs_state_ets:get_path(UID),
DirPath = filename:join(Dir, Name),
Key = filename:join(DirPath, ?NFS_DUMMY_FILE4S3DIR),
case leo_gateway_rpc_handler:put(#put_req_params{path = Key,
body = ?BIN_EMPTY,
dsize = 0}) of
{ok,_}->
{reply, {?NFS3_OK, {{false, void}, %% post_op file handle
{false, void}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY
}
}, State};
{error, Reason} ->
?error("nfsproc3_mkdir_3/3",
[{path, Key}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {{false, void}, %% post_op file handle
{false, void}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY
}
}, State}
end.
%% @doc
nfsproc3_symlink_3(_1, Clnt, State) ->
?debug("nfsproc3_symlink_3", "args:~p client:~p", [_1, Clnt]),
{reply, {?NFS3ERR_NOTSUPP, {{false, void}, %% post_op file handle
{false, void}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY}
}, State}.
%% @doc
nfsproc3_mknod_3(_1, Clnt, State) ->
?debug("nfsproc3_mknod_3", "args:~p client:~p", [_1, Clnt]),
{reply, {?NFS3ERR_BADTYPE, {{false, void}, %% post_op file handle
{false, void}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY}
}, State}.
%% @doc
nfsproc3_remove_3({{{UID}, Name}} = _1, Clnt, State) ->
?debug("nfsproc3_remove_3", "args:~p client:~p", [_1, Clnt]),
{ok, Dir} = leo_nfs_state_ets:get_path(UID),
Key = filename:join(Dir, Name),
case leo_gateway_rpc_handler:delete(Key) of
ok ->
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY}}, State};
{error, Reason} ->
?error("nfsproc3_remove_3/3",
[{path, Key}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY}}, State}
end.
%% @doc
nfsproc3_rmdir_3({{{UID}, Name}} = _1, Clnt, State) ->
?debug("nfsproc3_rmdir_3", "args:~p client:~p", [_1, Clnt]),
{ok, Dir} = leo_nfs_state_ets:get_path(UID),
Path4S3 = filename:join(Dir, Name),
Path4S3Dir = leo_nfs_file_handler:path_to_dir(Path4S3),
case is_empty_dir(Path4S3Dir) of
true ->
Key = filename:join(Path4S3Dir, ?NFS_DUMMY_FILE4S3DIR_OLD),
case leo_gateway_rpc_handler:delete(Key) of
ok ->
Key_2 = filename:join(Path4S3Dir, ?NFS_DUMMY_FILE4S3DIR),
case leo_gateway_rpc_handler:delete(Key_2) of
ok ->
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY}}, State};
{error, Reason} ->
?error("nfsproc3_rmdir_3/3",
[{path, Key_2}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY}}, State}
end;
{error, Reason} ->
?error("nfsproc3_rmdir_3/3",
[{path, Key}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY}}, State}
end;
false ->
{reply, {?NFS3ERR_NOTEMPTY, {?SIMPLENFS_WCC_EMPTY}}, State}
end.
%% @doc
nfsproc3_rename_3({{{SrcUID}, SrcName}, {{DstUID}, DstName}} =_1, Clnt, State) ->
?debug("nfsproc3_rename_3", "args:~p client:~p", [_1, Clnt]),
{ok, SrcDir} = leo_nfs_state_ets:get_path(SrcUID),
{ok, DstDir} = leo_nfs_state_ets:get_path(DstUID),
Src4S3 = filename:join(SrcDir, SrcName),
Dst4S3 = filename:join(DstDir, DstName),
case leo_nfs_file_handler:rename(Src4S3, Dst4S3) of
ok ->
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY, %% src
?SIMPLENFS_WCC_EMPTY %% dst
}}, State};
{error, not_found} ->
{reply, {?NFS3ERR_NOENT, {?SIMPLENFS_WCC_EMPTY, %% src
?SIMPLENFS_WCC_EMPTY %% dst
}}, State};
{error, Reason} ->
?error("nfsproc3_rename_3/3",
[{src, Src4S3}, {dest, Dst4S3}, {cause, Reason}]),
{reply, {?NFS3ERR_IO, {?SIMPLENFS_WCC_EMPTY, %% src
?SIMPLENFS_WCC_EMPTY %% dst
}}, State}
end.
%% @doc
nfsproc3_link_3(_1, Clnt, State) ->
?debug("nfsproc3_link_3", "args:~p client:~p", [_1, Clnt]),
{reply, {?NFS3ERR_NOTSUPP, {{false, void}, %% post_op_attr
?SIMPLENFS_WCC_EMPTY
}
}, State}.
%% @doc
nfsproc3_readdir_3({{UID}, Cookie, CookieVerf, MaxCnt} = _1, Clnt, State) ->
?debug("nfsproc3_readdir_3", "args:~p client:~p", [_1, Clnt]),
{ok, Path} = leo_nfs_state_ets:get_path(UID),
readdir(Path, Cookie, CookieVerf, MaxCnt, false, State).
%% @doc
nfsproc3_readdirplus_3({{UID}, Cookie, CookieVerf,_DirCnt, MaxCnt} = _1, Clnt, State) ->
?debug("nfsproc3_readdirplus_3", "args:~p client:~p", [_1, Clnt]),
{ok, Path} = leo_nfs_state_ets:get_path(UID),
readdir(Path, Cookie, CookieVerf, MaxCnt, true, State).
%% @doc
nfsproc3_fsstat_3(_1, Clnt, State) ->
?debug("nfsproc3_fsstat_3", "args:~p client:~p", [_1, Clnt]),
{ok, {Total, Free}} = leo_nfs_file_handler:get_disk_usage(),
{reply, {?NFS3_OK, {{false, void}, %% post_op_attr
Total, %% total size
Free, %% free size
Free, %% free size(for auth user)
4294967296, %% # of files (erlang:round(math:pow(2, 32)))
4294967296, %% # of free file slots
4294967296, %% # of free file slots(for auth user)
10 %% invarsec
}}, State}.
%% @doc
nfsproc3_fsinfo_3(_1, Clnt, {RTMAX, WTMAX} = State) ->
?debug("nfsproc3_fsinfo_3", "args:~p client:~p", [_1, Clnt]),
MaxFileSize = ?DEF_NFSD_MAX_FILE_SIZE,
{reply, {?NFS3_OK, {{false, void}, %% post_op_attr
RTMAX, %% rtmax
RTMAX, %% rtperf(limited at client up to 1024 * 1024)
8, %% rtmult
WTMAX, %% wtmaxa(limited at client up to 1024 * 1024)
WTMAX, %% wtperf
8, %% wtmult
5242880, %% dperf (limited at client up to 32768)
MaxFileSize, %% max file size
{1, 0}, %% time_delta
?FSF3_HOMOGENEOUS %% properties
}}, State}.
%% @doc
nfsproc3_pathconf_3(_1, Clnt, State) ->
?debug("nfsproc3_pathconf_3", "args:~p client:~p", [_1, Clnt]),
{reply, {?NFS3_OK, {{false, void}, %% post_op_attr
8, %% linkmax
1024, %% name_max
true, %% no_trunc - mean make a reques error if filename's length was larger than max
false, %% chown_restricted
true, %% case_insensitive
true %% case_preserving
}}, State}.
%% @doc
nfsproc3_commit_3(_1, Clnt, State) ->
?debug("nfsproc3_commit_3", "args:~p client:~p", [_1, Clnt]),
{ok, WriteVerf} = leo_nfs_state_ets:get_write_verfier(),
{reply, {?NFS3_OK, {?SIMPLENFS_WCC_EMPTY,
WriteVerf
}}, State}.
%% @doc
sattr_mode_to_file_info({0,_}) ->
undefined;
sattr_mode_to_file_info({true, Mode}) ->
Mode.
%% @doc
sattr_uid_to_file_info({0,_}) ->
undefined;
sattr_uid_to_file_info({true, UID}) ->
UID.
%% @doc
sattr_gid_to_file_info({0,_}) ->
undefined;
sattr_gid_to_file_info({true, GID}) ->
GID.
%% @doc
sattr_size_to_file_info({true, Size}) ->
Size;
sattr_size_to_file_info({_,_}) ->
undefined.
%% @doc
sattr_atime_to_file_info({'DONT_CHANGE',_}) ->
undefined;
sattr_atime_to_file_info({'SET_TO_SERVER_TIME',_}) ->
leo_date:unixtime();
sattr_atime_to_file_info({_, {ATime,_}}) ->
ATime.
%% @doc
sattr_mtime_to_file_info({'DONT_CHANGE',_}) ->
undefined;
sattr_mtime_to_file_info({'SET_TO_SERVER_TIME',_}) ->
leo_date:unixtime();
sattr_mtime_to_file_info({_, {MTime,_}}) ->
MTime.
%% ---------------------------------------------------------------------
%% INNER FUNCTIONS
%% ---------------------------------------------------------------------
%% @doc
%% @private
readdir(Path, Cookie, CookieVerf,_MaxCnt, IsPlus, State) ->
Path4S3Dir = leo_nfs_file_handler:path_to_dir(Path),
{ok, NewCookieVerf, ReadDir} = case CookieVerf of
<< 0,0,0,0,0,0,0,0 >> ->
readdir_add_entry(Path4S3Dir);
CookieVerf ->
readdir_get_entry(CookieVerf)
end,
case ReadDir of
undefined ->
%% empty response
readdir_del_entry(NewCookieVerf),
{reply, {?NFS3_OK, {{true, s3dir2fattr3(Path)}, %% post_op_attr
NewCookieVerf, %% cookie verfier
{ %% dir_list(empty)
void, %% pre_op_attr
true %% eof
}}}, State};
ReadDir ->
%% create response
%% @TODO
%% # of entries should be determinted by _MaxCnt
{Resp, EOF} = readdir_create_resp(Path4S3Dir, Cookie, ReadDir, ?NFS_READDIR_NUM_OF_RESPONSE, IsPlus),
case EOF of
true ->
readdir_del_entry(NewCookieVerf);
false ->
void
end,
{reply, {?NFS3_OK, {{true, s3dir2fattr3(Path)}, %% post_op_attr
NewCookieVerf, %% cookie verfier
{Resp, %% pre_op_attr
EOF}
}}, State}
end.
%% @doc Returns true if Path refers to a directory which have child files,
%% and false otherwise.
%% @private
-spec(is_empty_dir(binary()) -> boolean()).
is_empty_dir(Path) ->
case leo_nfs_file_handler:list_dir(Path, false) of
{ok, MetaList} when is_list(MetaList) ->
FilteredList =
lists:foldl(
fun(Meta, Acc) ->
case Meta of
#?METADATA{del = 1} ->
Acc;
#?METADATA{dsize = -1} ->
DummyKey = filename:join(Meta#?METADATA.key,
?NFS_DUMMY_FILE4S3DIR_OLD),
case leo_gateway_rpc_handler:head(DummyKey) of
{ok, #?METADATA{del = 0}} ->
[Meta | Acc];
_ ->
DummyKey_2 = filename:join(Meta#?METADATA.key,
?NFS_DUMMY_FILE4S3DIR),
case leo_gateway_rpc_handler:head(DummyKey_2) of
{ok, #?METADATA{del = 0}} ->
[Meta | Acc];
_ ->
Acc
end
end;
_ ->
case filename:basename(Meta#?METADATA.key) of
?NFS_DUMMY_FILE4S3DIR ->
Acc;
?NFS_DUMMY_FILE4S3DIR_OLD ->
Acc;
_ ->
[Meta | Acc]
end
end
end, [], MetaList),
?debug("is_empty_dir/1", "Dir:~p, Filter:~p", [Path, FilteredList]),
length(FilteredList) =:= 0;
_Error ->
false
end.
%% @doc Returns list of file's metadatas stored in the specified Path and
%% store that with a verifier to identify and retrieve later.
%% @private
-spec(readdir_add_entry(binary()) ->
{ok, binary(), #ongoing_readdir{}}).
readdir_add_entry(Path) ->
%% @TODO READDIR does not need the attribute
case leo_nfs_file_handler:list_dir(Path) of
{ok, FileList}->
%% gen cookie verfier
CookieVerf = crypto:strong_rand_bytes(8),
ReadDir = #ongoing_readdir{filelist = FileList},
leo_nfs_readdir_state_ets_server:add_readdir_entry(CookieVerf, ReadDir),
{ok, CookieVerf, ReadDir};
Error ->
Error
end.
%% @doc Get resources correspond with a cookies verifier which returned by readdir_get_eantry
%% @private
-spec(readdir_get_entry(binary()) ->
{ok, binary(), #ongoing_readdir{} | undefined}).
readdir_get_entry(CookieVerf) ->
case leo_nfs_readdir_state_ets_server:get_readdir_entry(CookieVerf) of
not_found ->
{ok, CookieVerf, undefined};
{ok, Ret} ->
{ok, CookieVerf, Ret}
end.
%% @doc Delete resources correspond with a cookies verifier which returned by readdir_get_entry
%% @private
-spec(readdir_del_entry(binary()) -> ok).
readdir_del_entry(CookieVerf) ->
leo_nfs_readdir_state_ets_server:del_readdir_entry(CookieVerf),
ok.
%% @doc Create a rpc response for a readdir3 request
%% @private
-spec(readdir_create_resp(binary(), integer(), #ongoing_readdir{}, integer(), boolean()) ->
void | tuple()).
readdir_create_resp(Path, Cookie,
#ongoing_readdir{filelist = FileList} = ReadDir, NumEntry, IsPlus) ->
{StartCookie, EOF} = case length(FileList) =< (Cookie + NumEntry) of
true ->
{length(FileList), true};
false ->
{Cookie + NumEntry, false}
end,
readdir_create_resp(Path,
StartCookie,
ReadDir,
Cookie,
EOF,
IsPlus,
void).
%% @private
readdir_create_resp(_Path, CurCookie,
_ReadDir, Cookie,
EOF,_IsPlus, Resp) when CurCookie =:= Cookie ->
?debug("readdir_create_resp", "resp:~p ~n", [Resp]),
{Resp, EOF};
readdir_create_resp(Path, CurCookie,
#ongoing_readdir{filelist = FileList} = ReadDir,
Cookie, EOF, IsPlus, Resp) ->
Meta = lists:nth(CurCookie, FileList),
#?METADATA{key = Key, dsize = Size, del = Del} = Meta,
?debug("readdir_create_resp/7", "Meta ~p", [Meta]),
NormalizedKey = case Size of
-1 ->
%% dir to be normalized(means expand .|.. chars)
leo_nfs_file_handler:path_relative_to_abs(Key);
_ ->
%% file
Key
end,
Del2 = case Size =:= -1 andalso
is_empty_dir(NormalizedKey) of
true ->
DummyKey = filename:join(NormalizedKey, ?NFS_DUMMY_FILE4S3DIR_OLD),
case leo_gateway_rpc_handler:head(DummyKey) of
{ok, #?METADATA{del = 1}} ->
1;
_ ->
DummyKey_2 = filename:join(NormalizedKey, ?NFS_DUMMY_FILE4S3DIR),
case leo_gateway_rpc_handler:head(DummyKey_2) of
{ok, #?METADATA{del = 1}} ->
1;
_ ->
Del
end
end;
_ ->
Del
end,
FileName = filename:basename(Key),
case {Del2, FileName} of
{1,_} ->
readdir_create_resp(Path,
CurCookie - 1,
ReadDir,
Cookie,
EOF,
IsPlus,
Resp);
{_, ?NFS_DUMMY_FILE4S3DIR_OLD} ->
readdir_create_resp(Path,
CurCookie - 1,
ReadDir,
Cookie,
EOF,
IsPlus,
Resp);
{_, ?NFS_DUMMY_FILE4S3DIR} ->
readdir_create_resp(Path,
CurCookie - 1,
ReadDir,
Cookie,
EOF,
IsPlus,
Resp);
_ ->
{ok, UID} = leo_nfs_state_ets:add_path(NormalizedKey),
NewResp = case IsPlus of
true ->
{inode(NormalizedKey),
FileName,
CurCookie,
{true, %% post_op_attr
meta2fattr3(Meta#?METADATA{key = NormalizedKey})
},
{true, {UID}}, %% post_op_fh3
Resp
};
_ ->
{inode(NormalizedKey),
FileName,
CurCookie,
Resp
}
end,
?debug("readdir_create_resp", "key:~p cookie:~p~n",
[NormalizedKey, CurCookie]),
readdir_create_resp(Path,
CurCookie - 1,
ReadDir,
Cookie,
EOF,
IsPlus,
NewResp)
end.
%% @private
getattr(Path) ->
case leo_nfs_file_handler:binary_is_contained(Path, $/) of
%% object
true ->
%% @todo emulate directories
case leo_gateway_rpc_handler:head(Path) of
{ok, #?METADATA{del = 0} = Meta} ->
{ok, meta2fattr3(Meta)};
{ok, #?METADATA{del = 1}} ->
not_found;
{error, not_found} ->
Path4S3Dir = leo_nfs_file_handler:path_to_dir(Path),
case leo_nfs_file_handler:is_dir(Path4S3Dir) of
true ->
{ok, s3dir2fattr3(Path)};
false ->
not_found
end;
{error, Reason} ->
{error, Reason}
end;
%% bucket
false ->
case leo_s3_bucket:find_bucket_by_name(Path) of
{ok, Bucket} ->
Attr = bucket2fattr3(Bucket),
{ok, Attr};
not_found ->
not_found;
{error, Reason} ->
{error, Reason}
end
end.
%% @doc Return the inode of Path
%% @private
%% @todo to be replaced with truely unique id supported by LeoFS future works
-spec(inode(binary() | string()) ->
integer()).
inode(Path) ->
<< F8:8/binary,_/binary >> = erlang:md5(Path),
Hex = leo_hex:binary_to_hex(F8),
leo_hex:hex_to_integer(Hex).
%% @doc Convert from #?METADATA{} format to the format codes erpcgen generated expect
%% @private
-spec(meta2fattr3(#?METADATA{}) ->
tuple()).
meta2fattr3(#?METADATA{dsize = -1, key = Key}) ->
Path4S3Dir = leo_nfs_file_handler:path_to_dir(Key),
UT = get_dir_unix_timestamp(Path4S3Dir),
{'NF3DIR',
8#00777, %% @todo determin based on ACL? protection mode bits
2, %% # of hard links
0, %% @todo determin base on ACL? uid
0, %% @todo gid
4096, %% file size
4096, %% @todo actual size used at disk(LeoFS should return `body + metadata + header/footer`)
{0, 0}, %% data used for special file(in Linux first is major, second is minor number)
0, %% fsid
inode(Key),
{UT, 0}, %% last access
{UT, 0}, %% last modification
{UT, 0}}; %% last change
meta2fattr3(#?METADATA{key = Key, timestamp = TS, dsize = Size}) ->
UT = leo_date:greg_seconds_to_unixtime(TS),
{'NF3REG',
8#00666, %% @todo determin based on ACL? protection mode bits
1, %% # of hard links
0, %% @todo determin base on ACL? uid
0, %% @todo gid
Size, %% file size
Size, %% @todo actual size used at disk(LeoFS should return `body + metadata + header/footer`)
{0, 0}, %% data used for special file(in Linux first is major, second is minor number)
0, %% fsid
inode(Key), %% @todo Unique ID to be specifed fieldid
{UT, 0}, %% last access
{UT, 0}, %% last modification
{UT, 0}}. %% last change
%% @doc Convert from the directory path to the format codes erpcgen generated expect
%% @private
-spec(s3dir2fattr3(binary()) ->
tuple()).
s3dir2fattr3(Dir) ->
Path4S3Dir = leo_nfs_file_handler:path_to_dir(Dir),
UT = get_dir_unix_timestamp(Path4S3Dir),
{'NF3DIR',
8#00777, %% @todo determin based on ACL? protection mode bits
2, %% # of hard links
0, %% @todo determin base on ACL? uid
0, %% @todo gid
4096, %% @todo how to calc?
4096, %% @todo actual size used at disk(LeoFS should return `body + metadata + header/footer`)
{0, 0}, %% data used for special file(in Linux first is major, second is minor number)
0, %% fsid
inode(Dir), %% @todo Unique ID to be specifed fieldid
{UT, 0}, %% last access
{UT, 0}, %% last modification
{UT, 0}}. %% last change
%% @doc Convert from #?BUCKET to the format codes erpcgen generated expect
%% @private
-spec(bucket2fattr3(#?BUCKET{}) ->
tuple()).
bucket2fattr3(Bucket) ->
UT = leo_date:greg_seconds_to_unixtime(Bucket#?BUCKET.last_modified_at),
{'NF3DIR',
8#00777, %% @todo determin based on ACL? protection mode bits
3, %% # of hard links
0, %% @todo determin base on ACL? uid
0, %% @todo gid
4096, %% @todo directory size
4096, %% @todo actual size used at disk
{0, 0}, %% data used for special file(in Linux first is major, second is minor number)
0, %% fsid
inode(Bucket#?BUCKET.name), %% @todo Unique ID to be specifed fieldid
{UT, 0}, %% last access
{UT, 0}, %% last modification
{UT, 0}}.%% last change
%% @private
get_dir_unix_timestamp(_Dir) ->
%% === For 1.4
%% case leo_gateway_rpc_handler:get_dir_meta(Dir) of
%% {ok, MetaBin} ->
%% Meta = binary_to_term(MetaBin),
%% leo_date:greg_seconds_to_unixtime(Meta#?METADATA.timestamp);
%% _Error ->
%% Now = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
%% leo_date:greg_seconds_to_unixtime(Now)
%% end.
Now = calendar:datetime_to_gregorian_seconds(calendar:universal_time()),
leo_date:greg_seconds_to_unixtime(Now).