dfs_nfs.c 28 KB

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