dfs_nfs.c 21 KB

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