dfs_nfs.c 28 KB

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