dfs_nfs.c 22 KB

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