1
0

dfs_nfs.c 20 KB

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