dfs_nfs.c 22 KB

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