dfs_nfs.c 23 KB

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