dfs_nfs.c 28 KB

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