forked from apache/nuttx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfs_vfsops.c
2940 lines (2284 loc) · 76.2 KB
/
nfs_vfsops.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* fs/nfs/nfs_vfsops.c
*
* Copyright (C) 2012-2013, 2015, 2017-2018 Gregory Nutt. All rights
* reserved.
* Copyright (C) 2012 Jose Pablo Rojas Vargas. All rights reserved.
* Author: Jose Pablo Rojas Vargas <jrojas@nx-engineering.com>
* Gregory Nutt <gnutt@nuttx.org>
*
* Leveraged from OpenBSD:
*
* Copyright (c) 1989, 1993, 1995
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Rick Macklem at The University of Guelph.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/statfs.h>
#include <sys/stat.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/fs/nfs.h>
#include <nuttx/net/netconfig.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "nfs.h"
#include "rpc.h"
#include "nfs_proto.h"
#include "nfs_node.h"
#include "nfs_mount.h"
#include "xdr_subs.h"
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#define DIRENT_NFS_MAXHANDLE 64 /* Maximum length of an NFSv3 file handle */
#define DIRENT_NFS_VERFLEN 8 /* Length of the copy verifier */
/* include/nuttx/fs/dirent.h has its own version of these lengths. They must
* match the NFS versions.
*/
#if NFSX_V3FHMAX != DIRENT_NFS_MAXHANDLE
# error "Length of file handle in fs_dirent_s is incorrect"
#endif
#if NFSX_V3COOKIEVERF != DIRENT_NFS_VERFLEN
# error "Length of cookie verify in fs_dirent_s is incorrect"
#endif
#define CH_STAT_SIZE (1 << 7)
/****************************************************************************
* Private Type
****************************************************************************/
struct nfs_dir_s
{
struct fs_dirent_s nfs_base; /* VFS diretory structure */
uint8_t nfs_fhsize; /* Length of the file handle */
uint8_t nfs_fhandle[DIRENT_NFS_MAXHANDLE]; /* File handle (max size allocated) */
uint8_t nfs_verifier[DIRENT_NFS_VERFLEN]; /* Cookie verifier */
uint32_t nfs_cookie[2]; /* Cookie */
};
/****************************************************************************
* Private Data
****************************************************************************/
static uint32_t nfs_true;
static uint32_t nfs_false;
static uint32_t nfs_xdrneg1;
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int nfs_filecreate(FAR struct nfsmount *nmp,
FAR struct nfsnode *np, FAR const char *relpath,
mode_t mode);
static int nfs_filechstat(FAR struct nfsmount *nmp,
FAR struct nfsnode *np,
FAR const struct stat *buf, int flags);
static int nfs_fileopen(FAR struct nfsmount *nmp,
FAR struct nfsnode *np, FAR const char *relpath,
int oflags, mode_t mode);
static int nfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode);
static int nfs_close(FAR struct file *filep);
static ssize_t nfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t nfs_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int nfs_dup(FAR const struct file *oldp, FAR struct file *newp);
static int nfs_fsinfo(FAR struct nfsmount *nmp);
static int nfs_fstat(FAR const struct file *filep, FAR struct stat *buf);
static int nfs_fchstat(FAR const struct file *filep,
FAR const struct stat *buf, int flags);
static int nfs_truncate(FAR struct file *filep, off_t length);
static int nfs_opendir(FAR struct inode *mountpt,
FAR const char *relpath, FAR struct fs_dirent_s **dir);
static int nfs_closedir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static int nfs_readdir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir,
FAR struct dirent *entry);
static int nfs_rewinddir(FAR struct inode *mountpt,
FAR struct fs_dirent_s *dir);
static void nfs_decode_args(FAR struct nfs_mount_parameters *nprmt,
FAR struct nfs_args *argp);
static int nfs_bind(FAR struct inode *blkdriver, FAR const void *data,
FAR void **handle);
static int nfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
unsigned int flags);
static int nfs_statfs(FAR struct inode *mountpt,
FAR struct statfs *buf);
static int nfs_remove(FAR struct inode *mountpt,
FAR const char *relpath);
static int nfs_mkdir(FAR struct inode *mountpt,
FAR const char *relpath, mode_t mode);
static int nfs_rmdir(FAR struct inode *mountpt,
FAR const char *relpath);
static int nfs_rename(FAR struct inode *mountpt,
FAR const char *oldrelpath, FAR const char *newrelpath);
static mode_t nfs_stat_mode(unsigned int mode, unsigned int type);
static int nfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
FAR struct stat *buf);
static int nfs_chstat(FAR struct inode *mountpt, FAR const char *relpath,
FAR const struct stat *buf, int flags);
/****************************************************************************
* Public Data
****************************************************************************/
#ifdef CONFIG_NFS_STATISTICS
struct nfsstats nfsstats;
#endif
/* nfs vfs operations. */
const struct mountpt_operations g_nfs_operations =
{
nfs_open, /* open */
nfs_close, /* close */
nfs_read, /* read */
nfs_write, /* write */
NULL, /* seek */
NULL, /* ioctl */
NULL, /* mmap */
nfs_truncate, /* truncate */
NULL, /* sync */
nfs_dup, /* dup */
nfs_fstat, /* fstat */
nfs_fchstat, /* fchstat */
nfs_opendir, /* opendir */
nfs_closedir, /* closedir */
nfs_readdir, /* readdir */
nfs_rewinddir, /* rewinddir */
nfs_bind, /* bind */
nfs_unbind, /* unbind */
nfs_statfs, /* statfs */
nfs_remove, /* unlink */
nfs_mkdir, /* mkdir */
nfs_rmdir, /* rmdir */
nfs_rename, /* rename */
nfs_stat, /* stat */
nfs_chstat /* chstat */
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: nfs_filecreate
*
* Description:
* Create a file. This is part of the file open logic that is executed if
* the user asks to create a file.
*
* Returned Value:
* 0 on success; a negative errno value on failure.
*
****************************************************************************/
static int nfs_filecreate(FAR struct nfsmount *nmp, FAR struct nfsnode *np,
FAR const char *relpath, mode_t mode)
{
struct file_handle fhandle;
struct nfs_fattr fattr;
char filename[NAME_MAX + 1];
FAR uint32_t *ptr;
uint32_t tmp;
int namelen;
int reqlen;
int ret;
/* Find the NFS node of the directory containing the file to be created */
ret = nfs_finddir(nmp, relpath, &fhandle, &fattr, filename);
if (ret != OK)
{
ferr("ERROR: nfs_finddir returned: %d\n", ret);
return ret;
}
/* Create the CREATE RPC call arguments */
ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.create.create;
reqlen = 0;
/* Copy the variable length, directory file handle */
*ptr++ = txdr_unsigned(fhandle.length);
reqlen += sizeof(uint32_t);
memcpy(ptr, &fhandle.handle, fhandle.length);
reqlen += uint32_alignup(fhandle.length);
ptr += uint32_increment(fhandle.length);
/* Copy the variable-length file name */
namelen = strlen(filename);
*ptr++ = txdr_unsigned(namelen);
reqlen += sizeof(uint32_t);
memcpy(ptr, filename, namelen);
ptr += uint32_increment(namelen);
reqlen += uint32_alignup(namelen);
/* Set the creation mode */
*ptr++ = HTONL(NFSV3CREATE_GUARDED);
reqlen += sizeof(uint32_t);
/* Set the mode. NOTE: Here we depend on the fact that the NuttX and NFS
* bit settings are the same (at least for the bits of interest).
*/
*ptr++ = nfs_true; /* True: mode value follows */
reqlen += sizeof(uint32_t);
tmp = mode & (NFSMODE_IXOTH | NFSMODE_IWOTH | NFSMODE_IROTH |
NFSMODE_IXGRP | NFSMODE_IWGRP | NFSMODE_IRGRP |
NFSMODE_IXUSR | NFSMODE_IWUSR | NFSMODE_IRUSR |
NFSMODE_SAVETEXT | NFSMODE_ISGID | NFSMODE_ISUID);
*ptr++ = txdr_unsigned(tmp);
reqlen += sizeof(uint32_t);
/* Set the user ID to zero */
*ptr++ = nfs_true; /* True: Uid value follows */
*ptr++ = 0; /* UID = 0 (nobody) */
reqlen += 2*sizeof(uint32_t);
/* Set the group ID to one */
*ptr++ = nfs_true; /* True: Gid value follows */
*ptr++ = 0; /* GID = 0 (nogroup) */
reqlen += 2*sizeof(uint32_t);
/* Set the size to zero */
*ptr++ = nfs_true; /* True: Size value follows */
*ptr++ = 0; /* Size = 0 */
*ptr++ = 0;
reqlen += 3*sizeof(uint32_t);
/* Don't change times */
*ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE); /* Don't change atime */
*ptr++ = HTONL(NFSV3SATTRTIME_DONTCHANGE); /* Don't change mtime */
reqlen += 2*sizeof(uint32_t);
/* Send the NFS request. */
nfs_statistics(NFSPROC_CREATE);
ret = nfs_request(nmp, NFSPROC_CREATE,
&nmp->nm_msgbuffer.create, reqlen,
nmp->nm_iobuffer, nmp->nm_buflen);
/* Check for success */
if (ret == OK)
{
/* Parse the returned data */
ptr = (FAR uint32_t *)&((FAR struct rpc_reply_create *)
nmp->nm_iobuffer)->create;
/* Save the file handle in the file data structure */
tmp = *ptr++; /* handle_follows */
if (!tmp)
{
ferr("ERROR: no file handle follows\n");
return -EINVAL;
}
tmp = *ptr++;
tmp = fxdr_unsigned(uint32_t, tmp);
DEBUGASSERT(tmp <= NFSX_V3FHMAX);
np->n_fhsize = (uint8_t)tmp;
memcpy(&np->n_fhandle, ptr, tmp);
ptr += uint32_increment(tmp);
/* Save the attributes in the file data structure */
tmp = *ptr; /* attributes_follows */
if (!tmp)
{
fwarn("WARNING: no file attributes\n");
}
else
{
/* Initialize the file attributes */
nfs_attrupdate(np, (FAR struct nfs_fattr *)ptr);
}
/* Any following dir_wcc data is ignored for now */
}
return ret;
}
/****************************************************************************
* Name: nfs_filechstat
*
* Description:
* Change the status of an open file. This is part of the file open logic.
*
* Returned Value:
* 0 on success; a negative errno value on failure.
*
****************************************************************************/
static int nfs_filechstat(FAR struct nfsmount *nmp, FAR struct nfsnode *np,
FAR const struct stat *buf, int flags)
{
FAR uint32_t *ptr;
int reqlen;
int ret;
finfo("Changing file status\n");
/* Create the SETATTR RPC call arguments */
ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.setattr.setattr;
reqlen = 0;
/* Copy the variable length, directory file handle */
*ptr++ = txdr_unsigned(np->n_fhsize);
reqlen += sizeof(uint32_t);
memcpy(ptr, &np->n_fhandle, np->n_fhsize);
reqlen += uint32_alignup(np->n_fhsize);
ptr += uint32_increment(np->n_fhsize);
/* Copy the variable-length attributes */
if (flags & CH_STAT_MODE)
{
*ptr++ = nfs_true;
*ptr++ = txdr_unsigned(buf->st_mode);
reqlen += 2 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
if (flags & CH_STAT_UID)
{
*ptr++ = nfs_true;
*ptr++ = txdr_unsigned(buf->st_uid);
reqlen += 2 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
if (flags & CH_STAT_GID)
{
*ptr++ = nfs_true;
*ptr++ = txdr_unsigned(buf->st_gid);
reqlen += 2 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
if (flags & CH_STAT_SIZE)
{
*ptr++ = nfs_true;
txdr_hyper(buf->st_size, ptr);
ptr += 2;
reqlen += 3 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
if (flags & CH_STAT_ATIME)
{
*ptr++ = nfs_true;
txdr_nfsv3time(&buf->st_atim, ptr);
ptr += 2;
reqlen += 3 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
if (flags & CH_STAT_MTIME)
{
*ptr++ = nfs_true;
txdr_nfsv3time(&buf->st_mtim, ptr);
ptr += 2;
reqlen += 3 * sizeof(uint32_t);
}
else
{
*ptr++ = nfs_false;
reqlen += sizeof(uint32_t);
}
*ptr++ = nfs_false; /* No guard value */
reqlen += sizeof(uint32_t);
/* Perform the SETATTR RPC */
nfs_statistics(NFSPROC_SETATTR);
ret = nfs_request(nmp, NFSPROC_SETATTR,
&nmp->nm_msgbuffer.setattr, reqlen,
nmp->nm_iobuffer, nmp->nm_buflen);
if (ret != OK)
{
ferr("ERROR: nfs_request failed: %d\n", ret);
return ret;
}
/* Get a pointer to the SETATTR reply data */
ptr = (FAR uint32_t *)&((FAR struct rpc_reply_setattr *)
nmp->nm_iobuffer)->setattr;
/* Parse file_wcc. First, check if WCC attributes follow. */
if (*ptr++ != 0)
{
/* Yes.. WCC attributes follow. But we just skip over them. */
ptr += uint32_increment(sizeof(struct wcc_attr));
}
/* Check if normal file attributes follow */
if (*ptr++ != 0)
{
/* Yes.. Update the cached file status in the file structure. */
nfs_attrupdate(np, (FAR struct nfs_fattr *)ptr);
}
return OK;
}
/****************************************************************************
* Name: nfs_fileopen
*
* Description:
* Open a file. This is part of the file open logic that attempts to open
* an existing file.
*
* Returned Value:
* 0 on success; a negative errno value on failure.
*
****************************************************************************/
static int nfs_fileopen(FAR struct nfsmount *nmp, FAR struct nfsnode *np,
FAR const char *relpath, int oflags, mode_t mode)
{
struct file_handle fhandle;
struct nfs_fattr fattr;
uint32_t tmp;
int ret = 0;
/* Find the NFS node associate with the path */
ret = nfs_findnode(nmp, relpath, &fhandle, &fattr, NULL);
if (ret != OK)
{
ferr("ERROR: nfs_findnode returned: %d\n", ret);
return ret;
}
/* Check if the object is a directory */
tmp = fxdr_unsigned(uint32_t, fattr.fa_type);
if (tmp == NFDIR)
{
/* Exit with EISDIR if we attempt to open a directory */
ferr("ERROR: Path is a directory\n");
return -EISDIR;
}
/* Check if the caller has sufficient privileges to open the file */
if ((oflags & O_WRONLY) != 0)
{
/* Check if anyone has privileges to write to the file -- owner,
* group, or other (we are probably "other" and may still not be
* able to write).
*/
tmp = fxdr_unsigned(uint32_t, fattr.fa_mode);
if ((tmp & (NFSMODE_IWOTH | NFSMODE_IWGRP | NFSMODE_IWUSR)) == 0)
{
ferr("ERROR: File is read-only: %08" PRIx32 "\n", tmp);
return -EACCES;
}
}
/* It would be an ret if we are asked to create the file exclusively */
if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
{
/* Already exists -- can't create it exclusively */
ferr("ERROR: File exists\n");
return -EEXIST;
}
/* Initialize the file private data.
*
* Copy the file handle.
*/
np->n_fhsize = (uint8_t)fhandle.length;
memcpy(&np->n_fhandle, &fhandle.handle, fhandle.length);
/* Save the file attributes */
nfs_attrupdate(np, &fattr);
/* If O_TRUNC is specified and the file is opened for writing,
* then truncate the file. This operation requires that the file is
* writable, but we have already checked that. O_TRUNC without write
* access is ignored.
*/
if ((oflags & (O_TRUNC | O_WRONLY)) == (O_TRUNC | O_WRONLY))
{
struct stat buf;
/* Truncate the file to zero length. I think we can do this with
* the SETATTR call by setting the length to zero.
*/
buf.st_size = 0;
return nfs_filechstat(nmp, np, &buf, CH_STAT_SIZE);
}
return OK;
}
/****************************************************************************
* Name: nfs_open
*
* Description:
* If oflags == O_CREAT it creates a file, if not it check to see if the
* type is ok and that deletion is not in progress.
*
* Returned Value:
* 0 on success; a negated errno value on failure.
*
****************************************************************************/
static int nfs_open(FAR struct file *filep, FAR const char *relpath,
int oflags, mode_t mode)
{
FAR struct nfsmount *nmp;
FAR struct nfsnode *np;
int ret;
/* Sanity checks */
DEBUGASSERT(filep->f_inode != NULL);
/* Get the mountpoint inode reference from the file structure and the
* mountpoint private data from the inode structure
*/
nmp = (FAR struct nfsmount *)filep->f_inode->i_private;
DEBUGASSERT(nmp != NULL);
/* Pre-allocate the file private data to describe the opened file. */
np = (FAR struct nfsnode *)kmm_zalloc(sizeof(struct nfsnode));
if (!np)
{
ferr("ERROR: Failed to allocate private data\n");
return -ENOMEM;
}
ret = nxmutex_lock(&nmp->nm_lock);
if (ret < 0)
{
kmm_free(np);
return ret;
}
/* Try to open an existing file at that path */
ret = nfs_fileopen(nmp, np, relpath, oflags, mode);
if (ret != OK)
{
/* An ret occurred while trying to open the existing file. Check if
* the open failed because the file does not exist. That is not
* necessarily an ret; that may only mean that we have to create the
* file.
*/
if (ret != -ENOENT)
{
ferr("ERROR: nfs_fileopen failed: %d\n", ret);
goto errout_with_lock;
}
/* The file does not exist. Check if we were asked to create the file.
* If the O_CREAT bit is set in the oflags then we should create the
* file if it does not exist.
*/
if ((oflags & O_CREAT) == 0)
{
/* Return ENOENT if the file does not exist and we were not asked
* to create it.
*/
ferr("ERROR: File does not exist\n");
goto errout_with_lock;
}
/* Create the file */
ret = nfs_filecreate(nmp, np, relpath, mode);
if (ret != OK)
{
ferr("ERROR: nfs_filecreate failed: %d\n", ret);
goto errout_with_lock;
}
}
/* Initialize the file private data (only need to initialize
* non-zero elements)
*/
np->n_crefs = 1;
/* Attach the private data to the struct file instance */
filep->f_priv = np;
/* Then insert the new instance at the head of the list in the mountpoint
* structure. It needs to be there (1) to handle error conditions that
* effect all files, and (2) to inform the umount logic that we are busy.
* We cannot unmount the file system if this list is not empty!
*/
np->n_next = nmp->nm_head;
nmp->nm_head = np;
nxmutex_unlock(&nmp->nm_lock);
return OK;
errout_with_lock:
if (np)
{
kmm_free(np);
}
nxmutex_unlock(&nmp->nm_lock);
return ret;
}
/****************************************************************************
* Name: nfs_close
*
* Description:
* Close a file.
*
* Returned Value:
* 0 on success; a negated errno value on failure.
*
****************************************************************************/
static int nfs_close(FAR struct file *filep)
{
FAR struct nfsmount *nmp;
FAR struct nfsnode *np;
FAR struct nfsnode *prev;
FAR struct nfsnode *curr;
int ret;
/* Sanity checks */
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
/* Recover our private data from the struct file instance */
nmp = (FAR struct nfsmount *)filep->f_inode->i_private;
np = (FAR struct nfsnode *)filep->f_priv;
DEBUGASSERT(nmp != NULL);
/* Get exclusive access to the mount structure. */
ret = nxmutex_lock(&nmp->nm_lock);
if (ret < 0)
{
return ret;
}
/* Decrement the reference count. If the reference count would not
* decrement to zero, then that is all we have to do.
*/
if (np->n_crefs > 1)
{
np->n_crefs--;
ret = OK;
}
/* There are no more references to the file structure. Now we need to
* free up all resources associated with the open file.
*
* First, find our file structure in the list of file structures
* contained in the mount structure.
*/
else
{
/* Assume file structure won't be found. This should never happen. */
ret = -EINVAL;
for (prev = NULL, curr = nmp->nm_head;
curr;
prev = curr, curr = curr->n_next)
{
/* Check if this node is ours */
if (np == curr)
{
/* Yes.. remove it from the list of file structures */
if (prev)
{
/* Remove from mid-list */
prev->n_next = np->n_next;
}
else
{
/* Remove from the head of the list */
nmp->nm_head = np->n_next;
}
/* Then deallocate the file structure and return success */
kmm_free(np);
ret = OK;
break;
}
}
}
filep->f_priv = NULL;
nxmutex_unlock(&nmp->nm_lock);
return ret;
}
/****************************************************************************
* Name: nfs_read
*
* Returned Value:
* The (non-negative) number of bytes read on success; a negated errno
* value on failure.
*
****************************************************************************/
static ssize_t nfs_read(FAR struct file *filep, FAR char *buffer,
size_t buflen)
{
FAR struct nfsmount *nmp;
FAR struct nfsnode *np;
ssize_t readsize;
ssize_t tmp;
ssize_t bytesread;
size_t reqlen;
FAR uint32_t *ptr;
int ret = 0;
finfo("Read %zu bytes from offset %jd\n",
buflen, (intmax_t)filep->f_pos);
/* Sanity checks */
DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
/* Recover our private data from the struct file instance */
nmp = (FAR struct nfsmount *)filep->f_inode->i_private;
np = (FAR struct nfsnode *)filep->f_priv;
DEBUGASSERT(nmp != NULL);
ret = nxmutex_lock(&nmp->nm_lock);
if (ret < 0)
{
return (ssize_t)ret;
}
/* Get the number of bytes left in the file and truncate read count so that
* it does not exceed the number of bytes left in the file.
*/
tmp = np->n_size - filep->f_pos;
if (buflen > tmp)
{
buflen = tmp;
finfo("Read size truncated to %zu\n", buflen);
}
/* Now loop until we fill the user buffer (or hit the end of the file) */
for (bytesread = 0; bytesread < buflen; )
{
/* Make sure that the attempted read size does not exceed the RPC
* maximum
*/
readsize = buflen - bytesread;
if (readsize > nmp->nm_rsize)
{
readsize = nmp->nm_rsize;
}
/* Make sure that the attempted read size does not exceed the IO buffer
* size
*/
tmp = SIZEOF_rpc_reply_read(readsize);
if (tmp > nmp->nm_buflen)
{
readsize -= (tmp - nmp->nm_buflen);
}
/* Initialize the request */
ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.read.read;
reqlen = 0;
/* Copy the variable length, file handle */
*ptr++ = txdr_unsigned((uint32_t)np->n_fhsize);
reqlen += sizeof(uint32_t);
memcpy(ptr, &np->n_fhandle, np->n_fhsize);
reqlen += uint32_alignup(np->n_fhsize);
ptr += uint32_increment(np->n_fhsize);
/* Copy the file offset */
txdr_hyper((uint64_t)filep->f_pos, ptr);
ptr += 2;
reqlen += 2*sizeof(uint32_t);
/* Set the readsize */
*ptr = txdr_unsigned(readsize);
reqlen += sizeof(uint32_t);
/* Perform the read */
finfo("Reading %zu bytes\n", readsize);
nfs_statistics(NFSPROC_READ);
ret = nfs_request(nmp, NFSPROC_READ,
&nmp->nm_msgbuffer.read, reqlen,
nmp->nm_iobuffer, nmp->nm_buflen);
if (ret)
{
ferr("ERROR: nfs_request failed: %d\n", ret);
goto errout_with_lock;
}
/* The read was successful. Get a pointer to the beginning of the NFS
* response data.
*/
ptr = (FAR uint32_t *)
&((FAR struct rpc_reply_read *)nmp->nm_iobuffer)->read;
/* Check if attributes are included in the responses */
tmp = *ptr++;
if (tmp != 0)
{
/* Yes.. Update the cached file status in the file structure. */
nfs_attrupdate(np, (FAR struct nfs_fattr *)ptr);
ptr += uint32_increment(sizeof(struct nfs_fattr));
}
/* This is followed by the count of data read. Isn't this
* the same as the length that is included in the read data?
*
* Just skip over if for now.
*/
ptr++;
/* Next comes an EOF indication. Save that in tmp for now. */
tmp = *ptr++;
/* Then the length of the read data followed by the read data itself */