xref: /third_party/NuttX/fs/nfs/nfs_proto.h (revision beacf11b)
1/****************************************************************************
2 * fs/nfs/nfs_proto.h
3 *
4 *   Copyright (C) 2012, 2018 Gregory Nutt. All rights reserved.
5 *   Copyright (C) 2012 Jose Pablo Rojas Vargas. All rights reserved.
6 *   Author: Jose Pablo Rojas Vargas <jrojas@nx-engineering.com>
7 *           Gregory Nutt <gnutt@nuttx.org>
8 *
9 * Leveraged from OpenBSD:
10 *
11 *   Copyright (c) 1989, 1993
12 *   The Regents of the University of California.  All rights reserved.
13 *
14 * This code is derived from software contributed to Berkeley by
15 * Rick Macklem at The University of Guelph.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 *
21 * 1. Redistributions of source code must retain the above copyright
22 *    notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 *    notice, this list of conditions and the following disclaimer in the
25 *    documentation and/or other materials provided with the distribution.
26 * 3. Neither the name of the University nor the names of its contributors
27 *    may be used to endorse or promote products derived from this software
28 *    without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
41 *
42 ****************************************************************************/
43
44#ifndef __FS_NFS_NFS_PROTO_H
45#define __FS_NFS_NFS_PROTO_H
46
47/****************************************************************************
48 * Included Files
49 ****************************************************************************/
50#include <limits.h>
51
52#ifdef __cplusplus
53#if __cplusplus
54extern "C" {
55#endif /* __cplusplus */
56#endif /* __cplusplus */
57
58/****************************************************************************
59 * Pre-processor Definitions
60 ****************************************************************************/
61
62/* Constants as defined in the Sun NFS Version 2 and 3 specs.
63 * "NFS: Network File System Protocol Specification" RFC1094
64 * and in the "NFS: Network File System Version 3 Protocol
65 * Specification"
66 */
67
68#define NFS_PORT                  2049
69#define NFS_PROG                  100003
70#define NFS_VER2                  2
71#define NFS_VER3                  3
72#define NFS_VER4                  4
73#define NFS_MAXDGRAMDATA          32768
74#define MAXBSIZE                  64000
75#define NFS_MAXDATA               MAXBSIZE
76#define NFS_MAXPATHLEN            1024
77#define NFS_MAXNAMLEN             255
78#define NFS_MAXPKTHDR             404
79#define NFS_MAXPACKET             (NFS_MAXPKTHDR + NFS_MAXDATA)
80#define NFS_MINPACKET             20
81#define NFS_FABLKSIZE             512   /* Size in bytes of a block wrt fa_blocks */
82#define NFS_MOUNT_PATH_MAX_SIZE PATH_MAX
83
84/* Stat numbers for rpc returns (version 2 and 3) */
85
86#define NFS_OK                    0
87#define NFSERR_PERM               1
88#define NFSERR_NOENT              2
89#define NFSERR_IO                 5
90#define NFSERR_NXIO               6
91#define NFSERR_ACCES              13
92#define NFSERR_EXIST              17
93#define NFSERR_XDEV               18    /* Version 3 only */
94#define NFSERR_NODEV              19
95#define NFSERR_NOTDIR             20
96#define NFSERR_ISDIR              21
97#define NFSERR_INVAL              22    /* Version 3 only */
98#define NFSERR_FBIG               27
99#define NFSERR_NOSPC              28
100#define NFSERR_ROFS               30
101#define NFSERR_MLINK              31    /* Version 3 only */
102#define NFSERR_NAMETOL            63
103#define NFSERR_NOTEMPTY           66
104#define NFSERR_DQUOT              69
105#define NFSERR_STALE              70
106#define NFSERR_REMOTE             71    /* Version 3 only */
107#define NFSERR_WFLUSH             99    /* Version 2 only */
108#define NFSERR_BADHANDLE          10001 /* The rest Version 3 only */
109#define NFSERR_NOT_SYNC           10002
110#define NFSERR_BAD_COOKIE         10003
111#define NFSERR_NOTSUPP            10004
112#define NFSERR_TOOSMALL           10005
113#define NFSERR_SERVERFAULT        10006
114#define NFSERR_BADTYPE            10007
115#define NFSERR_JUKEBOX            10008
116#define NFSERR_TRYLATER           NFSERR_JUKEBOX
117#define NFSERR_STALEWRITEVERF     30001 /* Fake return for nfs_commit() */
118
119#define NFSERR_RETVOID            0x20000000    /* Return void, not error */
120#define NFSERR_AUTHERR            0x40000000    /* Mark an authentication error */
121#define NFSERR_RETERR             0x80000000    /* Mark an error return for V3 */
122
123/* Sizes in bytes of various NFS RPC components */
124
125#define NFSX_UNSIGNED             4
126
127/* Specific to NFS Version 3 */
128
129#define NFSX_V3FH                 (sizeof(fhandle_t))                     /* size this server uses */
130#define NFSX_V3FHMAX              64                                      /* max. allowed by protocol */
131#define NFSX_V3FATTR              84
132#define NFSX_V3SATTR              60                                      /* max. all fields filled in */
133#define NFSX_V3SRVSATTR           (sizeof (struct nfsv3_sattr))
134#define NFSX_V3POSTOPATTR         (NFSX_V3FATTR + NFSX_UNSIGNED)
135#define NFSX_V3WCCDATA            (NFSX_V3POSTOPATTR + 8 * NFSX_UNSIGNED)
136#define NFSX_V3COOKIEVERF         8
137#define NFSX_V3WRITEVERF          8
138#define NFSX_V3CREATEVERF         8
139#define NFSX_V3STATFS             52
140#define NFSX_V3FSINFO             48
141#define NFSX_V3PATHCONF           24
142
143/* NFS RPC procedure numbers (before version mapping) */
144
145#define NFSPROC_NULL              0
146#define NFSPROC_GETATTR           1
147#define NFSPROC_SETATTR           2
148#define NFSPROC_LOOKUP            3
149#define NFSPROC_ACCESS            4
150#define NFSPROC_READLINK          5
151#define NFSPROC_READ              6
152#define NFSPROC_WRITE             7
153#define NFSPROC_CREATE            8
154#define NFSPROC_MKDIR             9
155#define NFSPROC_SYMLINK           10
156#define NFSPROC_MKNOD             11
157#define NFSPROC_REMOVE            12
158#define NFSPROC_RMDIR             13
159#define NFSPROC_RENAME            14
160#define NFSPROC_LINK              15
161#define NFSPROC_READDIR           16
162#define NFSPROC_READDIRPLUS       17
163#define NFSPROC_FSSTAT            18
164#define NFSPROC_FSINFO            19
165#define NFSPROC_PATHCONF          20
166#define NFSPROC_COMMIT            21
167#define NFSPROC_NOOP              22
168#define NFS_NPROCS                23
169
170/* Constants used by the Version 3 protocol for various RPCs */
171
172#define NFSV3SATTRTIME_DONTCHANGE 0
173#define NFSV3SATTRTIME_TOSERVER   1
174#define NFSV3SATTRTIME_TOCLIENT   2
175
176#define NFSV3ACCESS_READ          0x01
177#define NFSV3ACCESS_LOOKUP        0x02
178#define NFSV3ACCESS_MODIFY        0x04
179#define NFSV3ACCESS_EXTEND        0x08
180#define NFSV3ACCESS_DELETE        0x10
181#define NFSV3ACCESS_EXECUTE       0x20
182
183#define NFSV3WRITE_UNSTABLE       0
184#define NFSV3WRITE_DATASYNC       1
185#define NFSV3WRITE_FILESYNC       2
186
187#define NFSV3CREATE_UNCHECKED     0
188#define NFSV3CREATE_GUARDED       1
189#define NFSV3CREATE_EXCLUSIVE     2
190
191#define NFSV3FSINFO_LINK          0x01
192#define NFSV3FSINFO_SYMLINK       0x02
193#define NFSV3FSINFO_HOMOGENEOUS   0x08
194#define NFSV3FSINFO_CANSETTIME    0x10
195
196/* Conversion macros */
197
198#define vtonfsv3_mode(m)         txdr_unsigned((m) & 07777)
199#define nfstov_mode(a)           (fxdr_unsigned(u_int16_t, (a))&07777)
200#define vtonfsv3_type(a)         txdr_unsigned(nfsv3_type[((int32_t)(a))])
201#define nfsv3tov_type(a)         nv3tov_type[fxdr_unsigned(uint32_t,(a))&0x7]
202
203/* Mode bit values */
204
205#define NFSMODE_IXOTH             (1 << 0)      /* Execute permission for others on a file */
206#define NFSMODE_IWOTH             (1 << 1)      /* Write permission for others */
207#define NFSMODE_IROTH             (1 << 2)      /* Read permission for others */
208#define NFSMODE_IXGRP             (1 << 3)      /* Execute permission for group on a file */
209#define NFSMODE_IWGRP             (1 << 4)      /* Write permission for group */
210#define NFSMODE_IRGRP             (1 << 5)      /* Read permission for group */
211#define NFSMODE_IXUSR             (1 << 6)      /* Execute permission for owner on a file */
212#define NFSMODE_IWUSR             (1 << 7)      /* Write permission for owner */
213#define NFSMODE_IRUSR             (1 << 8)      /* Read permission for owner */
214#define NFSMODE_SAVETEXT          (1 << 9)      /* Save swapped text */
215#define NFSMODE_ISGID             (1 << 10)     /* Set group ID on execution */
216#define NFSMODE_ISUID             (1 << 11)     /* Set user ID on execution */
217
218#define DIRENT_NFS_MAXHANDLE     64   /* Maximum length of an NFSv3 file handle */
219#define DIRENT_NFS_VERFLEN       8    /* Length of the copy verifier */
220
221/* File identifier */
222
223#define MAXFIDSZ                  16
224
225/****************************************************************************
226 * Public Types
227 ****************************************************************************/
228
229/* File types */
230
231typedef enum
232{
233  NFNON  = 0,                  /* Unknown type */
234  NFREG  = 1,                  /* Regular file */
235  NFDIR  = 2,                  /* Directory */
236  NFBLK  = 3,                  /* Block special device file */
237  NFCHR  = 4,                  /* Character special device file */
238  NFLNK  = 5,                  /* Symbolic link */
239  NFSOCK = 6,                  /* Socket */
240  NFFIFO = 7                   /* Named FIFO */
241} nfstype;
242
243/* File Handle variable is up to 64 bytes for version 3. This structures a
244 * ariable sized and are provided only for setting aside maximum memory
245 * allocations for a file handle.
246 */
247
248struct nfsfh
249{
250  uint8_t fh_bytes[NFSX_V3FHMAX];
251};
252typedef struct nfsfh nfsfh_t;
253#define SIZEOF_nfsfh_t(n) (n)
254
255struct nfsv3_time
256{
257  uint32_t nfsv3_sec;
258  uint32_t nfsv3_nsec;
259};
260typedef struct nfsv3_time nfstime3;
261
262/* Quads are defined as arrays of 2 longs to ensure dense packing for the
263 * protocol and to facilitate xdr conversion.
264 */
265
266struct nfs_uquad
267{
268  uint32_t nfsuquad[2];
269};
270typedef struct nfs_uquad nfsuint64;
271
272/* NFS Version 3 special file number. */
273
274struct nfsv3_spec
275{
276  uint32_t specdata1;
277  uint32_t specdata2;
278};
279typedef struct nfsv3_spec nfsv3spec;
280
281/* File attributes and setable attributes. These structures cover both
282 * NFS version 2 and the version 3 protocol. Note that the union is only
283 * used so that one pointer can refer to both variants. These structures
284 * go out on the wire and must be densely packed, so no quad data types
285 * are used. (all fields are longs or u_longs or structures of same)
286 */
287
288struct nfs_fattr
289{
290  uint32_t           fa_type;
291  uint32_t           fa_mode;
292  uint32_t           fa_nlink;
293  uint32_t           fa_uid;
294  uint32_t           fa_gid;
295  nfsuint64          fa_size;
296  nfsuint64          fa_used;
297  nfsv3spec          fa_rdev;
298  nfsuint64          fa_fsid;
299  nfsuint64          fa_fileid;
300  nfstime3           fa_atime;
301  nfstime3           fa_mtime;
302  nfstime3           fa_ctime;
303};
304
305/* NFS Version 3 sattr structure for the new node creation case.  This is the
306 * maximum size of the attributes; the actual size may vary if values are not
307 * include.
308 */
309
310struct nfsv3_sattr
311{
312  uint32_t           sa_modefollows;   /* TRUE: Mode value follows */
313  uint32_t           sa_mode;          /* Mode value */
314  uint32_t           sa_uidfollows;    /* TRUE: Uid value follows */
315  uint32_t           sa_uid;           /* Uid value */
316  uint32_t           sa_gidfollows;    /* TRUE: Mode value follows */
317  uint32_t           sa_gid;           /* Mode value */
318  uint32_t           sa_sizefollows;   /* TRUE: Size value follows */
319  uint32_t           sa_size;          /* Size value */
320  uint32_t           sa_atimetype;     /* Don't change, use server timer, or use client time  */
321  nfstime3           sa_atime;         /* Client time */
322  uint32_t           sa_mtimetype;     /* Don't change, use server timer, or use client time  */
323  nfstime3           sa_mtime;         /* Client time */
324};
325
326struct nfs_statfs
327{
328  uint32_t           attributes_follow;
329  struct nfs_fattr   obj_attributes;
330  nfsuint64          sf_tbytes;
331  nfsuint64          sf_fbytes;
332  nfsuint64          sf_abytes;
333  nfsuint64          sf_tfiles;
334  nfsuint64          sf_ffiles;
335  nfsuint64          sf_afiles;
336  uint32_t           sf_invarsec;
337};
338
339struct nfs_statfs_ctx
340{
341  nfsuint64          sf_tbytes;
342  nfsuint64          sf_fbytes;
343  nfsuint64          sf_abytes;
344  nfsuint64          sf_tfiles;
345  nfsuint64          sf_ffiles;
346  nfsuint64          sf_afiles;
347  uint32_t           sf_invarsec;
348};
349
350
351struct entry3
352{
353  uint32_t        file_id[2];
354  uint32_t        name_len;
355  uint8_t         *contents;
356  uint32_t        cookie[2];
357  struct entry3   *next;
358};
359
360struct nfsdir_s
361{
362  struct nfsdir_s*   nfs_next;                            /* A list of opened directories. */
363  uint8_t            nfs_fhsize;                          /* Length of the file handle */
364  uint8_t            nfs_fhandle[DIRENT_NFS_MAXHANDLE];   /* File handle (max size allocated) */
365  uint8_t            nfs_verifier[DIRENT_NFS_VERFLEN];    /* Cookie verifier */
366  uint32_t           nfs_cookie[2];                       /* Cookie */
367  struct fs_dirent_s *nfs_dir;                            /* Directory pointer from VFS */
368  struct entry3      *nfs_entries;                        /* Directory pointer from VFS */
369};
370
371struct post_attr
372{
373  uint32_t           obj_attribute_follow;
374  struct nfs_fattr   attributes;
375};
376
377struct nfsv3_fsinfo
378{
379  struct post_attr   obj_attributes;
380  uint32_t           fs_rtmax;
381  uint32_t           fs_rtpref;
382  uint32_t           fs_rtmult;
383  uint32_t           fs_wtmax;
384  uint32_t           fs_wtpref;
385  uint32_t           fs_wtmult;
386  uint32_t           fs_dtpref;
387  nfsuint64          fs_maxfilesize;
388  nfstime3           fs_timedelta;
389  uint32_t           fs_properties;
390};
391
392struct nfs_fsinfo
393{
394  uint32_t           fs_rtmax;
395  uint32_t           fs_rtpref;
396  uint32_t           fs_rtmult;
397  uint32_t           fs_wtmax;
398  uint32_t           fs_wtpref;
399  uint32_t           fs_wtmult;
400  uint32_t           fs_dtpref;
401  nfsuint64          fs_maxfilesize;
402  nfstime3           fs_timedelta;
403  uint32_t           fs_properties;
404};
405
406/* NFS procedures args */
407
408struct wcc_attr
409{
410  nfsuint64          size;
411  nfstime3           mtime;
412  nfstime3           ctime;
413};
414
415struct wcc_data
416{
417  uint32_t           wcc_attr_follows;          /* True if data follows */
418  struct wcc_attr    before;
419  uint32_t           nfs_attr_follow;           /* True if attributes present */
420  struct nfs_fattr   after;
421};
422
423struct file_handle
424{
425  uint32_t           length;
426  nfsfh_t            handle;
427};
428#define SIZEOF_file_handle(n) (sizeof(uint32_t) + SIZEOF_nfsfh_t(n))
429
430struct diropargs3
431{
432  struct file_handle fhandle;                   /* Variable length */
433  uint32_t           length;                    /* Size of name[]  */
434  uint32_t           name[(NAME_MAX + 3) >> 2]; /* Variable length */
435};
436
437struct CREATE3args
438{
439  struct diropargs3  where;
440  uint32_t           create_mode;
441  struct nfsv3_sattr how;
442};
443
444struct CREATE3resok
445{
446  uint32_t           handle_follows;           /* True, handle follows */
447  struct file_handle fhandle;                  /* Variable length */
448  uint32_t           attributes_follows;       /* True, attributes follows */
449  struct nfs_fattr   attributes;               /* File attributes */
450  struct wcc_data    dir_wcc;
451};
452
453/* The actual size of the lookup argument is variable.
454 * These structures are, therefore, only useful in setting
455 * aside maximum memory usage for the LOOKUP arguments.
456 */
457
458struct LOOKUP3filename
459{
460  uint32_t           namelen;                   /* Size of name[]  */
461  uint32_t           name[(NAME_MAX + 3) >> 2]; /* Variable length */
462};
463
464struct LOOKUP3args
465{
466  struct file_handle     dirhandle;             /* Variable length */
467  struct LOOKUP3filename name;                  /* Variable length */
468};
469
470struct SETATTR3args
471{
472  struct file_handle     fhandle;              /* Variable length */
473  struct nfsv3_sattr     new_attributes;       /* Variable length */
474  uint32_t               guard;                /* Guard value */
475};
476
477struct SETATTR3resok
478{
479  struct wcc_data         wcc_data;
480};
481
482/* Actual size of LOOKUP3args */
483
484#define SIZEOF_LOOKUP3filename(b) (sizeof(uint32_t) + (((b) + 3) & ~3))
485#define SIZEOF_LOOKUP3args(a,b)   (SIZEOF_file_handle(a) + SIZEOF_LOOKUP3filename(b))
486
487struct LOOKUP3resok
488{
489  struct file_handle fhandle;
490  uint32_t           obj_attributes_follow;
491  struct nfs_fattr   obj_attributes;
492  uint32_t           dir_attributes_follow;
493  struct nfs_fattr   dir_attributes;
494};
495
496struct READ3args
497{
498  struct file_handle fhandle;           /* Variable length */
499  nfsuint64           offset;
500  uint32_t           count;
501};
502
503struct nfs_rdhdr_s
504{
505  uint32_t           attributes_follow;
506  struct nfs_fattr   attributes;       /* Will not be present if attributes_follow == 0 */
507  uint32_t           count;            /* Number of bytes read */
508  uint32_t           eof;              /* Non-zero if at the end of file */
509  uint32_t           length;           /* Length of data (same as count?) */
510};
511
512struct READ3resok
513{
514  struct nfs_rdhdr_s hdr;
515  uint8_t            data[1];          /* Actual data size depends on count */
516};
517#define SIZEOF_READ3resok(n) (sizeof(struct nfs_rdhdr_s) + (n))
518
519struct nfs_wrhdr_s
520{
521  struct file_handle fhandle;            /* Variable length */
522  nfsuint64           offset;
523  uint32_t           count;
524  uint32_t           stable;
525};
526
527struct WRITE3args
528{
529  struct nfs_wrhdr_s hdr;
530  uint8_t            data[1];              /* Actual data size depends on count */
531};
532#define SIZEOF_WRITE3args(n) (sizeof(struct nfs_wrhdr_s) + (n))
533
534struct WRITE3resok
535{
536  struct wcc_data    file_wcc;
537  uint32_t           count;
538  uint32_t           committed;
539  uint8_t            verf[NFSX_V3WRITEVERF];
540};
541
542struct REMOVE3args
543{
544  struct diropargs3  object;
545};
546
547struct REMOVE3resok
548{
549  struct wcc_data    dir_wcc;
550};
551
552struct RENAME3args
553{
554  struct diropargs3  from;
555  struct diropargs3  to;
556};
557
558struct RENAME3resok
559{
560  struct wcc_data    fromdir_wcc;
561  struct wcc_data    todir_wcc;
562};
563
564struct MKDIR3args
565{
566  struct diropargs3  where;
567  struct nfsv3_sattr how;
568};
569
570struct MKDIR3resok
571{
572  uint32_t           handle_follows;               /* True, handle follows */
573  struct file_handle fhandle;                      /* Variable length */
574  uint32_t           attributes_follows;           /* True, attributes follows */
575  struct nfs_fattr   attributes;                   /* Directory attributes */
576  struct wcc_data    dir_wcc;
577};
578
579struct RMDIR3args
580{
581  struct diropargs3  object;
582};
583
584struct RMDIR3resok
585{
586  struct wcc_data    dir_wcc;
587};
588
589/* The actual size of the lookup argument is variable.  This structures is, therefore,
590 * only useful in setting aside maximum memory usage for the LOOKUP arguments.
591 */
592
593struct READDIR3args
594{
595  struct file_handle dir;                           /* Variable length */
596  nfsuint64          cookie;
597  uint8_t            cookieverf[NFSX_V3COOKIEVERF];
598  uint32_t           count;
599};
600
601/* The READDIR reply is variable length and consists of multiple entries, each
602 * of form:
603 *
604 *  EOF - OR -
605 *
606 *  File ID (8 bytes)
607 *  Name length (4 bytes)
608 *  Name string (varaiable size but in multiples of 4 bytes)
609 *  Cookie (8 bytes)
610 *  next entry (4 bytes)
611 */
612
613struct READDIR3resok
614{
615  uint32_t           attributes_follow;
616  struct nfs_fattr   dir_attributes;
617  uint8_t            cookieverf[NFSX_V3COOKIEVERF];
618  uint32_t           value_follows;
619  uint32_t           reply[1];                      /* Variable length reply begins here */
620};
621
622#define SIZEOF_READDIR3resok(n) \
623   (sizeof(uint32_t) + sizeof(struct nfs_fattr) + \
624    NFSX_V3COOKIEVERF + sizeof(uint32_t) + (n))
625
626struct FS3args
627{
628  struct file_handle fsroot;
629};
630
631#ifdef __cplusplus
632#if __cplusplus
633}
634#endif /* __cplusplus */
635#endif /* __cplusplus */
636
637#endif /* __FS_NFS_NFS_PROTO_H */
638
639