dfs_nfs.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. */
  9. #include <stdio.h>
  10. #include <rtthread.h>
  11. #include <dfs_fs.h>
  12. #include <dfs.h>
  13. #include <dfs_file.h>
  14. #include <rpc/rpc.h>
  15. #include "mount.h"
  16. #include "nfs.h"
  17. #define NAME_MAX 64
  18. #define DFS_NFS_MAX_MTU 1024
  19. #ifdef _WIN32
  20. #define strtok_r strtok_s
  21. #endif
  22. struct nfs_file
  23. {
  24. nfs_fh3 handle; /* handle */
  25. size_t offset; /* current offset */
  26. size_t size; /* total size */
  27. bool_t eof; /* end of file */
  28. };
  29. struct nfs_dir
  30. {
  31. nfs_fh3 handle;
  32. cookie3 cookie;
  33. cookieverf3 cookieverf;
  34. entry3 *entry;
  35. bool_t eof;
  36. READDIR3res res;
  37. };
  38. #define HOST_LENGTH 32
  39. #define EXPORT_PATH_LENGTH 32
  40. struct nfs_filesystem
  41. {
  42. nfs_fh3 root_handle;
  43. nfs_fh3 current_handle;
  44. CLIENT *nfs_client;
  45. CLIENT *mount_client;
  46. char host[HOST_LENGTH];
  47. char export[EXPORT_PATH_LENGTH];
  48. void *data; /* nfs_file or nfs_dir */
  49. };
  50. typedef struct nfs_filesystem nfs_filesystem;
  51. typedef struct nfs_file nfs_file;
  52. typedef struct nfs_dir nfs_dir;
  53. nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path);
  54. static int nfs_parse_host_export(const char *host_export,
  55. char *host,
  56. size_t host_len,
  57. char *export,
  58. size_t export_len)
  59. {
  60. int index;
  61. for (index = 0; index < host_len; index ++)
  62. {
  63. /* it's end of string, failed */
  64. if (host_export[index] == 0)
  65. return -1;
  66. /* copy to host buffer */
  67. if (host_export[index] != ':')
  68. host[index] = host_export[index];
  69. else
  70. break;
  71. }
  72. /* host buffer is not enough, failed */
  73. if (index == host_len)
  74. return -1;
  75. /* make NULL */
  76. host_len = index;
  77. host[host_len] = '\0';
  78. host_len ++;
  79. /* copy export path */
  80. for (index = host_len; index < host_len + export_len; index ++)
  81. {
  82. if (host_export[index] == 0)
  83. {
  84. export[index - host_len] = '\0';
  85. return 0;
  86. }
  87. export[index - host_len] = host_export[index];
  88. }
  89. return -1;
  90. }
  91. static void copy_handle(nfs_fh3 *dest, const nfs_fh3 *source)
  92. {
  93. dest->data.data_len = source->data.data_len;
  94. dest->data.data_val = rt_malloc(dest->data.data_len);
  95. if (dest->data.data_val == NULL)
  96. {
  97. dest->data.data_len = 0;
  98. return;
  99. }
  100. memcpy(dest->data.data_val, source->data.data_val, dest->data.data_len);
  101. }
  102. static nfs_fh3 *get_handle(nfs_filesystem *nfs, const char *name)
  103. {
  104. nfs_fh3 *handle = NULL;
  105. char *file;
  106. char *path;
  107. char *init;
  108. init = path = rt_malloc(strlen(name) + 1);
  109. if (init == NULL)
  110. return NULL;
  111. memcpy(init, name, strlen(name) + 1);
  112. handle = rt_malloc(sizeof(nfs_fh3));
  113. if (handle == NULL)
  114. {
  115. rt_free(init);
  116. return NULL;
  117. }
  118. if (path[0] == '/')
  119. {
  120. path ++;
  121. copy_handle(handle, &nfs->root_handle);
  122. }
  123. else
  124. {
  125. copy_handle(handle, &nfs->current_handle);
  126. }
  127. while ((file = strtok_r(NULL, "/", &path)) != NULL)
  128. {
  129. LOOKUP3args args;
  130. LOOKUP3res res;
  131. memset(&res, 0, sizeof(res));
  132. copy_handle(&args.what.dir, handle);
  133. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  134. args.what.name = file;
  135. if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  136. {
  137. rt_kprintf("Lookup failed\n");
  138. rt_free(init);
  139. rt_free(handle);
  140. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  141. return NULL;
  142. }
  143. else if (res.status != NFS3_OK)
  144. {
  145. rt_kprintf("Lookup failed: %d\n", res.status);
  146. rt_free(init);
  147. rt_free(handle);
  148. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  149. xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
  150. return NULL;
  151. }
  152. copy_handle(handle, &res.LOOKUP3res_u.resok.object);
  153. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  154. xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
  155. }
  156. rt_free(init);
  157. return handle;
  158. }
  159. static nfs_fh3 *get_dir_handle(nfs_filesystem *nfs, const char *name)
  160. {
  161. nfs_fh3 *handle = NULL;
  162. char *file;
  163. char *path;
  164. char *init;
  165. init = path = rt_malloc(strlen(name) + 1);
  166. if (init == NULL)
  167. return NULL;
  168. memcpy(init, name, strlen(name) + 1);
  169. handle = rt_malloc(sizeof(nfs_fh3));
  170. if (handle == NULL)
  171. {
  172. rt_free(init);
  173. return NULL;
  174. }
  175. if (path[0] == '/')
  176. {
  177. path ++;
  178. copy_handle(handle, &nfs->root_handle);
  179. }
  180. else
  181. {
  182. copy_handle(handle, &nfs->current_handle);
  183. }
  184. while ((file = strtok_r(NULL, "/", &path)) != NULL && path && path[0] != 0)
  185. {
  186. LOOKUP3args args;
  187. LOOKUP3res res;
  188. memset(&res, 0, sizeof(res));
  189. copy_handle(&args.what.dir, handle);
  190. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  191. args.what.name = file;
  192. if (nfsproc3_lookup_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  193. {
  194. rt_kprintf("Lookup failed\n");
  195. rt_free(init);
  196. rt_free(handle);
  197. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  198. return NULL;
  199. }
  200. else if (res.status != NFS3_OK)
  201. {
  202. rt_kprintf("Lookup failed: %d\n", res.status);
  203. rt_free(init);
  204. rt_free(handle);
  205. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  206. xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
  207. return NULL;
  208. }
  209. copy_handle(handle, &res.LOOKUP3res_u.resok.object);
  210. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&args.what.dir);
  211. xdr_free((xdrproc_t)xdr_LOOKUP3res, (char *)&res);
  212. }
  213. rt_free(init);
  214. return handle;
  215. }
  216. static size_t nfs_get_filesize(nfs_filesystem *nfs, nfs_fh3 *handle)
  217. {
  218. GETATTR3args args;
  219. GETATTR3res res;
  220. fattr3 *info;
  221. size_t size;
  222. args.object = *handle;
  223. memset(&res, '\0', sizeof(res));
  224. if ((nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS) ||
  225. res.status != NFS3_OK)
  226. {
  227. rt_kprintf("GetAttr failed: %d\n", res.status);
  228. return 0;
  229. }
  230. info = &res.GETATTR3res_u.resok.obj_attributes;
  231. size = info->size;
  232. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  233. return size;
  234. }
  235. rt_bool_t nfs_is_directory(nfs_filesystem *nfs, const char *name)
  236. {
  237. GETATTR3args args;
  238. GETATTR3res res;
  239. fattr3 *info;
  240. nfs_fh3 *handle;
  241. rt_bool_t result;
  242. result = RT_FALSE;
  243. handle = get_handle(nfs, name);
  244. if (handle == NULL)
  245. return RT_FALSE;
  246. args.object = *handle;
  247. memset(&res, '\0', sizeof(res));
  248. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  249. {
  250. rt_kprintf("GetAttr failed\n");
  251. return RT_FALSE;
  252. }
  253. else if (res.status != NFS3_OK)
  254. {
  255. rt_kprintf("Getattr failed: %d\n", res.status);
  256. return RT_FALSE;
  257. }
  258. info = &res.GETATTR3res_u.resok.obj_attributes;
  259. if (info->type == NFS3DIR)
  260. result = RT_TRUE;
  261. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  262. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  263. rt_free(handle);
  264. return result;
  265. }
  266. int nfs_create(nfs_filesystem *nfs, const char *name, mode_t mode)
  267. {
  268. CREATE3args args;
  269. CREATE3res res;
  270. int ret = 0;
  271. nfs_fh3 *handle;
  272. if (nfs->nfs_client == NULL)
  273. {
  274. return -1;
  275. }
  276. handle = get_dir_handle(nfs, name);
  277. if (handle == NULL)
  278. {
  279. return -1;
  280. }
  281. args.where.dir = *handle;
  282. args.where.name = strrchr(name, '/') + 1;
  283. if (args.where.name == NULL)
  284. {
  285. args.where.name = (char *)name;
  286. }
  287. args.how.mode = GUARDED;
  288. args.how.createhow3_u.obj_attributes.mode.set_it = TRUE;
  289. args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode = mode;
  290. args.how.createhow3_u.obj_attributes.uid.set_it = FALSE;
  291. args.how.createhow3_u.obj_attributes.gid.set_it = FALSE;
  292. args.how.createhow3_u.obj_attributes.size.set_it = FALSE;
  293. args.how.createhow3_u.obj_attributes.atime.set_it = DONT_CHANGE;
  294. args.how.createhow3_u.obj_attributes.mtime.set_it = DONT_CHANGE;
  295. memset(&res, 0, sizeof(res));
  296. if (nfsproc3_create_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  297. {
  298. rt_kprintf("Create failed\n");
  299. ret = -1;
  300. }
  301. else if (res.status != NFS3_OK)
  302. {
  303. rt_kprintf("Create failed: %d\n", res.status);
  304. ret = -1;
  305. }
  306. xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res);
  307. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  308. rt_free(handle);
  309. return ret;
  310. }
  311. int nfs_mkdir(nfs_filesystem *nfs, const char *name, mode_t mode)
  312. {
  313. MKDIR3args args;
  314. MKDIR3res res;
  315. int ret = 0;
  316. nfs_fh3 *handle;
  317. if (nfs->nfs_client == NULL)
  318. return -1;
  319. handle = get_dir_handle(nfs, name);
  320. if (handle == NULL)
  321. return -1;
  322. args.where.dir = *handle;
  323. args.where.name = strrchr(name, '/') + 1;
  324. if (args.where.name == NULL)
  325. {
  326. args.where.name = (char *)name;
  327. }
  328. args.attributes.mode.set_it = TRUE;
  329. args.attributes.mode.set_mode3_u.mode = mode;
  330. args.attributes.uid.set_it = FALSE;
  331. args.attributes.gid.set_it = FALSE;
  332. args.attributes.size.set_it = FALSE;
  333. args.attributes.atime.set_it = DONT_CHANGE;
  334. args.attributes.mtime.set_it = DONT_CHANGE;
  335. memset(&res, 0, sizeof(res));
  336. if (nfsproc3_mkdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  337. {
  338. rt_kprintf("Mkdir failed\n");
  339. ret = -1;
  340. }
  341. else if (res.status != NFS3_OK)
  342. {
  343. rt_kprintf("Mkdir failed: %d\n", res.status);
  344. ret = -1;
  345. }
  346. xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res);
  347. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  348. rt_free(handle);
  349. return ret;
  350. }
  351. /* mount(NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */
  352. int nfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
  353. {
  354. mountres3 res;
  355. nfs_filesystem *nfs;
  356. nfs = (nfs_filesystem *)rt_malloc(sizeof(nfs_filesystem));
  357. memset(nfs, 0, sizeof(nfs_filesystem));
  358. if (nfs_parse_host_export((const char *)data, nfs->host, HOST_LENGTH,
  359. nfs->export, EXPORT_PATH_LENGTH) < 0)
  360. {
  361. rt_kprintf("host or export path error\n");
  362. goto __return;
  363. }
  364. nfs->mount_client = clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp");
  365. if (nfs->mount_client == NULL)
  366. {
  367. rt_kprintf("create mount client failed\n");
  368. goto __return;
  369. }
  370. memset(&res, '\0', sizeof(mountres3));
  371. if (mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client) != RPC_SUCCESS)
  372. {
  373. rt_kprintf("nfs mount failed\n");
  374. goto __return;
  375. }
  376. else if (res.fhs_status != MNT3_OK)
  377. {
  378. rt_kprintf("nfs mount failed\n");
  379. goto __return;
  380. }
  381. nfs->nfs_client = clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp");
  382. if (nfs->nfs_client == NULL)
  383. {
  384. rt_kprintf("creat nfs client failed\n");
  385. goto __return;
  386. }
  387. copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle);
  388. copy_handle(&nfs->current_handle, &nfs->root_handle);
  389. nfs->nfs_client->cl_auth = authnone_create();
  390. fs->data = nfs;
  391. return 0;
  392. __return:
  393. if (nfs != NULL)
  394. {
  395. if (nfs->mount_client != NULL)
  396. {
  397. clnt_destroy(nfs->mount_client);
  398. }
  399. if (nfs->nfs_client != NULL)
  400. {
  401. if (nfs->nfs_client->cl_auth != NULL)
  402. {
  403. auth_destroy(nfs->nfs_client->cl_auth);
  404. }
  405. clnt_destroy(nfs->nfs_client);
  406. }
  407. rt_free(nfs);
  408. }
  409. return -1;
  410. }
  411. int nfs_unmount(struct dfs_filesystem *fs)
  412. {
  413. nfs_filesystem *nfs;
  414. RT_ASSERT(fs != NULL);
  415. RT_ASSERT(fs->data != NULL);
  416. nfs = (nfs_filesystem *)fs->data;
  417. if (nfs->mount_client != NULL &&
  418. mountproc3_umnt_3((char *)nfs->export, NULL, nfs->mount_client) != RPC_SUCCESS)
  419. {
  420. rt_kprintf("unmount failed\n");
  421. return -1;
  422. }
  423. /* destroy nfs client */
  424. if (nfs->nfs_client != NULL)
  425. {
  426. if (nfs->nfs_client->cl_auth != NULL)
  427. {
  428. auth_destroy(nfs->nfs_client->cl_auth);
  429. nfs->nfs_client->cl_auth = NULL;
  430. }
  431. clnt_destroy(nfs->nfs_client);
  432. nfs->nfs_client = NULL;
  433. }
  434. /* destroy mount client */
  435. if (nfs->mount_client != NULL)
  436. {
  437. if (nfs->mount_client->cl_auth != NULL)
  438. {
  439. auth_destroy(nfs->mount_client->cl_auth);
  440. nfs->mount_client->cl_auth = NULL;
  441. }
  442. clnt_destroy(nfs->mount_client);
  443. nfs->mount_client = NULL;
  444. }
  445. rt_free(nfs);
  446. fs->data = NULL;
  447. return 0;
  448. }
  449. int nfs_ioctl(struct dfs_file *file, int cmd, void *args)
  450. {
  451. return -ENOSYS;
  452. }
  453. int nfs_read(struct dfs_file *file, void *buf, size_t count)
  454. {
  455. READ3args args;
  456. READ3res res;
  457. ssize_t bytes, total = 0;
  458. nfs_file *fd;
  459. nfs_filesystem *nfs;
  460. if (file->vnode->type == FT_DIRECTORY)
  461. return -EISDIR;
  462. RT_ASSERT(file->vnode->fs != NULL);
  463. struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->fs));
  464. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  465. fd = (nfs_file *)(nfs->data);
  466. RT_ASSERT(fd != NULL);
  467. if (nfs->nfs_client == NULL)
  468. return -1;
  469. /* end of file */
  470. if (fd->eof == TRUE)
  471. return 0;
  472. args.file = fd->handle;
  473. do
  474. {
  475. args.offset = fd->offset;
  476. args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
  477. count -= args.count;
  478. memset(&res, 0, sizeof(res));
  479. if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  480. {
  481. rt_kprintf("Read failed\n");
  482. total = 0;
  483. break;
  484. }
  485. else if (res.status != NFS3_OK)
  486. {
  487. rt_kprintf("Read failed: %d\n", res.status);
  488. total = 0;
  489. break;
  490. }
  491. else
  492. {
  493. bytes = res.READ3res_u.resok.count;
  494. total += bytes;
  495. fd->offset += bytes;
  496. /* update current position */
  497. file->pos = fd->offset;
  498. memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
  499. buf = (void *)((char *)buf + args.count);
  500. if (res.READ3res_u.resok.eof)
  501. {
  502. /* something should probably be here */
  503. fd->eof = TRUE;
  504. break;
  505. }
  506. }
  507. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  508. }
  509. while (count > 0);
  510. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  511. return total;
  512. }
  513. int nfs_write(struct dfs_file *file, const void *buf, size_t count)
  514. {
  515. WRITE3args args;
  516. WRITE3res res;
  517. ssize_t bytes, total = 0;
  518. nfs_file *fd;
  519. nfs_filesystem *nfs;
  520. if (file->vnode->type == FT_DIRECTORY)
  521. return -EISDIR;
  522. RT_ASSERT(file->vnode->fs != NULL);
  523. struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->fs));
  524. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  525. fd = (nfs_file *)(nfs->data);
  526. RT_ASSERT(fd != NULL);
  527. if (nfs->nfs_client == NULL)
  528. return -1;
  529. args.file = fd->handle;
  530. args.stable = FILE_SYNC;
  531. do
  532. {
  533. args.offset = fd->offset;
  534. memset(&res, 0, sizeof(res));
  535. args.data.data_val = (void *)buf;
  536. args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
  537. args.data.data_len = args.count;
  538. count -= args.count;
  539. buf = (const void *)((char *)buf + args.count);
  540. if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  541. {
  542. rt_kprintf("Write failed\n");
  543. total = 0;
  544. break;
  545. }
  546. else if (res.status != NFS3_OK)
  547. {
  548. rt_kprintf("Write failed: %d\n", res.status);
  549. total = 0;
  550. break;
  551. }
  552. else
  553. {
  554. bytes = res.WRITE3res_u.resok.count;
  555. fd->offset += bytes;
  556. total += bytes;
  557. /* update current position */
  558. file->pos = fd->offset;
  559. /* update file size */
  560. if (fd->size < fd->offset) fd->size = fd->offset;
  561. file->vnode->size = fd->size;
  562. }
  563. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  564. } while (count > 0);
  565. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  566. return total;
  567. }
  568. int nfs_lseek(struct dfs_file *file, off_t offset)
  569. {
  570. nfs_file *fd;
  571. nfs_filesystem *nfs;
  572. if (file->vnode->type == FT_DIRECTORY)
  573. return -EISDIR;
  574. RT_ASSERT(file->vnode->fs != NULL);
  575. struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->fs));
  576. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  577. fd = (nfs_file *)(nfs->data);
  578. RT_ASSERT(fd != NULL);
  579. if (offset <= fd->size)
  580. {
  581. fd->offset = offset;
  582. return offset;
  583. }
  584. return -EIO;
  585. }
  586. int nfs_close(struct dfs_file *file)
  587. {
  588. nfs_filesystem *nfs;
  589. RT_ASSERT(file->vnode->fs != NULL);
  590. struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->fs));
  591. RT_ASSERT(file->vnode->ref_count > 0);
  592. if (file->vnode->ref_count > 1)
  593. {
  594. return 0;
  595. }
  596. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  597. if (file->vnode->type == FT_DIRECTORY)
  598. {
  599. struct nfs_dir *dir;
  600. dir = (struct nfs_dir *)nfs->data;
  601. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
  602. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  603. rt_free(dir);
  604. }
  605. else if (file->vnode->type == FT_REGULAR)
  606. {
  607. struct nfs_file *fd;
  608. fd = (struct nfs_file *)nfs->data;
  609. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
  610. rt_free(fd);
  611. }
  612. nfs->data = NULL;
  613. return 0;
  614. }
  615. int nfs_open(struct dfs_file *file)
  616. {
  617. nfs_filesystem *nfs;
  618. RT_ASSERT(file->vnode->fs != NULL);
  619. struct dfs_filesystem *dfs_nfs = file->vnode->fs;
  620. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  621. RT_ASSERT(nfs != NULL);
  622. RT_ASSERT(file->vnode->ref_count > 0);
  623. if (file->vnode->ref_count > 1)
  624. {
  625. if (file->vnode->type == FT_DIRECTORY
  626. && !(file->flags & O_DIRECTORY))
  627. {
  628. return -ENOENT;
  629. }
  630. file->pos = 0;
  631. return 0;
  632. }
  633. if (file->flags & O_DIRECTORY)
  634. {
  635. nfs_dir *dir;
  636. if (file->flags & O_CREAT)
  637. {
  638. if (nfs_mkdir(nfs, file->vnode->path, 0755) < 0)
  639. {
  640. return -EAGAIN;
  641. }
  642. }
  643. /* open directory */
  644. dir = nfs_opendir(nfs, file->vnode->path);
  645. if (dir == NULL)
  646. {
  647. return -ENOENT;
  648. }
  649. file->vnode->type = FT_DIRECTORY;
  650. nfs->data = dir;
  651. }
  652. else
  653. {
  654. nfs_file *fp;
  655. nfs_fh3 *handle;
  656. /* create file */
  657. if (file->flags & O_CREAT)
  658. {
  659. if (nfs_create(nfs, file->vnode->path, 0664) < 0)
  660. {
  661. return -EAGAIN;
  662. }
  663. }
  664. /* open file (get file handle ) */
  665. fp = rt_malloc(sizeof(nfs_file));
  666. if (fp == NULL)
  667. return -ENOMEM;
  668. handle = get_handle(nfs, file->vnode->path);
  669. if (handle == NULL)
  670. {
  671. rt_free(fp);
  672. return -ENOENT;
  673. }
  674. /* get size of file */
  675. fp->size = nfs_get_filesize(nfs, handle);
  676. fp->offset = 0;
  677. fp->eof = FALSE;
  678. copy_handle(&fp->handle, handle);
  679. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  680. rt_free(handle);
  681. if (file->flags & O_APPEND)
  682. {
  683. fp->offset = fp->size;
  684. }
  685. /* set private file */
  686. nfs->data = fp;
  687. file->vnode->size = fp->size;
  688. file->vnode->type = FT_REGULAR;
  689. }
  690. return 0;
  691. }
  692. int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  693. {
  694. GETATTR3args args;
  695. GETATTR3res res;
  696. fattr3 *info;
  697. nfs_fh3 *handle;
  698. nfs_filesystem *nfs;
  699. RT_ASSERT(fs != NULL);
  700. RT_ASSERT(fs->data != NULL);
  701. nfs = (nfs_filesystem *)fs->data;
  702. handle = get_handle(nfs, path);
  703. if (handle == NULL)
  704. return -1;
  705. args.object = *handle;
  706. memset(&res, '\0', sizeof(res));
  707. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  708. {
  709. rt_kprintf("GetAttr failed\n");
  710. return -1;
  711. }
  712. else if (res.status != NFS3_OK)
  713. {
  714. rt_kprintf("Getattr failed: %d\n", res.status);
  715. return -1;
  716. }
  717. info = &res.GETATTR3res_u.resok.obj_attributes;
  718. st->st_dev = 0;
  719. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
  720. if (info->type == NFS3DIR)
  721. {
  722. st->st_mode &= ~S_IFREG;
  723. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  724. }
  725. st->st_size = info->size;
  726. st->st_mtime = info->mtime.seconds;
  727. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  728. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  729. rt_free(handle);
  730. return 0;
  731. }
  732. nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path)
  733. {
  734. nfs_dir *dir;
  735. nfs_fh3 *handle;
  736. dir = rt_malloc(sizeof(nfs_dir));
  737. if (dir == NULL)
  738. {
  739. return NULL;
  740. }
  741. handle = get_handle(nfs, path);
  742. if (handle == NULL)
  743. {
  744. rt_free(dir);
  745. return NULL;
  746. }
  747. copy_handle(&dir->handle, handle);
  748. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  749. rt_free(handle);
  750. dir->cookie = 0;
  751. memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
  752. dir->entry = NULL;
  753. dir->eof = FALSE;
  754. memset(&dir->res, '\0', sizeof(dir->res));
  755. return dir;
  756. }
  757. char *nfs_readdir(nfs_filesystem *nfs, nfs_dir *dir)
  758. {
  759. static char name[NAME_MAX];
  760. if (nfs->nfs_client == NULL || dir == NULL)
  761. return NULL;
  762. if (dir->entry == NULL)
  763. {
  764. READDIR3args args;
  765. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  766. memset(&dir->res, '\0', sizeof(dir->res));
  767. args.dir = dir->handle;
  768. args.cookie = dir->cookie;
  769. memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
  770. args.count = 1024;
  771. if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
  772. {
  773. rt_kprintf("Readdir failed\n");
  774. return NULL;
  775. }
  776. else if (dir->res.status != NFS3_OK)
  777. {
  778. rt_kprintf("Readdir failed: %d\n", dir->res.status);
  779. return NULL;
  780. }
  781. memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
  782. dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
  783. dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
  784. }
  785. if (dir->eof == TRUE && dir->entry == NULL)
  786. return NULL;
  787. dir->cookie = dir->entry->cookie;
  788. strncpy(name, dir->entry->name, NAME_MAX - 1);
  789. dir->entry = dir->entry->nextentry;
  790. name[NAME_MAX - 1] = '\0';
  791. return name;
  792. }
  793. int nfs_unlink(struct dfs_filesystem *fs, const char *path)
  794. {
  795. int ret = 0;
  796. nfs_filesystem *nfs;
  797. RT_ASSERT(fs != NULL);
  798. RT_ASSERT(fs->data != NULL);
  799. nfs = (nfs_filesystem *)fs->data;
  800. if (nfs_is_directory(nfs, path) == RT_FALSE)
  801. {
  802. /* remove file */
  803. REMOVE3args args;
  804. REMOVE3res res;
  805. nfs_fh3 *handle;
  806. handle = get_dir_handle(nfs, path);
  807. if (handle == NULL)
  808. return -1;
  809. args.object.dir = *handle;
  810. args.object.name = strrchr(path, '/') + 1;
  811. if (args.object.name == NULL)
  812. {
  813. args.object.name = (char *)path;
  814. }
  815. memset(&res, 0, sizeof(res));
  816. if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  817. {
  818. rt_kprintf("Remove failed\n");
  819. ret = -1;
  820. }
  821. else if (res.status != NFS3_OK)
  822. {
  823. rt_kprintf("Remove failed: %d\n", res.status);
  824. ret = -1;
  825. }
  826. xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
  827. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  828. rt_free(handle);
  829. }
  830. else
  831. {
  832. /* remove directory */
  833. RMDIR3args args;
  834. RMDIR3res res;
  835. nfs_fh3 *handle;
  836. handle = get_dir_handle(nfs, path);
  837. if (handle == NULL)
  838. return -1;
  839. args.object.dir = *handle;
  840. args.object.name = strrchr(path, '/') + 1;
  841. if (args.object.name == NULL)
  842. {
  843. args.object.name = (char *)path;
  844. }
  845. memset(&res, 0, sizeof(res));
  846. if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  847. {
  848. rt_kprintf("Rmdir failed\n");
  849. ret = -1;
  850. }
  851. else if (res.status != NFS3_OK)
  852. {
  853. rt_kprintf("Rmdir failed: %d\n", res.status);
  854. ret = -1;
  855. }
  856. xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
  857. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  858. rt_free(handle);
  859. }
  860. return ret;
  861. }
  862. int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
  863. {
  864. RENAME3args args;
  865. RENAME3res res;
  866. nfs_fh3 *sHandle;
  867. nfs_fh3 *dHandle;
  868. int ret = 0;
  869. nfs_filesystem *nfs;
  870. RT_ASSERT(fs != NULL);
  871. RT_ASSERT(fs->data != NULL);
  872. nfs = (nfs_filesystem *)fs->data;
  873. if (nfs->nfs_client == NULL)
  874. return -1;
  875. sHandle = get_dir_handle(nfs, src);
  876. if (sHandle == NULL)
  877. return -1;
  878. dHandle = get_dir_handle(nfs, dest);
  879. if (dHandle == NULL)
  880. return -1;
  881. args.from.dir = *sHandle;
  882. args.from.name = strrchr(src, '/') + 1;
  883. if (args.from.name == NULL)
  884. args.from.name = (char *)src;
  885. args.to.dir = *dHandle;
  886. args.to.name = strrchr(src, '/') + 1;
  887. if (args.to.name == NULL)
  888. args.to.name = (char *)dest;
  889. memset(&res, '\0', sizeof(res));
  890. if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  891. {
  892. rt_kprintf("Rename failed\n");
  893. ret = -1;
  894. }
  895. else if (res.status != NFS3_OK)
  896. {
  897. rt_kprintf("Rename failed: %d\n", res.status);
  898. ret = -1;
  899. }
  900. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
  901. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
  902. xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
  903. return ret;
  904. }
  905. int nfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
  906. {
  907. nfs_dir *dir;
  908. rt_uint32_t index;
  909. struct dirent *d;
  910. nfs_filesystem *nfs;
  911. char *name;
  912. RT_ASSERT(file->vnode->fs != NULL);
  913. struct dfs_filesystem *dfs_nfs = ((struct dfs_filesystem *)(file->vnode->fs));
  914. nfs = (struct nfs_filesystem *)(dfs_nfs->data);
  915. dir = (nfs_dir *)(nfs->data);
  916. RT_ASSERT(dir != NULL);
  917. /* make integer count */
  918. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  919. if (count == 0)
  920. return -EINVAL;
  921. index = 0;
  922. while (1)
  923. {
  924. d = dirp + index;
  925. name = nfs_readdir(nfs, dir);
  926. if (name == NULL)
  927. break;
  928. if (rt_strcmp(name, ".") == 0)
  929. {
  930. continue;
  931. }
  932. else if (rt_strcmp(name, "..") == 0)
  933. {
  934. continue;
  935. }
  936. d->d_type = DT_REG;
  937. d->d_namlen = rt_strlen(name);
  938. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  939. rt_strncpy(d->d_name, name, DFS_PATH_MAX);
  940. index ++;
  941. if (index * sizeof(struct dirent) >= count)
  942. break;
  943. }
  944. return index * sizeof(struct dirent);
  945. }
  946. static const struct dfs_file_ops nfs_fops =
  947. {
  948. nfs_open,
  949. nfs_close,
  950. nfs_ioctl,
  951. nfs_read,
  952. nfs_write,
  953. NULL, /* flush */
  954. nfs_lseek,
  955. nfs_getdents,
  956. NULL, /* poll */
  957. };
  958. static const struct dfs_filesystem_ops _nfs =
  959. {
  960. "nfs",
  961. DFS_FS_FLAG_DEFAULT,
  962. &nfs_fops,
  963. nfs_mount,
  964. nfs_unmount,
  965. NULL, /* mkfs */
  966. NULL, /* statfs */
  967. nfs_unlink,
  968. nfs_stat,
  969. nfs_rename,
  970. };
  971. int nfs_init(void)
  972. {
  973. /* register nfs file system */
  974. dfs_register(&_nfs);
  975. return RT_EOK;
  976. }
  977. INIT_COMPONENT_EXPORT(nfs_init);