]> git.decadent.org.uk Git - nfs-utils.git/blob - support/nfs/getfh.c
Imported Upstream version 1.2.3
[nfs-utils.git] / support / nfs / getfh.c
1 /*
2  * support/nfs/getfh.c
3  *
4  * Get the FH for a given client and directory. This function takes
5  * the NFS protocol version number as an additional argument.
6  *
7  * This function has nothing in common with the SunOS getfh function,
8  * which is a front-end to the RPC mount call.
9  *
10  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #include <config.h>
15 #endif
16
17 #include <string.h>
18 #include <sys/types.h>
19 #include <errno.h>
20 #include "nfslib.h"
21
22 /**
23  * getfh_old - ask the kernel for an NFSv2 file handle via nfsctl()
24  * @sin: pointer to IPv4 address of a client
25  * @dev: device number of device where requested object resides
26  * @ino: inode number of requested object
27  *
28  * Returns a pointer to an NFSv2 file handle, or NULL if some error
29  * occurred.  errno is set to reflect the specifics of the error.
30  */
31 struct nfs_fh_len *
32 getfh_old(const struct sockaddr_in *sin, const dev_t dev, const ino_t ino)
33 {
34         union nfsctl_res        res;
35         struct nfsctl_arg       arg;
36         static struct nfs_fh_len rfh;
37
38         if (sin->sin_family != AF_INET) {
39                 errno = EAFNOSUPPORT;
40                 return NULL;
41         }
42
43         memset(&arg, 0, sizeof(arg));
44         memset(&res, 0, sizeof(res));
45
46         arg.ca_version = NFSCTL_VERSION;
47         arg.ca_getfh.gf_version = 2;    /* obsolete */
48         arg.ca_getfh.gf_dev = dev;
49         arg.ca_getfh.gf_ino = ino;
50         memcpy(&arg.ca_getfh.gf_addr, sin, sizeof(*sin));
51
52         if (nfsctl(NFSCTL_GETFH, &arg, &res) < 0)
53                 return NULL;
54
55         memset(&rfh, 0, sizeof(rfh));
56         rfh.fh_size = 32;
57         memcpy(rfh.fh_handle, &res.cr_getfh, 32);
58         return &rfh;
59 }
60
61 /**
62  * getfh - ask the kernel for an NFSv2 file handle via nfsctl()
63  * @sin: pointer to IPv4 address of a client
64  * @path: pointer to a '\0'-terminated ASCII string containing an pathname
65  *
66  * Returns a pointer to an NFSv2 file handle, or NULL if some error
67  * occurred.  errno is set to reflect the specifics of the error.
68  */
69 struct nfs_fh_len *
70 getfh(const struct sockaddr_in *sin, const char *path)
71 {
72         static union nfsctl_res res;
73         struct nfsctl_arg       arg;
74         static struct nfs_fh_len rfh;
75
76         if (sin->sin_family != AF_INET) {
77                 errno = EAFNOSUPPORT;
78                 return NULL;
79         }
80
81         memset(&arg, 0, sizeof(arg));
82         memset(&res, 0, sizeof(res));
83
84         arg.ca_version = NFSCTL_VERSION;
85         arg.ca_getfd.gd_version = 2;    /* obsolete */
86         strncpy(arg.ca_getfd.gd_path, path,
87                 sizeof(arg.ca_getfd.gd_path) - 1);
88         arg.ca_getfd.gd_path[sizeof (arg.ca_getfd.gd_path) - 1] = '\0';
89         memcpy(&arg.ca_getfd.gd_addr, sin, sizeof(*sin));
90
91         if (nfsctl(NFSCTL_GETFD, &arg, &res) < 0)
92                 return NULL;
93
94         memset(&rfh, 0, sizeof(rfh));
95         rfh.fh_size = 32;
96         memcpy(rfh.fh_handle, &res.cr_getfh, 32);
97         return &rfh;
98 }
99
100 /**
101  * getfh_size - ask the kernel for a file handle via nfsctl()
102  * @sin: pointer to IPv4 address of a client
103  * @path: pointer to a '\0'-terminated ASCII string containing an pathname
104  * @size: maximum size, in bytes, of the returned file handle
105  *
106  * Returns a pointer to an NFSv3 file handle, or NULL if some error
107  * occurred.  errno is set to reflect the specifics of the error.
108  */
109 struct nfs_fh_len *
110 getfh_size(const struct sockaddr_in *sin, const char *path, const int size)
111 {
112         static union nfsctl_res res;
113         struct nfsctl_arg       arg;
114
115         if (sin->sin_family != AF_INET) {
116                 errno = EAFNOSUPPORT;
117                 return NULL;
118         }
119
120         memset(&arg, 0, sizeof(arg));
121         memset(&res, 0, sizeof(res));
122
123         arg.ca_version = NFSCTL_VERSION;
124         strncpy(arg.ca_getfs.gd_path, path,
125                 sizeof(arg.ca_getfs.gd_path) - 1);
126         arg.ca_getfs.gd_path[sizeof (arg.ca_getfs.gd_path) - 1] = '\0';
127         memcpy(&arg.ca_getfs.gd_addr, sin, sizeof(*sin));
128         arg.ca_getfs.gd_maxlen = size;
129
130         if (nfsctl(NFSCTL_GETFS, &arg, &res) < 0)
131                 return NULL;
132
133         return &res.cr_getfs;
134 }