dfs_nfs.c 27 KB

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