dfs_nfs.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. memcpy(buf, res.READ3res_u.resok.data.data_val, bytes);
  473. }
  474. xdr_free((xdrproc_t)xdr_READ3res, (char *)&res);
  475. return bytes;
  476. }
  477. int nfs_write(struct dfs_fd* file, const void *buf, rt_size_t count)
  478. {
  479. WRITE3args args;
  480. WRITE3res res;
  481. ssize_t bytes;
  482. nfs_file *fd;
  483. struct nfs_filesystem* nfs;
  484. if (file->type == FT_DIRECTORY)
  485. return -DFS_STATUS_EISDIR;
  486. fd = (nfs_file *)(file->data);
  487. RT_ASSERT(fd != RT_NULL);
  488. RT_ASSERT(file->fs != RT_NULL);
  489. RT_ASSERT(file->fs->data != RT_NULL);
  490. nfs = (struct nfs_filesystem *)file->fs->data;
  491. if(nfs->nfs_client==RT_NULL)
  492. return -1;
  493. args.file=fd->handle;
  494. args.stable=FILE_SYNC;
  495. args.offset=fd->offset;
  496. memset(&res, 0, sizeof(res));
  497. args.data.data_val=(void *)buf;
  498. args.count=args.data.data_len=count;
  499. if(nfsproc3_write_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  500. {
  501. rt_kprintf("Write failed\n");
  502. bytes = 0;
  503. }
  504. else if(res.status!=NFS3_OK)
  505. {
  506. rt_kprintf("Write failed: %d\n", res.status);
  507. bytes = 0;
  508. }
  509. else
  510. {
  511. bytes=res.WRITE3res_u.resok.count;
  512. fd->offset+=bytes;
  513. }
  514. xdr_free((xdrproc_t)xdr_WRITE3res, (char *)&res);
  515. return bytes;
  516. }
  517. int nfs_lseek(struct dfs_fd* file, rt_off_t offset)
  518. {
  519. nfs_file *fd;
  520. if (file->type == FT_DIRECTORY)
  521. return -DFS_STATUS_EISDIR;
  522. fd = (nfs_file *)(file->data);
  523. RT_ASSERT(fd != RT_NULL);
  524. if (offset < fd->size)
  525. {
  526. fd->offset = offset;
  527. return offset;
  528. }
  529. return -DFS_STATUS_EIO;
  530. }
  531. int nfs_close(struct dfs_fd* file)
  532. {
  533. if (file->type == FT_DIRECTORY)
  534. {
  535. struct nfs_dir* dir;
  536. dir = (struct nfs_dir*)file->data;
  537. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&dir->handle);
  538. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  539. rt_free(dir);
  540. }
  541. else if (file->type == FT_REGULAR)
  542. {
  543. struct nfs_file* fd;
  544. fd = (struct nfs_file*)file->data;
  545. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)&fd->handle);
  546. rt_free(fd);
  547. }
  548. file->data = RT_NULL;
  549. return 0;
  550. }
  551. int nfs_open(struct dfs_fd* file)
  552. {
  553. struct nfs_filesystem* nfs;
  554. RT_ASSERT(file->fs != RT_NULL);
  555. RT_ASSERT(file->fs->data != RT_NULL);
  556. nfs = (struct nfs_filesystem *)file->fs->data;
  557. if (file->flags & DFS_O_DIRECTORY)
  558. {
  559. nfs_dir *dir;
  560. if (file->flags & DFS_O_CREAT)
  561. {
  562. if (nfs_mkdir(nfs, file->path, 555) < 0)
  563. return -1;
  564. }
  565. /* open directory */
  566. dir = nfs_opendir(nfs, file->path);
  567. file->data = dir;
  568. }
  569. else
  570. {
  571. nfs_file *fp;
  572. nfs_fh3 *handle;
  573. /* create file */
  574. if (file->flags & DFS_O_CREAT)
  575. {
  576. if (nfs_create(nfs, file->path, 555) < 0) return -1;
  577. }
  578. /* open file (get file handle ) */
  579. fp=rt_malloc(sizeof(nfs_file));
  580. if(fp == RT_NULL)
  581. return -1;
  582. handle = get_handle(nfs, file->path);
  583. if(handle == RT_NULL)
  584. {
  585. rt_free(fp);
  586. return -1;
  587. }
  588. /* get size of file */
  589. fp->size = nfs_get_filesize(nfs, handle);
  590. fp->offset=0;
  591. fp->eof = FALSE;
  592. copy_handle(&fp->handle, handle);
  593. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  594. rt_free(handle);
  595. if (file->flags & DFS_O_APPEND)
  596. {
  597. fp->offset = fp->size;
  598. }
  599. /* set private file */
  600. file->data = fp;
  601. }
  602. return 0;
  603. }
  604. int nfs_stat(struct dfs_filesystem* fs, const char *path, struct _stat *st)
  605. {
  606. GETATTR3args args;
  607. GETATTR3res res;
  608. fattr3 *info;
  609. nfs_fh3 *handle;
  610. struct nfs_filesystem* nfs;
  611. RT_ASSERT(fs != RT_NULL);
  612. RT_ASSERT(fs->data != RT_NULL);
  613. nfs = (struct nfs_filesystem *)fs->data;
  614. handle = get_handle(nfs, path);
  615. if(handle == RT_NULL)
  616. return -1;
  617. args.object = *handle;
  618. memset(&res, '\0', sizeof(res));
  619. if (nfsproc3_getattr_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  620. {
  621. rt_kprintf("GetAttr failed\n");
  622. return -1;
  623. }
  624. else if(res.status!=NFS3_OK)
  625. {
  626. rt_kprintf("Getattr failed: %d\n", res.status);
  627. return -1;
  628. }
  629. info = &res.GETATTR3res_u.resok.obj_attributes;
  630. st->st_dev = 0;
  631. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  632. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  633. if (info->type == NFS3DIR)
  634. {
  635. st->st_mode &= ~DFS_S_IFREG;
  636. st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  637. }
  638. st->st_size = info->size;
  639. st->st_mtime = info->mtime.seconds;
  640. st->st_blksize = 512;
  641. xdr_free((xdrproc_t)xdr_GETATTR3res, (char *)&res);
  642. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  643. rt_free(handle);
  644. return 0;
  645. }
  646. nfs_dir *nfs_opendir(struct nfs_filesystem* nfs, const char *path)
  647. {
  648. nfs_dir *dir;
  649. nfs_fh3 *handle;
  650. dir=rt_malloc(sizeof(nfs_dir));
  651. if(dir==RT_NULL)
  652. {
  653. return RT_NULL;
  654. }
  655. handle = get_handle(nfs, path);
  656. if(handle == RT_NULL)
  657. {
  658. rt_free(dir);
  659. return RT_NULL;
  660. }
  661. copy_handle(&dir->handle, handle);
  662. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  663. rt_free(handle);
  664. dir->cookie=0;
  665. memset(&dir->cookieverf, '\0', sizeof(cookieverf3));
  666. dir->entry=RT_NULL;
  667. dir->eof=FALSE;
  668. memset(&dir->res, '\0', sizeof(dir->res));
  669. return dir;
  670. }
  671. char *nfs_readdir(struct nfs_filesystem* nfs, nfs_dir *dir)
  672. {
  673. static char name[NAME_MAX];
  674. if(nfs->nfs_client==RT_NULL || dir == RT_NULL)
  675. return RT_NULL;
  676. if(dir->entry==RT_NULL)
  677. {
  678. READDIR3args args;
  679. xdr_free((xdrproc_t)xdr_READDIR3res, (char *)&dir->res);
  680. memset(&dir->res, '\0', sizeof(dir->res));
  681. args.dir=dir->handle;
  682. args.cookie=dir->cookie;
  683. memcpy(&args.cookieverf, &dir->cookieverf, sizeof(cookieverf3));
  684. args.count=1024;
  685. if(nfsproc3_readdir_3(args, &dir->res, nfs->nfs_client)!=RPC_SUCCESS)
  686. {
  687. rt_kprintf("Readdir failed\n");
  688. return RT_NULL;
  689. }
  690. else if(dir->res.status!=NFS3_OK)
  691. {
  692. rt_kprintf("Readdir failed: %d\n", dir->res.status);
  693. return RT_NULL;
  694. }
  695. memcpy(&dir->cookieverf, &dir->res.READDIR3res_u.resok.cookieverf, sizeof(cookieverf3));
  696. dir->eof=dir->res.READDIR3res_u.resok.reply.eof;
  697. dir->entry=dir->res.READDIR3res_u.resok.reply.entries;
  698. }
  699. if(dir->eof==TRUE && dir->entry==RT_NULL)
  700. return RT_NULL;
  701. dir->cookie=dir->entry->cookie;
  702. strncpy(name, dir->entry->name, NAME_MAX-1);
  703. dir->entry=dir->entry->nextentry;
  704. name[NAME_MAX - 1]='\0';
  705. return name;
  706. }
  707. int nfs_unlink(struct dfs_filesystem* fs, const char* path)
  708. {
  709. int ret = 0;
  710. struct nfs_filesystem* nfs;
  711. RT_ASSERT(fs != RT_NULL);
  712. RT_ASSERT(fs->data != RT_NULL);
  713. nfs = (struct nfs_filesystem *)fs->data;
  714. if (nfs_is_directory(nfs, path) == RT_FALSE)
  715. {
  716. /* remove file */
  717. REMOVE3args args;
  718. REMOVE3res res;
  719. nfs_fh3 *handle;
  720. handle = get_dir_handle(nfs, path);
  721. if(handle == RT_NULL) return -1;
  722. args.object.dir=*handle;
  723. args.object.name=strrchr(path, '/') + 1;
  724. if(args.object.name==RT_NULL)
  725. {
  726. args.object.name=(char *)path;
  727. }
  728. memset(&res, 0, sizeof(res));
  729. if(nfsproc3_remove_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  730. {
  731. rt_kprintf("Remove failed\n");
  732. ret=-1;
  733. }
  734. else if(res.status!=NFS3_OK)
  735. {
  736. rt_kprintf("Remove failed: %d\n", res.status);
  737. ret=-1;
  738. }
  739. xdr_free((xdrproc_t)xdr_REMOVE3res, (char *)&res);
  740. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  741. rt_free(handle);
  742. }
  743. else
  744. {
  745. /* remove directory */
  746. RMDIR3args args;
  747. RMDIR3res res;
  748. nfs_fh3 *handle;
  749. handle=get_dir_handle(nfs, path);
  750. if(handle==RT_NULL) return -1;
  751. args.object.dir=*handle;
  752. args.object.name=strrchr(path, '/') + 1;
  753. if(args.object.name==RT_NULL)
  754. {
  755. args.object.name=(char *)path;
  756. }
  757. memset(&res, 0, sizeof(res));
  758. if(nfsproc3_rmdir_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  759. {
  760. rt_kprintf("Rmdir failed\n");
  761. ret = -1;
  762. }
  763. else if(res.status!=NFS3_OK)
  764. {
  765. rt_kprintf("Rmdir failed: %d\n", res.status);
  766. ret = -1;
  767. }
  768. xdr_free((xdrproc_t)xdr_RMDIR3res, (char *)&res);
  769. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)handle);
  770. rt_free(handle);
  771. }
  772. return ret;
  773. }
  774. int nfs_rename(struct dfs_filesystem* fs, const char *src, const char *dest)
  775. {
  776. RENAME3args args;
  777. RENAME3res res;
  778. nfs_fh3 *sHandle;
  779. nfs_fh3 *dHandle;
  780. int ret=0;
  781. struct nfs_filesystem* nfs;
  782. RT_ASSERT(fs != RT_NULL);
  783. RT_ASSERT(fs->data != RT_NULL);
  784. nfs = (struct nfs_filesystem *)fs->data;
  785. if(nfs->nfs_client==RT_NULL)
  786. return -1;
  787. sHandle=get_dir_handle(nfs, src);
  788. if(sHandle==RT_NULL)
  789. return -1;
  790. dHandle=get_dir_handle(nfs, dest);
  791. if(dHandle==RT_NULL)
  792. return -1;
  793. args.from.dir=*sHandle;
  794. args.from.name=strrchr(src, '/') + 1;
  795. if(args.from.name==RT_NULL)
  796. args.from.name=(char *)src;
  797. args.to.dir=*dHandle;
  798. args.to.name=strrchr(src, '/') + 1;
  799. if(args.to.name==RT_NULL)
  800. args.to.name=(char *)dest;
  801. memset(&res, '\0', sizeof(res));
  802. if(nfsproc3_rename_3(args, &res, nfs->nfs_client)!=RPC_SUCCESS)
  803. {
  804. rt_kprintf("Rename failed\n");
  805. ret = -1;
  806. }
  807. else if(res.status!=NFS3_OK)
  808. {
  809. rt_kprintf("Rename failed: %d\n", res.status);
  810. ret = -1;
  811. }
  812. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)sHandle);
  813. xdr_free((xdrproc_t)xdr_nfs_fh3, (char *)dHandle);
  814. xdr_free((xdrproc_t)xdr_RENAME3res, (char *)&res);
  815. return ret;
  816. }
  817. int nfs_getdents(struct dfs_fd* file, struct _dirent* dirp, rt_uint32_t count)
  818. {
  819. nfs_dir *dir;
  820. rt_uint32_t index;
  821. struct _dirent* d;
  822. struct nfs_filesystem* nfs;
  823. char *name;
  824. dir = (nfs_dir *)(file->data);
  825. RT_ASSERT(dir != RT_NULL);
  826. RT_ASSERT(file->fs != RT_NULL);
  827. RT_ASSERT(file->fs->data != RT_NULL);
  828. nfs = (struct nfs_filesystem *)file->fs->data;
  829. /* make integer count */
  830. count = (count / sizeof(struct _dirent)) * sizeof(struct _dirent);
  831. if ( count == 0 ) return -DFS_STATUS_EINVAL;
  832. index = 0;
  833. while (1)
  834. {
  835. char *fn;
  836. d = dirp + index;
  837. name = nfs_readdir(nfs, dir);
  838. if (name == RT_NULL) break;
  839. d->d_type &= DFS_DT_REG;
  840. d->d_namlen = rt_strlen(name);
  841. d->d_reclen = (rt_uint16_t)sizeof(struct _dirent);
  842. rt_strncpy(d->d_name, name, rt_strlen(name) + 1);
  843. index ++;
  844. if ( index * sizeof(struct _dirent) >= count )
  845. break;
  846. }
  847. return index * sizeof(struct _dirent);
  848. }
  849. static const struct dfs_filesystem_operation _nfs =
  850. {
  851. "nfs",
  852. nfs_mount,
  853. nfs_unmount,
  854. RT_NULL, /* mkfs */
  855. RT_NULL, /* statfs */
  856. nfs_open,
  857. nfs_close,
  858. nfs_ioctl,
  859. nfs_read,
  860. nfs_write,
  861. RT_NULL, /* flush */
  862. nfs_lseek,
  863. nfs_getdents,
  864. nfs_unlink,
  865. nfs_stat,
  866. nfs_rename,
  867. };
  868. int nfs_init(void)
  869. {
  870. /* register fatfs file system */
  871. dfs_register(&_nfs);
  872. return RT_EOK;
  873. }