nfs_util.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /****************************************************************************
  2. * fs/nfs/nfs_util.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <stdint.h>
  27. #include <stdbool.h>
  28. #include <stdlib.h>
  29. #include <time.h>
  30. #include <string.h>
  31. #include <assert.h>
  32. #include <errno.h>
  33. #include <debug.h>
  34. #include "rpc.h"
  35. #include "nfs.h"
  36. #include "nfs_proto.h"
  37. #include "nfs_mount.h"
  38. #include "nfs_node.h"
  39. #include "xdr_subs.h"
  40. /****************************************************************************
  41. * Private Functions
  42. ****************************************************************************/
  43. static inline int nfs_pathsegment(FAR const char **path, FAR char *buffer,
  44. FAR char *terminator)
  45. {
  46. FAR const char *src = *path;
  47. FAR char *dest = buffer;
  48. int nbytes = 0;
  49. char ch;
  50. /* Loop until the name is successfully parsed or an error occurs */
  51. for (; ; )
  52. {
  53. /* Get the next byte from the path */
  54. ch = *src++;
  55. /* Check if this the last byte in this segment name */
  56. if (ch == '\0' || ch == '/')
  57. {
  58. /* This logic just supports "//" sequences in the path name */
  59. if (ch == '\0' || nbytes > 0)
  60. {
  61. /* NULL terminate the parsed path segment */
  62. *dest = '\0';
  63. /* Return next path and the terminating character */
  64. *terminator = ch;
  65. *path = src;
  66. return OK;
  67. }
  68. /* Just skip over any leading '/' characters */
  69. }
  70. else if (nbytes >= NAME_MAX)
  71. {
  72. ferr("ERROR: File name segment is too long: %d\n", *path);
  73. return -EFBIG;
  74. }
  75. else
  76. {
  77. /* Save next character in the accumulated name */
  78. *dest++ = ch;
  79. nbytes++;
  80. }
  81. }
  82. }
  83. /****************************************************************************
  84. * Public Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: nfs_request
  88. *
  89. * Description:
  90. * Perform the NFS request. On successful receipt, it verifies the NFS
  91. * level of the returned values.
  92. *
  93. * Returned Value:
  94. * Zero on success; a negative errno value on failure.
  95. *
  96. ****************************************************************************/
  97. int nfs_request(FAR struct nfsmount *nmp, int procnum,
  98. FAR void *request, size_t reqlen,
  99. FAR void *response, size_t resplen)
  100. {
  101. FAR struct rpcclnt *clnt = nmp->nm_rpcclnt;
  102. struct nfs_reply_header replyh;
  103. int error;
  104. error = rpcclnt_request(clnt, procnum, NFS_PROG, NFS_VER3,
  105. request, reqlen, response, resplen);
  106. if (error != 0)
  107. {
  108. ferr("ERROR: rpcclnt_request failed: %d\n", error);
  109. return error;
  110. }
  111. memcpy(&replyh, response, sizeof(struct nfs_reply_header));
  112. if (replyh.nfs_status != 0)
  113. {
  114. /* NFS_ERRORS are the same as NuttX errno values */
  115. return -fxdr_unsigned(uint32_t, replyh.nfs_status);
  116. }
  117. if (replyh.rh.rpc_verfi.authtype != 0)
  118. {
  119. error = -EOPNOTSUPP;
  120. ferr("ERROR: NFS authtype %d from server\n",
  121. fxdr_unsigned(int, replyh.rh.rpc_verfi.authtype));
  122. return error;
  123. }
  124. finfo("NFS_SUCCESS\n");
  125. return OK;
  126. }
  127. /****************************************************************************
  128. * Name: nfs_lookup
  129. *
  130. * Description:
  131. * Given a directory file handle, and the path to file in the directory,
  132. * return the file handle of the path and attributes of both the file and
  133. * the directory containing the file.
  134. *
  135. * NOTE: The LOOKUP call differs from other RPC messages in that the
  136. * call message is variable length, depending upon the size of the path
  137. * name.
  138. *
  139. ****************************************************************************/
  140. int nfs_lookup(FAR struct nfsmount *nmp, FAR const char *filename,
  141. FAR struct file_handle *fhandle,
  142. FAR struct nfs_fattr *obj_attributes,
  143. FAR struct nfs_fattr *dir_attributes)
  144. {
  145. FAR uint32_t *ptr;
  146. uint32_t value;
  147. int reqlen;
  148. int namelen;
  149. int error = 0;
  150. DEBUGASSERT(nmp && filename && fhandle);
  151. /* Get the length of the string to be sent */
  152. namelen = strlen(filename);
  153. if (namelen > NAME_MAX)
  154. {
  155. ferr("ERROR: Length of the string is too long: %d\n", namelen);
  156. return -E2BIG;
  157. }
  158. /* Initialize the request */
  159. ptr = (FAR uint32_t *)&nmp->nm_msgbuffer.lookup.lookup;
  160. reqlen = 0;
  161. /* Copy the variable length, directory file handle */
  162. *ptr++ = txdr_unsigned(fhandle->length);
  163. reqlen += sizeof(uint32_t);
  164. memcpy(ptr, &fhandle->handle, fhandle->length);
  165. reqlen += uint32_alignup(fhandle->length);
  166. ptr += uint32_increment(fhandle->length);
  167. /* Copy the variable-length file name */
  168. *ptr++ = txdr_unsigned(namelen);
  169. reqlen += sizeof(uint32_t);
  170. memcpy(ptr, filename, namelen);
  171. reqlen += uint32_alignup(namelen);
  172. /* Request LOOKUP from the server */
  173. nfs_statistics(NFSPROC_LOOKUP);
  174. error = nfs_request(nmp, NFSPROC_LOOKUP,
  175. (FAR void *)&nmp->nm_msgbuffer.lookup, reqlen,
  176. (FAR void *)nmp->nm_iobuffer, nmp->nm_buflen);
  177. if (error)
  178. {
  179. ferr("ERROR: nfs_request failed: %d\n", error);
  180. return error;
  181. }
  182. /* Return the data to the caller's buffers. NOTE: Here we ignore the
  183. * the exact layout of the rpc_reply_lookup structure. File handles
  184. * may differ in size whereas struct rpc_reply_lookup uses a fixed size.
  185. */
  186. ptr = (FAR uint32_t *)
  187. &((FAR struct rpc_reply_lookup *)nmp->nm_iobuffer)->lookup;
  188. /* Get the length of the file handle */
  189. value = *ptr++;
  190. value = fxdr_unsigned(uint32_t, value);
  191. if (value > NFSX_V3FHMAX)
  192. {
  193. ferr("ERROR: Bad file handle length: %d\n", value);
  194. return -EIO;
  195. }
  196. /* Return the file handle */
  197. fhandle->length = value;
  198. memcpy(&fhandle->handle, ptr, value);
  199. ptr += uint32_increment(value);
  200. /* Check if there are object attributes and, if so, copy them to the user
  201. * buffer
  202. */
  203. value = *ptr++;
  204. if (value)
  205. {
  206. if (obj_attributes)
  207. {
  208. memcpy(obj_attributes, ptr, sizeof(struct nfs_fattr));
  209. }
  210. ptr += uint32_increment(sizeof(struct nfs_fattr));
  211. }
  212. /* Check if there are directory attributes and, if so, copy them to the
  213. * user buffer
  214. */
  215. value = *ptr++;
  216. if (value && dir_attributes)
  217. {
  218. memcpy(dir_attributes, ptr, sizeof(struct nfs_fattr));
  219. }
  220. return OK;
  221. }
  222. /****************************************************************************
  223. * Name: nfs_findnode
  224. *
  225. * Description:
  226. * Given a path to something that may or may not be in the file system,
  227. * return the handle of the directory entry of the requested object.
  228. *
  229. * Returned Value:
  230. * Zero on success; a negative errno value on failure.
  231. *
  232. ****************************************************************************/
  233. int nfs_findnode(FAR struct nfsmount *nmp, FAR const char *relpath,
  234. FAR struct file_handle *fhandle,
  235. FAR struct nfs_fattr *obj_attributes,
  236. FAR struct nfs_fattr *dir_attributes)
  237. {
  238. FAR const char *path = relpath;
  239. char buffer[NAME_MAX + 1];
  240. char terminator;
  241. uint32_t tmp;
  242. int error;
  243. /* Start with the file handle of the root directory. */
  244. fhandle->length = nmp->nm_fhsize;
  245. memcpy(&fhandle->handle, nmp->nm_fh, nmp->nm_fhsize);
  246. /* If no path was provided, then the root directory must be exactly what
  247. * the caller is looking for.
  248. */
  249. if (*path == '\0')
  250. {
  251. /* Return the root directory attributes */
  252. if (obj_attributes)
  253. {
  254. memcpy(obj_attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
  255. }
  256. if (dir_attributes)
  257. {
  258. memcpy(dir_attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
  259. }
  260. return OK;
  261. }
  262. /* This is not the root directory. Loop until the directory entry
  263. * corresponding to the path is found.
  264. */
  265. for (; ; )
  266. {
  267. /* Extract the next path segment name. */
  268. error = nfs_pathsegment(&path, buffer, &terminator);
  269. if (error != OK)
  270. {
  271. /* The filename segment contains is too long. */
  272. ferr("ERROR: nfs_pathsegment of \"%s\" failed after \"%s\": %d\n",
  273. relpath, buffer, error);
  274. return error;
  275. }
  276. /* Look-up this path segment */
  277. error = nfs_lookup(nmp, buffer, fhandle, obj_attributes,
  278. dir_attributes);
  279. if (error != OK)
  280. {
  281. ferr("ERROR: nfs_lookup of \"%s\" failed at \"%s\": %d\n",
  282. relpath, buffer, error);
  283. return error;
  284. }
  285. /* If the terminator character in the path was the end of the string
  286. * then we have successfully found the directory entry that describes
  287. * the path.
  288. */
  289. if (!terminator)
  290. {
  291. /* Return success meaning that the description the matching
  292. * directory entry is in fhandle, obj_attributes, and
  293. * dir_attributes.
  294. */
  295. return OK;
  296. }
  297. /* No.. then we have found one of the intermediate directories on
  298. * the way to the final path target. In this case, make sure
  299. * the thing that we found is, indeed, a directory.
  300. */
  301. tmp = fxdr_unsigned(uint32_t, obj_attributes->fa_type);
  302. if (tmp != NFDIR)
  303. {
  304. /* Ooops.. we found something else */
  305. ferr("ERROR: Intermediate segment \"%s\" of \'%s\" is not a "
  306. "directory\n",
  307. buffer, relpath);
  308. return -ENOTDIR;
  309. }
  310. }
  311. }
  312. /****************************************************************************
  313. * Name: nfs_finddir
  314. *
  315. * Description:
  316. * Given a path to something that may or may not be in the file system,
  317. * return the handle of the entry of the directory containing the requested
  318. * object.
  319. *
  320. * Returned Value:
  321. * Zero on success; a negative errno value on failure.
  322. *
  323. ****************************************************************************/
  324. int nfs_finddir(FAR struct nfsmount *nmp, FAR const char *relpath,
  325. FAR struct file_handle *fhandle,
  326. FAR struct nfs_fattr *attributes, FAR char *filename)
  327. {
  328. FAR const char *path = relpath;
  329. uint32_t tmp;
  330. char terminator;
  331. int error;
  332. /* Verify that a path was provided */
  333. if (*path == '\0')
  334. {
  335. return -ENOENT;
  336. }
  337. /* Start with the file handle of the root directory. */
  338. fhandle->length = nmp->nm_fhsize;
  339. memcpy(&fhandle->handle, nmp->nm_fh, nmp->nm_fhsize);
  340. memcpy(attributes, &nmp->nm_fattr, sizeof(struct nfs_fattr));
  341. /* Loop until the directory entry containing the path is found. */
  342. for (; ; )
  343. {
  344. /* Extract the next path segment name. */
  345. error = nfs_pathsegment(&path, filename, &terminator);
  346. if (error != OK)
  347. {
  348. /* The filename segment contains is too long. */
  349. ferr("ERROR: nfs_pathsegment of \"%s\" failed after \"%s\": %d\n",
  350. relpath, filename, error);
  351. return error;
  352. }
  353. /* If the terminator character in the path was the end of the string
  354. * then we have successfully found the directory that contains the name
  355. * of interest.
  356. */
  357. if (!terminator)
  358. {
  359. /* Return success meaning that the description of the directory
  360. * containing the object is in fhandle and attributes.
  361. */
  362. return OK;
  363. }
  364. /* Look-up the next path segment */
  365. error = nfs_lookup(nmp, filename, fhandle, attributes, NULL);
  366. if (error != OK)
  367. {
  368. ferr("ERROR: fs_lookup of \"%s\" failed at \"%s\": %d\n",
  369. relpath, filename, error);
  370. return error;
  371. }
  372. /* Make sure the thing that we found is, indeed, a directory. */
  373. tmp = fxdr_unsigned(uint32_t, attributes->fa_type);
  374. if (tmp != NFDIR)
  375. {
  376. /* Ooops.. we found something else */
  377. ferr("ERROR: Intermediate segment \"%s\" of \'%s\" is not a "
  378. "directory\n",
  379. filename, relpath);
  380. return -ENOTDIR;
  381. }
  382. }
  383. }
  384. /****************************************************************************
  385. * Name: nfs_attrupdate
  386. *
  387. * Description:
  388. * Update file attributes on write or after the file is modified.
  389. *
  390. * Returned Value:
  391. * None.
  392. *
  393. ****************************************************************************/
  394. void nfs_attrupdate(FAR struct nfsnode *np, FAR struct nfs_fattr *attributes)
  395. {
  396. struct timespec ts;
  397. /* Save a few of the files attribute values in file structure (host
  398. * order).
  399. */
  400. np->n_type = fxdr_unsigned(uint8_t, attributes->fa_type);
  401. np->n_mode = fxdr_unsigned(uint16_t, attributes->fa_mode);
  402. np->n_size = fxdr_hyper(&attributes->fa_size);
  403. fxdr_nfsv3time(&attributes->fa_atime, &ts);
  404. np->n_atime = ts.tv_sec;
  405. fxdr_nfsv3time(&attributes->fa_mtime, &ts);
  406. np->n_mtime = ts.tv_sec;
  407. fxdr_nfsv3time(&attributes->fa_ctime, &ts);
  408. np->n_ctime = ts.tv_sec;
  409. }