dfs_nfs.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  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 != RT_NULL)
  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. /* todo: update file size */
  568. }
  569. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  570. } while (count > 0);
  571. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  572. return total;
  573. }
  574. int nfs_lseek(struct dfs_fd *file, rt_off_t offset)
  575. {
  576. nfs_file *fd;
  577. if (file->type == FT_DIRECTORY)
  578. return -DFS_STATUS_EISDIR;
  579. fd = (nfs_file *)(file->data);
  580. RT_ASSERT(fd != RT_NULL);
  581. if (offset <= fd->size)
  582. {
  583. fd->offset = offset;
  584. return offset;
  585. }
  586. return -DFS_STATUS_EIO;
  587. }
  588. int nfs_close(struct dfs_fd *file)
  589. {
  590. if (file->type == FT_DIRECTORY)
  591. {
  592. struct nfs_dir *dir;
  593. dir = (struct nfs_dir *)file->data;
  594. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
  595. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  596. rt_free(dir);
  597. }
  598. else if (file->type == FT_REGULAR)
  599. {
  600. struct nfs_file *fd;
  601. fd = (struct nfs_file *)file->data;
  602. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
  603. rt_free(fd);
  604. }
  605. file->data = RT_NULL;
  606. return 0;
  607. }
  608. int nfs_open(struct dfs_fd *file)
  609. {
  610. struct nfs_filesystem *nfs;
  611. RT_ASSERT(file->fs != RT_NULL);
  612. RT_ASSERT(file->fs->data != RT_NULL);
  613. nfs = (struct nfs_filesystem *)file->fs->data;
  614. if (file->flags & DFS_O_DIRECTORY)
  615. {
  616. nfs_dir *dir;
  617. if (file->flags & DFS_O_CREAT)
  618. {
  619. if (nfs_mkdir(nfs, file->path, 0755) < 0)
  620. return -1;
  621. }
  622. /* open directory */
  623. dir = nfs_opendir(nfs, file->path);
  624. file->data = dir;
  625. }
  626. else
  627. {
  628. nfs_file *fp;
  629. nfs_fh3 *handle;
  630. /* create file */
  631. if (file->flags & DFS_O_CREAT)
  632. {
  633. if (nfs_create(nfs, file->path, 0664) < 0)
  634. return -1;
  635. }
  636. /* open file (get file handle ) */
  637. fp = rt_malloc(sizeof(nfs_file));
  638. if (fp == RT_NULL)
  639. return -1;
  640. handle = get_handle(nfs, file->path);
  641. if (handle == RT_NULL)
  642. {
  643. rt_free(fp);
  644. return -1;
  645. }
  646. /* get size of file */
  647. fp->size = nfs_get_filesize(nfs, handle);
  648. fp->offset = 0;
  649. fp->eof = FALSE;
  650. copy_handle(&fp->handle, handle);
  651. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  652. rt_free(handle);
  653. if (file->flags & DFS_O_APPEND)
  654. {
  655. fp->offset = fp->size;
  656. }
  657. /* set private file */
  658. file->data = fp;
  659. }
  660. return 0;
  661. }
  662. int nfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
  663. {
  664. GETATTR3args args;
  665. GETATTR3res res;
  666. fattr3 *info;
  667. nfs_fh3 *handle;
  668. struct nfs_filesystem *nfs;
  669. RT_ASSERT(fs != RT_NULL);
  670. RT_ASSERT(fs->data != RT_NULL);
  671. nfs = (struct nfs_filesystem *)fs->data;
  672. handle = get_handle(nfs, path);
  673. if (handle == RT_NULL)
  674. return -1;
  675. args.object = *handle;
  676. memset(&res, '\0', sizeof(res));
  677. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  678. {
  679. rt_kprintf("GetAttr failed\n");
  680. return -1;
  681. }
  682. else if (res.status != NFS3_OK)
  683. {
  684. rt_kprintf("Getattr failed: %d\n", res.status);
  685. return -1;
  686. }
  687. info = &res.GETATTR3res_u.resok.obj_attributes;
  688. st->st_dev = 0;
  689. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  690. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  691. if (info->type == NFS3DIR)
  692. {
  693. st->st_mode &= ~DFS_S_IFREG;
  694. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  695. }
  696. st->st_size = info->size;
  697. st->st_mtime = info->mtime.seconds;
  698. st->st_blksize = 512;
  699. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  700. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  701. rt_free(handle);
  702. return 0;
  703. }
  704. nfs_dir *nfs_opendir(struct nfs_filesystem *nfs, const char *path)
  705. {
  706. nfs_dir *dir;
  707. nfs_fh3 *handle;
  708. dir = rt_malloc(sizeof(nfs_dir));
  709. if (dir == RT_NULL)
  710. {
  711. return RT_NULL;
  712. }
  713. handle = get_handle(nfs, path);
  714. if (handle == RT_NULL)
  715. {
  716. rt_free(dir);
  717. return RT_NULL;
  718. }
  719. copy_handle(&dir->handle, handle);
  720. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  721. rt_free(handle);
  722. dir->cookie = 0;
  723. memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
  724. dir->entry = RT_NULL;
  725. dir->eof = FALSE;
  726. memset(&dir->res, '\0', sizeof(dir->res));
  727. return dir;
  728. }
  729. char *nfs_readdir(struct nfs_filesystem *nfs, nfs_dir *dir)
  730. {
  731. static char name[NAME_MAX];
  732. if (nfs->nfs_client == RT_NULL || dir == RT_NULL)
  733. return RT_NULL;
  734. if (dir->entry == RT_NULL)
  735. {
  736. READDIR3args args;
  737. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  738. memset(&dir->res, '\0', sizeof(dir->res));
  739. args.dir = dir->handle;
  740. args.cookie = dir->cookie;
  741. memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
  742. args.count = 1024;
  743. if (nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client) != RPC_SUCCESS)
  744. {
  745. rt_kprintf("Readdir failed\n");
  746. return RT_NULL;
  747. }
  748. else if (dir->res.status != NFS3_OK)
  749. {
  750. rt_kprintf("Readdir failed: %d\n", dir->res.status);
  751. return RT_NULL;
  752. }
  753. memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
  754. dir->eof = dir->res.READDIR3res_u.resok.reply.eof;
  755. dir->entry = dir->res.READDIR3res_u.resok.reply.entries;
  756. }
  757. if (dir->eof == TRUE && dir->entry == RT_NULL)
  758. return RT_NULL;
  759. dir->cookie = dir->entry->cookie;
  760. strncpy(name, dir->entry->name, NAME_MAX-1);
  761. dir->entry = dir->entry->nextentry;
  762. name[NAME_MAX - 1] = '\0';
  763. return name;
  764. }
  765. int nfs_unlink(struct dfs_filesystem *fs, const char *path)
  766. {
  767. int ret = 0;
  768. struct nfs_filesystem *nfs;
  769. RT_ASSERT(fs != RT_NULL);
  770. RT_ASSERT(fs->data != RT_NULL);
  771. nfs = (struct nfs_filesystem *)fs->data;
  772. if (nfs_is_directory(nfs, path) == RT_FALSE)
  773. {
  774. /* remove file */
  775. REMOVE3args args;
  776. REMOVE3res res;
  777. nfs_fh3 *handle;
  778. handle = get_dir_handle(nfs, path);
  779. if (handle == RT_NULL)
  780. return -1;
  781. args.object.dir = *handle;
  782. args.object.name = strrchr(path, '/') + 1;
  783. if (args.object.name == RT_NULL)
  784. {
  785. args.object.name = (char *)path;
  786. }
  787. memset(&res, 0, sizeof(res));
  788. if (nfsproc3_remove_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  789. {
  790. rt_kprintf("Remove failed\n");
  791. ret = -1;
  792. }
  793. else if (res.status != NFS3_OK)
  794. {
  795. rt_kprintf("Remove failed: %d\n", res.status);
  796. ret = -1;
  797. }
  798. xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
  799. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  800. rt_free(handle);
  801. }
  802. else
  803. {
  804. /* remove directory */
  805. RMDIR3args args;
  806. RMDIR3res res;
  807. nfs_fh3 *handle;
  808. handle = get_dir_handle(nfs, path);
  809. if (handle == RT_NULL)
  810. return -1;
  811. args.object.dir = *handle;
  812. args.object.name = strrchr(path, '/') + 1;
  813. if (args.object.name == RT_NULL)
  814. {
  815. args.object.name = (char *)path;
  816. }
  817. memset(&res, 0, sizeof(res));
  818. if (nfsproc3_rmdir_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  819. {
  820. rt_kprintf("Rmdir failed\n");
  821. ret = -1;
  822. }
  823. else if (res.status != NFS3_OK)
  824. {
  825. rt_kprintf("Rmdir failed: %d\n", res.status);
  826. ret = -1;
  827. }
  828. xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
  829. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  830. rt_free(handle);
  831. }
  832. return ret;
  833. }
  834. int nfs_rename(struct dfs_filesystem *fs, const char *src, const char *dest)
  835. {
  836. RENAME3args args;
  837. RENAME3res res;
  838. nfs_fh3 *sHandle;
  839. nfs_fh3 *dHandle;
  840. int ret = 0;
  841. struct nfs_filesystem *nfs;
  842. RT_ASSERT(fs != RT_NULL);
  843. RT_ASSERT(fs->data != RT_NULL);
  844. nfs = (struct nfs_filesystem *)fs->data;
  845. if (nfs->nfs_client == RT_NULL)
  846. return -1;
  847. sHandle = get_dir_handle(nfs, src);
  848. if (sHandle == RT_NULL)
  849. return -1;
  850. dHandle = get_dir_handle(nfs, dest);
  851. if (dHandle == RT_NULL)
  852. return -1;
  853. args.from.dir = *sHandle;
  854. args.from.name = strrchr(src, '/') + 1;
  855. if (args.from.name == RT_NULL)
  856. args.from.name = (char *)src;
  857. args.to.dir = *dHandle;
  858. args.to.name = strrchr(src, '/') + 1;
  859. if (args.to.name == RT_NULL)
  860. args.to.name = (char *)dest;
  861. memset(&res, '\0', sizeof(res));
  862. if (nfsproc3_rename_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  863. {
  864. rt_kprintf("Rename failed\n");
  865. ret = -1;
  866. }
  867. else if (res.status != NFS3_OK)
  868. {
  869. rt_kprintf("Rename failed: %d\n", res.status);
  870. ret = -1;
  871. }
  872. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
  873. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
  874. xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
  875. return ret;
  876. }
  877. int nfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
  878. {
  879. nfs_dir *dir;
  880. rt_uint32_t index;
  881. struct dirent *d;
  882. struct nfs_filesystem *nfs;
  883. char *name;
  884. dir = (nfs_dir *)(file->data);
  885. RT_ASSERT(dir != RT_NULL);
  886. RT_ASSERT(file->fs != RT_NULL);
  887. RT_ASSERT(file->fs->data != RT_NULL);
  888. nfs = (struct nfs_filesystem *)file->fs->data;
  889. /* make integer count */
  890. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  891. if (count == 0)
  892. return -DFS_STATUS_EINVAL;
  893. index = 0;
  894. while (1)
  895. {
  896. char *fn;
  897. d = dirp + index;
  898. name = nfs_readdir(nfs, dir);
  899. if (name == RT_NULL)
  900. break;
  901. d->d_type = DFS_DT_REG;
  902. d->d_namlen = rt_strlen(name);
  903. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  904. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  905. index ++;
  906. if (index * sizeof(struct dirent) >= count)
  907. break;
  908. }
  909. return index * sizeof(struct dirent);
  910. }
  911. static const struct dfs_filesystem_operation _nfs =
  912. {
  913. "nfs",
  914. DFS_FS_FLAG_DEFAULT,
  915. nfs_mount,
  916. nfs_unmount,
  917. RT_NULL, /* mkfs */
  918. RT_NULL, /* statfs */
  919. nfs_open,
  920. nfs_close,
  921. nfs_ioctl,
  922. nfs_read,
  923. nfs_write,
  924. RT_NULL, /* flush */
  925. nfs_lseek,
  926. nfs_getdents,
  927. nfs_unlink,
  928. nfs_stat,
  929. nfs_rename,
  930. };
  931. int nfs_init(void)
  932. {
  933. /* register fatfs file system */
  934. dfs_register(&_nfs);
  935. return RT_EOK;
  936. }
  937. INIT_FS_EXPORT(nfs_init);