dfs_nfs.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  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. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  478. fd = (nfs_file *)(nfs->data);
  479. RT_ASSERT(fd != NULL);
  480. if (nfs->nfs_client == NULL)
  481. return -1;
  482. /* end of file */
  483. if (fd->eof == TRUE)
  484. return 0;
  485. args.file = fd->handle;
  486. do {
  487. args.offset = fd->offset;
  488. args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
  489. count -= args.count;
  490. memset(&res, 0, sizeof(res));
  491. if (nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  492. {
  493. rt_kprintf("Read failed\n");
  494. total = 0;
  495. break;
  496. }
  497. else if (res.status != NFS3_OK)
  498. {
  499. rt_kprintf("Read failed: %d\n", res.status);
  500. total = 0;
  501. break;
  502. }
  503. else
  504. {
  505. bytes = res.READ3res_u.resok.count;
  506. total += bytes;
  507. fd->offset += bytes;
  508. /* update current position */
  509. file->pos = fd->offset;
  510. memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
  511. buf = (void *)((char *)buf + args.count);
  512. if (res.READ3res_u.resok.eof)
  513. {
  514. /* something should probably be here */
  515. fd->eof = TRUE;
  516. break;
  517. }
  518. }
  519. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  520. } while(count > 0);
  521. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  522. return total;
  523. }
  524. int nfs_write(struct dfs_fd *file, const void *buf, size_t count)
  525. {
  526. WRITE3args args;
  527. WRITE3res res;
  528. ssize_t bytes, total=0;
  529. nfs_file *fd;
  530. nfs_filesystem *nfs;
  531. if (file->type == FT_DIRECTORY)
  532. return -EISDIR;
  533. RT_ASSERT(file->data != NULL);
  534. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  535. fd = (nfs_file *)(nfs->data);
  536. RT_ASSERT(fd != NULL);
  537. if (nfs->nfs_client == NULL)
  538. return -1;
  539. args.file = fd->handle;
  540. args.stable = FILE_SYNC;
  541. do {
  542. args.offset = fd->offset;
  543. memset(&res, 0, sizeof(res));
  544. args.data.data_val=(void *)buf;
  545. args.count = count > DFS_NFS_MAX_MTU ? DFS_NFS_MAX_MTU : count;
  546. args.data.data_len = args.count;
  547. count -= args.count;
  548. buf = (const void *)((char *)buf + args.count);
  549. if (nfsproc3_write_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  550. {
  551. rt_kprintf("Write failed\n");
  552. total = 0;
  553. break;
  554. }
  555. else if (res.status != NFS3_OK)
  556. {
  557. rt_kprintf("Write failed: %d\n", res.status);
  558. total = 0;
  559. break;
  560. }
  561. else
  562. {
  563. bytes = res.WRITE3res_u.resok.count;
  564. fd->offset += bytes;
  565. total += bytes;
  566. /* update current position */
  567. file->pos = fd->offset;
  568. /* update file size */
  569. if (fd->size < fd->offset) fd->size = fd->offset;
  570. file->size = fd->size;
  571. }
  572. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  573. } while (count > 0);
  574. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  575. return total;
  576. }
  577. int nfs_lseek(struct dfs_fd *file, off_t offset)
  578. {
  579. nfs_file *fd;
  580. nfs_filesystem *nfs;
  581. if (file->type == FT_DIRECTORY)
  582. return -EISDIR;
  583. RT_ASSERT(file->data != NULL);
  584. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  585. fd = (nfs_file *)(nfs->data);
  586. RT_ASSERT(fd != NULL);
  587. if (offset <= fd->size)
  588. {
  589. fd->offset = offset;
  590. return offset;
  591. }
  592. return -EIO;
  593. }
  594. int nfs_close(struct dfs_fd *file)
  595. {
  596. nfs_filesystem *nfs;
  597. RT_ASSERT(file->data != NULL);
  598. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  599. if (file->type == FT_DIRECTORY)
  600. {
  601. struct nfs_dir *dir;
  602. dir = (struct nfs_dir *)nfs->data;
  603. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
  604. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  605. rt_free(dir);
  606. }
  607. else if (file->type == FT_REGULAR)
  608. {
  609. struct nfs_file *fd;
  610. fd = (struct nfs_file *)nfs->data;
  611. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
  612. rt_free(fd);
  613. }
  614. nfs->data = NULL;
  615. return 0;
  616. }
  617. int nfs_open(struct dfs_fd *file)
  618. {
  619. nfs_filesystem *nfs;
  620. RT_ASSERT(file->data != NULL);
  621. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  622. if (file->flags & O_DIRECTORY)
  623. {
  624. nfs_dir *dir;
  625. if (file->flags & O_CREAT)
  626. {
  627. if (nfs_mkdir(nfs, file->path, 0755) < 0)
  628. return -EAGAIN;
  629. }
  630. /* open directory */
  631. dir = nfs_opendir(nfs, file->path);
  632. if (dir == NULL) return -ENOENT;
  633. nfs->data = dir;
  634. }
  635. else
  636. {
  637. nfs_file *fp;
  638. nfs_fh3 *handle;
  639. /* create file */
  640. if (file->flags & O_CREAT)
  641. {
  642. if (nfs_create(nfs, file->path, 0664) < 0)
  643. return -EAGAIN;
  644. }
  645. /* open file (get file handle ) */
  646. fp = rt_malloc(sizeof(nfs_file));
  647. if (fp == NULL)
  648. return -ENOMEM;
  649. handle = get_handle(nfs, file->path);
  650. if (handle == NULL)
  651. {
  652. rt_free(fp);
  653. return -ENOENT;
  654. }
  655. /* get size of file */
  656. fp->size = nfs_get_filesize(nfs, handle);
  657. fp->offset = 0;
  658. fp->eof = FALSE;
  659. copy_handle(&fp->handle, handle);
  660. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  661. rt_free(handle);
  662. if (file->flags & O_APPEND)
  663. {
  664. fp->offset = fp->size;
  665. }
  666. /* set private file */
  667. nfs->data = fp;
  668. file->size = fp->size;
  669. }
  670. return 0;
  671. }
  672. int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  673. {
  674. GETATTR3args args;
  675. GETATTR3res res;
  676. fattr3 *info;
  677. nfs_fh3 *handle;
  678. nfs_filesystem *nfs;
  679. RT_ASSERT(fs != NULL);
  680. RT_ASSERT(fs->data != NULL);
  681. nfs = (nfs_filesystem *)fs->data;
  682. handle = get_handle(nfs, path);
  683. if (handle == NULL)
  684. return -1;
  685. args.object = *handle;
  686. memset(&res, '\0', sizeof(res));
  687. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  688. {
  689. rt_kprintf("GetAttr failed\n");
  690. return -1;
  691. }
  692. else if (res.status != NFS3_OK)
  693. {
  694. rt_kprintf("Getattr failed: %d\n", res.status);
  695. return -1;
  696. }
  697. info = &res.GETATTR3res_u.resok.obj_attributes;
  698. st->st_dev = 0;
  699. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH;
  700. if (info->type == NFS3DIR)
  701. {
  702. st->st_mode &= ~S_IFREG;
  703. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  704. }
  705. st->st_size = info->size;
  706. st->st_mtime = info->mtime.seconds;
  707. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  708. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  709. rt_free(handle);
  710. return 0;
  711. }
  712. nfs_dir *nfs_opendir(nfs_filesystem *nfs, const char *path)
  713. {
  714. nfs_dir *dir;
  715. nfs_fh3 *handle;
  716. dir = rt_malloc(sizeof(nfs_dir));
  717. if (dir == NULL)
  718. {
  719. return NULL;
  720. }
  721. handle = get_handle(nfs, path);
  722. if (handle == NULL)
  723. {
  724. rt_free(dir);
  725. return NULL;
  726. }
  727. copy_handle(&dir->handle, handle);
  728. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  729. rt_free(handle);
  730. dir->cookie = 0;
  731. memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
  732. dir->entry = NULL;
  733. dir->eof = FALSE;
  734. memset(&dir->res, '\0', sizeof(dir->res));
  735. return dir;
  736. }
  737. char *nfs_readdir(nfs_filesystem *nfs, nfs_dir *dir)
  738. {
  739. static char name[NAME_MAX];
  740. if (nfs->nfs_client == NULL || dir == NULL)
  741. return NULL;
  742. if (dir->entry == NULL)
  743. {
  744. READDIR3args args;
  745. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  746. memset(&dir->res, '\0', sizeof(dir->res));
  747. args.dir = dir->handle;
  748. args.cookie = dir->cookie;
  749. memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
  750. args.count = 1024;
  751. if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
  752. {
  753. rt_kprintf("Readdir failed\n");
  754. return NULL;
  755. }
  756. else if (dir->res.status != NFS3_OK)
  757. {
  758. rt_kprintf("Readdir failed: %d\n", dir->res.status);
  759. return NULL;
  760. }
  761. memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
  762. dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
  763. dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
  764. }
  765. if (dir->eof == TRUE && dir->entry == NULL)
  766. return NULL;
  767. dir->cookie = dir->entry->cookie;
  768. strncpy(name, dir->entry->name, NAME_MAX-1);
  769. dir->entry = dir->entry->nextentry;
  770. name[NAME_MAX - 1] = '\0';
  771. return name;
  772. }
  773. int nfs_unlink(struct dfs_filesystem *fs, const char *path)
  774. {
  775. int ret = 0;
  776. nfs_filesystem *nfs;
  777. RT_ASSERT(fs != NULL);
  778. RT_ASSERT(fs->data != NULL);
  779. nfs = (nfs_filesystem *)fs->data;
  780. if (nfs_is_directory(nfs, path) == RT_FALSE)
  781. {
  782. /* remove file */
  783. REMOVE3args args;
  784. REMOVE3res res;
  785. nfs_fh3 *handle;
  786. handle = get_dir_handle(nfs, path);
  787. if (handle == NULL)
  788. return -1;
  789. args.object.dir = *handle;
  790. args.object.name = strrchr(path, '/') + 1;
  791. if (args.object.name == NULL)
  792. {
  793. args.object.name = (char *)path;
  794. }
  795. memset(&res, 0, sizeof(res));
  796. if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  797. {
  798. rt_kprintf("Remove failed\n");
  799. ret = -1;
  800. }
  801. else if (res.status != NFS3_OK)
  802. {
  803. rt_kprintf("Remove failed: %d\n", res.status);
  804. ret = -1;
  805. }
  806. xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
  807. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  808. rt_free(handle);
  809. }
  810. else
  811. {
  812. /* remove directory */
  813. RMDIR3args args;
  814. RMDIR3res res;
  815. nfs_fh3 *handle;
  816. handle = get_dir_handle(nfs, path);
  817. if (handle == NULL)
  818. return -1;
  819. args.object.dir = *handle;
  820. args.object.name = strrchr(path, '/') + 1;
  821. if (args.object.name == NULL)
  822. {
  823. args.object.name = (char *)path;
  824. }
  825. memset(&res, 0, sizeof(res));
  826. if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  827. {
  828. rt_kprintf("Rmdir failed\n");
  829. ret = -1;
  830. }
  831. else if (res.status != NFS3_OK)
  832. {
  833. rt_kprintf("Rmdir failed: %d\n", res.status);
  834. ret = -1;
  835. }
  836. xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
  837. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  838. rt_free(handle);
  839. }
  840. return ret;
  841. }
  842. int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
  843. {
  844. RENAME3args args;
  845. RENAME3res res;
  846. nfs_fh3 *sHandle;
  847. nfs_fh3 *dHandle;
  848. int ret = 0;
  849. nfs_filesystem *nfs;
  850. RT_ASSERT(fs != NULL);
  851. RT_ASSERT(fs->data != NULL);
  852. nfs = (nfs_filesystem *)fs->data;
  853. if (nfs->nfs_client == NULL)
  854. return -1;
  855. sHandle = get_dir_handle(nfs, src);
  856. if (sHandle == NULL)
  857. return -1;
  858. dHandle = get_dir_handle(nfs, dest);
  859. if (dHandle == NULL)
  860. return -1;
  861. args.from.dir = *sHandle;
  862. args.from.name = strrchr(src, '/') + 1;
  863. if (args.from.name == NULL)
  864. args.from.name = (char *)src;
  865. args.to.dir = *dHandle;
  866. args.to.name = strrchr(src, '/') + 1;
  867. if (args.to.name == NULL)
  868. args.to.name = (char *)dest;
  869. memset(&res, '\0', sizeof(res));
  870. if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  871. {
  872. rt_kprintf("Rename failed\n");
  873. ret = -1;
  874. }
  875. else if (res.status != NFS3_OK)
  876. {
  877. rt_kprintf("Rename failed: %d\n", res.status);
  878. ret = -1;
  879. }
  880. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
  881. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
  882. xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
  883. return ret;
  884. }
  885. int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, uint32_t count)
  886. {
  887. nfs_dir *dir;
  888. rt_uint32_t index;
  889. struct dirent *d;
  890. nfs_filesystem *nfs;
  891. char *name;
  892. RT_ASSERT(file->data != NULL);
  893. nfs = (nfs_filesystem *)((struct dfs_filesystem*)(file->data));
  894. dir = (nfs_dir *)(nfs->data);
  895. RT_ASSERT(dir != NULL);
  896. /* make integer count */
  897. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  898. if (count == 0)
  899. return -EINVAL;
  900. index = 0;
  901. while (1)
  902. {
  903. d = dirp + index;
  904. name = nfs_readdir(nfs, dir);
  905. if (name == NULL)
  906. break;
  907. d->d_type = DT_REG;
  908. d->d_namlen = rt_strlen(name);
  909. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  910. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  911. index ++;
  912. if (index * sizeof(struct dirent) >= count)
  913. break;
  914. }
  915. return index * sizeof(struct dirent);
  916. }
  917. static const struct dfs_file_ops nfs_fops =
  918. {
  919. nfs_open,
  920. nfs_close,
  921. nfs_ioctl,
  922. nfs_read,
  923. nfs_write,
  924. NULL, /* flush */
  925. nfs_lseek,
  926. nfs_getdents,
  927. NULL, /* poll */
  928. };
  929. static const struct dfs_filesystem_ops _nfs =
  930. {
  931. "nfs",
  932. DFS_FS_FLAG_DEFAULT,
  933. &nfs_fops,
  934. nfs_mount,
  935. nfs_unmount,
  936. NULL, /* mkfs */
  937. NULL, /* statfs */
  938. nfs_unlink,
  939. nfs_stat,
  940. nfs_rename,
  941. };
  942. int nfs_init(void)
  943. {
  944. /* register nfs file system */
  945. dfs_register(&_nfs);
  946. return RT_EOK;
  947. }
  948. INIT_FS_EXPORT(nfs_init);