dfs_nfs.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  1. #include <stdio.h>
  2. #include <rtthread.h>
  3. #include <dfs_fs.h>
  4. #include <dfs_def.h>
  5. #include <rpc/rpc.h>
  6. #include "mount.h"
  7. #include "nfs.h"
  8. #define NAME_MAX 64
  9. struct nfs_file
  10. {
  11. nfs_fh3 handle; /* handle */
  12. size_t offset; /* current offset */
  13. size_t size; /* total size */
  14. bool_t eof; /* end of file */
  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. rt_bool_t nfs_is_directory(struct nfs_filesystem* nfs, const char* name)
  216. {
  217. GETATTR3args args;
  218. GETATTR3res res;
  219. fattr3 *info;
  220. nfs_fh3 *handle;
  221. rt_bool_t result;
  222. result = RT_FALSE;
  223. handle = get_handle(nfs, name);
  224. if(handle == RT_NULL) return RT_FALSE;
  225. args.object = *handle;
  226. memset(&res, '\0', sizeof(res));
  227. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  228. {
  229. rt_kprintf("GetAttr failed\n");
  230. return RT_FALSE;
  231. }
  232. else if(res.status!=NFS3_OK)
  233. {
  234. rt_kprintf("Getattr failed: %d\n", res.status);
  235. return RT_FALSE;
  236. }
  237. info=&res.GETATTR3res_u.resok.obj_attributes;
  238. if (info->type == NFS3DIR) result = RT_TRUE;
  239. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  240. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  241. rt_free(handle);
  242. return result;
  243. }
  244. int nfs_create(struct nfs_filesystem* nfs, const char *name, mode_t mode)
  245. {
  246. CREATE3args args;
  247. CREATE3res res;
  248. int ret=0;
  249. nfs_fh3 *handle;
  250. if(nfs->nfs_client==RT_NULL)
  251. {
  252. return -1;
  253. }
  254. handle=get_dir_handle(nfs, name);
  255. if(handle==RT_NULL)
  256. {
  257. return -1;
  258. }
  259. args.where.dir=*handle;
  260. args.where.name=strrchr(name, '/') + 1;
  261. if(args.where.name==RT_NULL)
  262. {
  263. args.where.name=(char *)name;
  264. }
  265. args.how.mode=GUARDED;
  266. args.how.createhow3_u.obj_attributes.mode.set_it=TRUE;
  267. args.how.createhow3_u.obj_attributes.mode.set_mode3_u.mode=mode;
  268. args.how.createhow3_u.obj_attributes.uid.set_it=FALSE;
  269. args.how.createhow3_u.obj_attributes.gid.set_it=FALSE;
  270. args.how.createhow3_u.obj_attributes.size.set_it=FALSE;
  271. args.how.createhow3_u.obj_attributes.atime.set_it=DONT_CHANGE;
  272. args.how.createhow3_u.obj_attributes.mtime.set_it=DONT_CHANGE;
  273. memset(&res, 0, sizeof(res));
  274. if(nfsproc3_create_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  275. {
  276. rt_kprintf("Create failed\n");
  277. ret = -1;
  278. }
  279. else if(res.status!=NFS3_OK)
  280. {
  281. rt_kprintf("Create failed: %d\n", res.status);
  282. ret = -1;
  283. }
  284. xdr_free((xdrproc_t)xdr_CREATE3res, (char *)&res);
  285. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  286. rt_free(handle);
  287. return ret;
  288. }
  289. int nfs_mkdir(struct nfs_filesystem* nfs, const char *name, mode_t mode)
  290. {
  291. MKDIR3args args;
  292. MKDIR3res res;
  293. int ret=0;
  294. nfs_fh3 *handle;
  295. if(nfs->nfs_client==RT_NULL)
  296. return -1;
  297. handle=get_dir_handle(nfs, name);
  298. if(handle==RT_NULL)
  299. return -1;
  300. args.where.dir=*handle;
  301. args.where.name=strrchr(name, '/') + 1;
  302. if(args.where.name==RT_NULL)
  303. {
  304. args.where.name=(char *)name;
  305. }
  306. args.attributes.mode.set_it=TRUE;
  307. args.attributes.mode.set_mode3_u.mode=mode;
  308. args.attributes.uid.set_it=FALSE;
  309. args.attributes.gid.set_it=FALSE;
  310. args.attributes.size.set_it=FALSE;
  311. args.attributes.atime.set_it=DONT_CHANGE;
  312. args.attributes.mtime.set_it=DONT_CHANGE;
  313. memset(&res, 0, sizeof(res));
  314. if(nfsproc3_mkdir_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  315. {
  316. rt_kprintf("Mkdir failed\n");
  317. ret=-1;
  318. }
  319. else if(res.status!=NFS3_OK)
  320. {
  321. rt_kprintf("Mkdir failed: %d\n", res.status);
  322. ret=-1;
  323. }
  324. xdr_free((xdrproc_t)xdr_MKDIR3res, (char *)&res);
  325. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  326. rt_free(handle);
  327. return ret;
  328. }
  329. /* mount(RT_NULL, "/mnt", "nfs", 0, "192.168.1.1:/export") */
  330. int nfs_mount(struct dfs_filesystem* fs, unsigned long rwflag, const void* data)
  331. {
  332. mountres3 res;
  333. struct nfs_filesystem* nfs;
  334. nfs = (struct nfs_filesystem*)rt_malloc(sizeof(struct nfs_filesystem));
  335. memset(nfs, 0, sizeof(struct nfs_filesystem));
  336. if (nfs_parse_host_export((const char*)data, nfs->host, HOST_LENGTH,
  337. nfs->export, EXPORT_PATH_LENGTH) < 0)
  338. {
  339. rt_kprintf("host or export path error\n");
  340. goto __return;
  341. }
  342. nfs->mount_client=clnt_create((char *)nfs->host, MOUNT_PROGRAM, MOUNT_V3, "udp");
  343. if(nfs->mount_client==RT_NULL)
  344. {
  345. rt_kprintf("create mount client failed\n");
  346. goto __return;
  347. }
  348. memset(&res, '\0', sizeof(mountres3));
  349. if(mountproc3_mnt_3((char *)nfs->export, &res, nfs->mount_client)!=RPC_SUCCESS)
  350. {
  351. rt_kprintf("nfs mount failed\n");
  352. goto __return;
  353. }
  354. else if(res.fhs_status!=MNT3_OK)
  355. {
  356. rt_kprintf("nfs mount failed\n");
  357. goto __return;
  358. }
  359. nfs->nfs_client=clnt_create((char *)nfs->host, NFS_PROGRAM, NFS_V3, "udp");
  360. if(nfs->nfs_client == RT_NULL)
  361. {
  362. rt_kprintf("creat nfs client failed\n");
  363. goto __return;
  364. }
  365. copy_handle(&nfs->root_handle, (nfs_fh3 *)&res.mountres3_u.mountinfo.fhandle);
  366. copy_handle(&nfs->current_handle, &nfs->root_handle);
  367. nfs->nfs_client->cl_auth=authnone_create();
  368. fs->data = nfs;
  369. return 0;
  370. __return:
  371. if (nfs != RT_NULL)
  372. {
  373. if (nfs->mount_client != RT_NULL)
  374. {
  375. clnt_destroy(nfs->mount_client);
  376. }
  377. if (nfs->nfs_client != RT_NULL)
  378. {
  379. if (nfs->nfs_client->cl_auth != RT_NULL)
  380. {
  381. auth_destroy(nfs->nfs_client->cl_auth);
  382. }
  383. clnt_destroy(nfs->nfs_client);
  384. }
  385. rt_free(nfs);
  386. }
  387. return -1;
  388. }
  389. int nfs_unmount(struct dfs_filesystem* fs)
  390. {
  391. struct nfs_filesystem* nfs;
  392. RT_ASSERT(fs != RT_NULL);
  393. RT_ASSERT(fs->data != RT_NULL);
  394. nfs = (struct nfs_filesystem *)fs->data;
  395. if (nfs->mount_client != RT_NULL &&
  396. mountproc3_umnt_3((char *)nfs->export, RT_NULL, nfs->mount_client) != RPC_SUCCESS)
  397. {
  398. rt_kprintf("umount failed\n");
  399. return -1;
  400. }
  401. /* destroy nfs client */
  402. if(nfs->nfs_client != RT_NULL)
  403. {
  404. if(nfs->nfs_client->cl_auth!=RT_NULL)
  405. {
  406. auth_destroy(nfs->nfs_client->cl_auth);
  407. nfs->nfs_client->cl_auth = RT_NULL;
  408. }
  409. clnt_destroy(nfs->nfs_client);
  410. nfs->nfs_client = RT_NULL;
  411. }
  412. /* destroy mount client */
  413. if(nfs->mount_client != RT_NULL)
  414. {
  415. if(nfs->mount_client->cl_auth != RT_NULL)
  416. {
  417. auth_destroy(nfs->mount_client->cl_auth);
  418. nfs->mount_client->cl_auth = RT_NULL;
  419. }
  420. clnt_destroy(nfs->mount_client);
  421. nfs->mount_client=RT_NULL;
  422. }
  423. rt_free(nfs);
  424. fs->data = RT_NULL;
  425. return 0;
  426. }
  427. int nfs_ioctl(struct dfs_fd* file, int cmd, void* args)
  428. {
  429. return -DFS_STATUS_ENOSYS;
  430. }
  431. int nfs_read(struct dfs_fd* file, void *buf, rt_size_t count)
  432. {
  433. READ3args args;
  434. READ3res res;
  435. ssize_t bytes;
  436. nfs_file *fd;
  437. struct nfs_filesystem* nfs;
  438. if (file->type == FT_DIRECTORY)
  439. return -DFS_STATUS_EISDIR;
  440. fd = (nfs_file *)(file->data);
  441. RT_ASSERT(fd != RT_NULL);
  442. RT_ASSERT(file->fs != RT_NULL);
  443. RT_ASSERT(file->fs->data != RT_NULL);
  444. nfs = (struct nfs_filesystem *)file->fs->data;
  445. if(nfs->nfs_client==RT_NULL)
  446. return -1;
  447. /* end of file */
  448. if (fd->eof == TRUE) return 0;
  449. args.file=fd->handle;
  450. args.offset=fd->offset;
  451. args.count=count;
  452. memset(&res, 0, sizeof(res));
  453. if(nfsproc3_read_3(args, &res, nfs->nfs_client) != RPC_SUCCESS)
  454. {
  455. rt_kprintf("Read failed\n");
  456. bytes = 0;
  457. }
  458. else if(res.status!=NFS3_OK)
  459. {
  460. rt_kprintf("Read failed: %d\n", res.status);
  461. bytes = 0;
  462. }
  463. else
  464. {
  465. if(res.READ3res_u.resok.eof)
  466. {
  467. /* something should probably be here */
  468. fd->eof = TRUE;
  469. }
  470. bytes=res.READ3res_u.resok.count;
  471. fd->offset += bytes;
  472. /* update current position */
  473. file->pos = fd->offset;
  474. memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
  475. }
  476. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  477. return bytes;
  478. }
  479. int nfs_write(struct dfs_fd* file, const void *buf, rt_size_t count)
  480. {
  481. WRITE3args args;
  482. WRITE3res res;
  483. ssize_t bytes;
  484. nfs_file *fd;
  485. struct nfs_filesystem* nfs;
  486. if (file->type == FT_DIRECTORY)
  487. return -DFS_STATUS_EISDIR;
  488. fd = (nfs_file *)(file->data);
  489. RT_ASSERT(fd != RT_NULL);
  490. RT_ASSERT(file->fs != RT_NULL);
  491. RT_ASSERT(file->fs->data != RT_NULL);
  492. nfs = (struct nfs_filesystem *)file->fs->data;
  493. if(nfs->nfs_client==RT_NULL)
  494. return -1;
  495. args.file=fd->handle;
  496. args.stable=FILE_SYNC;
  497. args.offset=fd->offset;
  498. memset(&res, 0, sizeof(res));
  499. args.data.data_val=(void *)buf;
  500. args.count=args.data.data_len=count;
  501. if(nfsproc3_write_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  502. {
  503. rt_kprintf("Write failed\n");
  504. bytes = 0;
  505. }
  506. else if(res.status!=NFS3_OK)
  507. {
  508. rt_kprintf("Write failed: %d\n", res.status);
  509. bytes = 0;
  510. }
  511. else
  512. {
  513. bytes=res.WRITE3res_u.resok.count;
  514. fd->offset+=bytes;
  515. /* update current position */
  516. file->pos = fd->offset;
  517. /* todo: update file size */
  518. }
  519. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  520. return bytes;
  521. }
  522. int nfs_lseek(struct dfs_fd* file, rt_off_t offset)
  523. {
  524. nfs_file *fd;
  525. if (file->type == FT_DIRECTORY)
  526. return -DFS_STATUS_EISDIR;
  527. fd = (nfs_file *)(file->data);
  528. RT_ASSERT(fd != RT_NULL);
  529. if (offset < fd->size)
  530. {
  531. fd->offset = offset;
  532. return offset;
  533. }
  534. return -DFS_STATUS_EIO;
  535. }
  536. int nfs_close(struct dfs_fd* file)
  537. {
  538. if (file->type == FT_DIRECTORY)
  539. {
  540. struct nfs_dir* dir;
  541. dir = (struct nfs_dir*)file->data;
  542. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
  543. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  544. rt_free(dir);
  545. }
  546. else if (file->type == FT_REGULAR)
  547. {
  548. struct nfs_file* fd;
  549. fd = (struct nfs_file*)file->data;
  550. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
  551. rt_free(fd);
  552. }
  553. file->data = RT_NULL;
  554. return 0;
  555. }
  556. int nfs_open(struct dfs_fd* file)
  557. {
  558. struct nfs_filesystem* nfs;
  559. RT_ASSERT(file->fs != RT_NULL);
  560. RT_ASSERT(file->fs->data != RT_NULL);
  561. nfs = (struct nfs_filesystem *)file->fs->data;
  562. if (file->flags & DFS_O_DIRECTORY)
  563. {
  564. nfs_dir *dir;
  565. if (file->flags & DFS_O_CREAT)
  566. {
  567. if (nfs_mkdir(nfs, file->path, 555) < 0)
  568. return -1;
  569. }
  570. /* open directory */
  571. dir = nfs_opendir(nfs, file->path);
  572. file->data = dir;
  573. }
  574. else
  575. {
  576. nfs_file *fp;
  577. nfs_fh3 *handle;
  578. /* create file */
  579. if (file->flags & DFS_O_CREAT)
  580. {
  581. if (nfs_create(nfs, file->path, 555) < 0) return -1;
  582. }
  583. /* open file (get file handle ) */
  584. fp=rt_malloc(sizeof(nfs_file));
  585. if(fp == RT_NULL)
  586. return -1;
  587. handle = get_handle(nfs, file->path);
  588. if(handle == RT_NULL)
  589. {
  590. rt_free(fp);
  591. return -1;
  592. }
  593. /* get size of file */
  594. fp->size = nfs_get_filesize(nfs, handle);
  595. fp->offset=0;
  596. fp->eof = FALSE;
  597. copy_handle(&fp->handle, handle);
  598. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  599. rt_free(handle);
  600. if (file->flags & DFS_O_APPEND)
  601. {
  602. fp->offset = fp->size;
  603. }
  604. /* set private file */
  605. file->data = fp;
  606. }
  607. return 0;
  608. }
  609. int nfs_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
  610. {
  611. GETATTR3args args;
  612. GETATTR3res res;
  613. fattr3 *info;
  614. nfs_fh3 *handle;
  615. struct nfs_filesystem* nfs;
  616. RT_ASSERT(fs != RT_NULL);
  617. RT_ASSERT(fs->data != RT_NULL);
  618. nfs = (struct nfs_filesystem *)fs->data;
  619. handle = get_handle(nfs, path);
  620. if(handle == RT_NULL)
  621. return -1;
  622. args.object = *handle;
  623. memset(&res, '\0', sizeof(res));
  624. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  625. {
  626. rt_kprintf("GetAttr failed\n");
  627. return -1;
  628. }
  629. else if(res.status!=NFS3_OK)
  630. {
  631. rt_kprintf("Getattr failed: %d\n", res.status);
  632. return -1;
  633. }
  634. info = &res.GETATTR3res_u.resok.obj_attributes;
  635. st->st_dev = 0;
  636. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  637. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  638. if (info->type == NFS3DIR)
  639. {
  640. st->st_mode &= ~DFS_S_IFREG;
  641. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  642. }
  643. st->st_size = info->size;
  644. st->st_mtime = info->mtime.seconds;
  645. st->st_blksize = 512;
  646. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  647. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  648. rt_free(handle);
  649. return 0;
  650. }
  651. nfs_dir *nfs_opendir(struct nfs_filesystem* nfs, const char *path)
  652. {
  653. nfs_dir *dir;
  654. nfs_fh3 *handle;
  655. dir=rt_malloc(sizeof(nfs_dir));
  656. if(dir==RT_NULL)
  657. {
  658. return RT_NULL;
  659. }
  660. handle = get_handle(nfs, path);
  661. if(handle == RT_NULL)
  662. {
  663. rt_free(dir);
  664. return RT_NULL;
  665. }
  666. copy_handle(&dir->handle, handle);
  667. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  668. rt_free(handle);
  669. dir->cookie=0;
  670. memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
  671. dir->entry=RT_NULL;
  672. dir->eof=FALSE;
  673. memset(&dir->res, '\0', sizeof(dir->res));
  674. return dir;
  675. }
  676. char *nfs_readdir(struct nfs_filesystem* nfs, nfs_dir *dir)
  677. {
  678. static char name[NAME_MAX];
  679. if(nfs->nfs_client==RT_NULL || dir == RT_NULL)
  680. return RT_NULL;
  681. if(dir->entry==RT_NULL)
  682. {
  683. READDIR3args args;
  684. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  685. memset(&dir->res, '\0', sizeof(dir->res));
  686. args.dir=dir->handle;
  687. args.cookie=dir->cookie;
  688. memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
  689. args.count=1024;
  690. if(nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client)!=RPC_SUCCESS)
  691. {
  692. rt_kprintf("Readdir failed\n");
  693. return RT_NULL;
  694. }
  695. else if(dir->res.status!=NFS3_OK)
  696. {
  697. rt_kprintf("Readdir failed: %d\n", dir->res.status);
  698. return RT_NULL;
  699. }
  700. memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
  701. dir->eof=dir->res.READDIR3res_u.resok.reply.eof;
  702. dir->entry=dir->res.READDIR3res_u.resok.reply.entries;
  703. }
  704. if(dir->eof==TRUE && dir->entry==RT_NULL)
  705. return RT_NULL;
  706. dir->cookie=dir->entry->cookie;
  707. strncpy(name, dir->entry->name, NAME_MAX-1);
  708. dir->entry=dir->entry->nextentry;
  709. name[NAME_MAX - 1]='\0';
  710. return name;
  711. }
  712. int nfs_unlink(struct dfs_filesystem* fs, const char* path)
  713. {
  714. int ret = 0;
  715. struct nfs_filesystem* nfs;
  716. RT_ASSERT(fs != RT_NULL);
  717. RT_ASSERT(fs->data != RT_NULL);
  718. nfs = (struct nfs_filesystem *)fs->data;
  719. if (nfs_is_directory(nfs, path) == RT_FALSE)
  720. {
  721. /* remove file */
  722. REMOVE3args args;
  723. REMOVE3res res;
  724. nfs_fh3 *handle;
  725. handle = get_dir_handle(nfs, path);
  726. if(handle == RT_NULL) return -1;
  727. args.object.dir=*handle;
  728. args.object.name=strrchr(path, '/') + 1;
  729. if(args.object.name==RT_NULL)
  730. {
  731. args.object.name=(char *)path;
  732. }
  733. memset(&res, 0, sizeof(res));
  734. if(nfsproc3_remove_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  735. {
  736. rt_kprintf("Remove failed\n");
  737. ret=-1;
  738. }
  739. else if(res.status!=NFS3_OK)
  740. {
  741. rt_kprintf("Remove failed: %d\n", res.status);
  742. ret=-1;
  743. }
  744. xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
  745. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  746. rt_free(handle);
  747. }
  748. else
  749. {
  750. /* remove directory */
  751. RMDIR3args args;
  752. RMDIR3res res;
  753. nfs_fh3 *handle;
  754. handle=get_dir_handle(nfs, path);
  755. if(handle==RT_NULL) return -1;
  756. args.object.dir=*handle;
  757. args.object.name=strrchr(path, '/') + 1;
  758. if(args.object.name==RT_NULL)
  759. {
  760. args.object.name=(char *)path;
  761. }
  762. memset(&res, 0, sizeof(res));
  763. if(nfsproc3_rmdir_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  764. {
  765. rt_kprintf("Rmdir failed\n");
  766. ret = -1;
  767. }
  768. else if(res.status!=NFS3_OK)
  769. {
  770. rt_kprintf("Rmdir failed: %d\n", res.status);
  771. ret = -1;
  772. }
  773. xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
  774. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  775. rt_free(handle);
  776. }
  777. return ret;
  778. }
  779. int nfs_rename(struct dfs_filesystem* fs, const char *src, const char *dest)
  780. {
  781. RENAME3args args;
  782. RENAME3res res;
  783. nfs_fh3 *sHandle;
  784. nfs_fh3 *dHandle;
  785. int ret=0;
  786. struct nfs_filesystem* nfs;
  787. RT_ASSERT(fs != RT_NULL);
  788. RT_ASSERT(fs->data != RT_NULL);
  789. nfs = (struct nfs_filesystem *)fs->data;
  790. if(nfs->nfs_client==RT_NULL)
  791. return -1;
  792. sHandle=get_dir_handle(nfs, src);
  793. if(sHandle==RT_NULL)
  794. return -1;
  795. dHandle=get_dir_handle(nfs, dest);
  796. if(dHandle==RT_NULL)
  797. return -1;
  798. args.from.dir=*sHandle;
  799. args.from.name=strrchr(src, '/') + 1;
  800. if(args.from.name==RT_NULL)
  801. args.from.name=(char *)src;
  802. args.to.dir=*dHandle;
  803. args.to.name=strrchr(src, '/') + 1;
  804. if(args.to.name==RT_NULL)
  805. args.to.name=(char *)dest;
  806. memset(&res, '\0', sizeof(res));
  807. if(nfsproc3_rename_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  808. {
  809. rt_kprintf("Rename failed\n");
  810. ret = -1;
  811. }
  812. else if(res.status!=NFS3_OK)
  813. {
  814. rt_kprintf("Rename failed: %d\n", res.status);
  815. ret = -1;
  816. }
  817. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
  818. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
  819. xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
  820. return ret;
  821. }
  822. int nfs_getdents(struct dfs_fd* file, struct dirent* dirp, rt_uint32_t count)
  823. {
  824. nfs_dir *dir;
  825. rt_uint32_t index;
  826. struct dirent* d;
  827. struct nfs_filesystem* nfs;
  828. char *name;
  829. dir = (nfs_dir *)(file->data);
  830. RT_ASSERT(dir != RT_NULL);
  831. RT_ASSERT(file->fs != RT_NULL);
  832. RT_ASSERT(file->fs->data != RT_NULL);
  833. nfs = (struct nfs_filesystem *)file->fs->data;
  834. /* make integer count */
  835. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  836. if ( count == 0 ) return -DFS_STATUS_EINVAL;
  837. index = 0;
  838. while (1)
  839. {
  840. char *fn;
  841. d = dirp + index;
  842. name = nfs_readdir(nfs, dir);
  843. if (name == RT_NULL) break;
  844. d->d_type = DFS_DT_REG;
  845. d->d_namlen = rt_strlen(name);
  846. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  847. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  848. index ++;
  849. if ( index * sizeof(struct dirent) >= count )
  850. break;
  851. }
  852. return index * sizeof(struct dirent);
  853. }
  854. static const struct dfs_filesystem_operation _nfs =
  855. {
  856. "nfs",
  857. nfs_mount,
  858. nfs_unmount,
  859. RT_NULL, /* mkfs */
  860. RT_NULL, /* statfs */
  861. nfs_open,
  862. nfs_close,
  863. nfs_ioctl,
  864. nfs_read,
  865. nfs_write,
  866. RT_NULL, /* flush */
  867. nfs_lseek,
  868. nfs_getdents,
  869. nfs_unlink,
  870. nfs_stat,
  871. nfs_rename,
  872. };
  873. int nfs_init(void)
  874. {
  875. /* register fatfs file system */
  876. dfs_register(&_nfs);
  877. return RT_EOK;
  878. }