1
0

dfs_nfs.c 27 KB

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