orangefs-utils.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * See COPYING in top-level directory.
  5. */
  6. #include "protocol.h"
  7. #include "orangefs-kernel.h"
  8. #include "orangefs-dev-proto.h"
  9. #include "orangefs-bufmap.h"
  10. __s32 fsid_of_op(struct orangefs_kernel_op_s *op)
  11. {
  12. __s32 fsid = ORANGEFS_FS_ID_NULL;
  13. if (op) {
  14. switch (op->upcall.type) {
  15. case ORANGEFS_VFS_OP_FILE_IO:
  16. fsid = op->upcall.req.io.refn.fs_id;
  17. break;
  18. case ORANGEFS_VFS_OP_LOOKUP:
  19. fsid = op->upcall.req.lookup.parent_refn.fs_id;
  20. break;
  21. case ORANGEFS_VFS_OP_CREATE:
  22. fsid = op->upcall.req.create.parent_refn.fs_id;
  23. break;
  24. case ORANGEFS_VFS_OP_GETATTR:
  25. fsid = op->upcall.req.getattr.refn.fs_id;
  26. break;
  27. case ORANGEFS_VFS_OP_REMOVE:
  28. fsid = op->upcall.req.remove.parent_refn.fs_id;
  29. break;
  30. case ORANGEFS_VFS_OP_MKDIR:
  31. fsid = op->upcall.req.mkdir.parent_refn.fs_id;
  32. break;
  33. case ORANGEFS_VFS_OP_READDIR:
  34. fsid = op->upcall.req.readdir.refn.fs_id;
  35. break;
  36. case ORANGEFS_VFS_OP_SETATTR:
  37. fsid = op->upcall.req.setattr.refn.fs_id;
  38. break;
  39. case ORANGEFS_VFS_OP_SYMLINK:
  40. fsid = op->upcall.req.sym.parent_refn.fs_id;
  41. break;
  42. case ORANGEFS_VFS_OP_RENAME:
  43. fsid = op->upcall.req.rename.old_parent_refn.fs_id;
  44. break;
  45. case ORANGEFS_VFS_OP_STATFS:
  46. fsid = op->upcall.req.statfs.fs_id;
  47. break;
  48. case ORANGEFS_VFS_OP_TRUNCATE:
  49. fsid = op->upcall.req.truncate.refn.fs_id;
  50. break;
  51. case ORANGEFS_VFS_OP_RA_FLUSH:
  52. fsid = op->upcall.req.ra_cache_flush.refn.fs_id;
  53. break;
  54. case ORANGEFS_VFS_OP_FS_UMOUNT:
  55. fsid = op->upcall.req.fs_umount.fs_id;
  56. break;
  57. case ORANGEFS_VFS_OP_GETXATTR:
  58. fsid = op->upcall.req.getxattr.refn.fs_id;
  59. break;
  60. case ORANGEFS_VFS_OP_SETXATTR:
  61. fsid = op->upcall.req.setxattr.refn.fs_id;
  62. break;
  63. case ORANGEFS_VFS_OP_LISTXATTR:
  64. fsid = op->upcall.req.listxattr.refn.fs_id;
  65. break;
  66. case ORANGEFS_VFS_OP_REMOVEXATTR:
  67. fsid = op->upcall.req.removexattr.refn.fs_id;
  68. break;
  69. case ORANGEFS_VFS_OP_FSYNC:
  70. fsid = op->upcall.req.fsync.refn.fs_id;
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. return fsid;
  77. }
  78. static int orangefs_inode_flags(struct ORANGEFS_sys_attr_s *attrs)
  79. {
  80. int flags = 0;
  81. if (attrs->flags & ORANGEFS_IMMUTABLE_FL)
  82. flags |= S_IMMUTABLE;
  83. else
  84. flags &= ~S_IMMUTABLE;
  85. if (attrs->flags & ORANGEFS_APPEND_FL)
  86. flags |= S_APPEND;
  87. else
  88. flags &= ~S_APPEND;
  89. if (attrs->flags & ORANGEFS_NOATIME_FL)
  90. flags |= S_NOATIME;
  91. else
  92. flags &= ~S_NOATIME;
  93. return flags;
  94. }
  95. static int orangefs_inode_perms(struct ORANGEFS_sys_attr_s *attrs)
  96. {
  97. int perm_mode = 0;
  98. if (attrs->perms & ORANGEFS_O_EXECUTE)
  99. perm_mode |= S_IXOTH;
  100. if (attrs->perms & ORANGEFS_O_WRITE)
  101. perm_mode |= S_IWOTH;
  102. if (attrs->perms & ORANGEFS_O_READ)
  103. perm_mode |= S_IROTH;
  104. if (attrs->perms & ORANGEFS_G_EXECUTE)
  105. perm_mode |= S_IXGRP;
  106. if (attrs->perms & ORANGEFS_G_WRITE)
  107. perm_mode |= S_IWGRP;
  108. if (attrs->perms & ORANGEFS_G_READ)
  109. perm_mode |= S_IRGRP;
  110. if (attrs->perms & ORANGEFS_U_EXECUTE)
  111. perm_mode |= S_IXUSR;
  112. if (attrs->perms & ORANGEFS_U_WRITE)
  113. perm_mode |= S_IWUSR;
  114. if (attrs->perms & ORANGEFS_U_READ)
  115. perm_mode |= S_IRUSR;
  116. if (attrs->perms & ORANGEFS_G_SGID)
  117. perm_mode |= S_ISGID;
  118. if (attrs->perms & ORANGEFS_U_SUID)
  119. perm_mode |= S_ISUID;
  120. return perm_mode;
  121. }
  122. /*
  123. * NOTE: in kernel land, we never use the sys_attr->link_target for
  124. * anything, so don't bother copying it into the sys_attr object here.
  125. */
  126. static inline int copy_attributes_from_inode(struct inode *inode,
  127. struct ORANGEFS_sys_attr_s *attrs,
  128. struct iattr *iattr)
  129. {
  130. umode_t tmp_mode;
  131. if (!iattr || !inode || !attrs) {
  132. gossip_err("NULL iattr (%p), inode (%p), attrs (%p) "
  133. "in copy_attributes_from_inode!\n",
  134. iattr,
  135. inode,
  136. attrs);
  137. return -EINVAL;
  138. }
  139. /*
  140. * We need to be careful to only copy the attributes out of the
  141. * iattr object that we know are valid.
  142. */
  143. attrs->mask = 0;
  144. if (iattr->ia_valid & ATTR_UID) {
  145. attrs->owner = from_kuid(&init_user_ns, iattr->ia_uid);
  146. attrs->mask |= ORANGEFS_ATTR_SYS_UID;
  147. gossip_debug(GOSSIP_UTILS_DEBUG, "(UID) %d\n", attrs->owner);
  148. }
  149. if (iattr->ia_valid & ATTR_GID) {
  150. attrs->group = from_kgid(&init_user_ns, iattr->ia_gid);
  151. attrs->mask |= ORANGEFS_ATTR_SYS_GID;
  152. gossip_debug(GOSSIP_UTILS_DEBUG, "(GID) %d\n", attrs->group);
  153. }
  154. if (iattr->ia_valid & ATTR_ATIME) {
  155. attrs->mask |= ORANGEFS_ATTR_SYS_ATIME;
  156. if (iattr->ia_valid & ATTR_ATIME_SET) {
  157. attrs->atime = (time64_t)iattr->ia_atime.tv_sec;
  158. attrs->mask |= ORANGEFS_ATTR_SYS_ATIME_SET;
  159. }
  160. }
  161. if (iattr->ia_valid & ATTR_MTIME) {
  162. attrs->mask |= ORANGEFS_ATTR_SYS_MTIME;
  163. if (iattr->ia_valid & ATTR_MTIME_SET) {
  164. attrs->mtime = (time64_t)iattr->ia_mtime.tv_sec;
  165. attrs->mask |= ORANGEFS_ATTR_SYS_MTIME_SET;
  166. }
  167. }
  168. if (iattr->ia_valid & ATTR_CTIME)
  169. attrs->mask |= ORANGEFS_ATTR_SYS_CTIME;
  170. /*
  171. * ORANGEFS cannot set size with a setattr operation. Probably not likely
  172. * to be requested through the VFS, but just in case, don't worry about
  173. * ATTR_SIZE
  174. */
  175. if (iattr->ia_valid & ATTR_MODE) {
  176. tmp_mode = iattr->ia_mode;
  177. if (tmp_mode & (S_ISVTX)) {
  178. if (is_root_handle(inode)) {
  179. /*
  180. * allow sticky bit to be set on root (since
  181. * it shows up that way by default anyhow),
  182. * but don't show it to the server
  183. */
  184. tmp_mode -= S_ISVTX;
  185. } else {
  186. gossip_debug(GOSSIP_UTILS_DEBUG,
  187. "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
  188. return -EINVAL;
  189. }
  190. }
  191. if (tmp_mode & (S_ISUID)) {
  192. gossip_debug(GOSSIP_UTILS_DEBUG,
  193. "Attempting to set setuid bit (not supported); returning EINVAL.\n");
  194. return -EINVAL;
  195. }
  196. attrs->perms = ORANGEFS_util_translate_mode(tmp_mode);
  197. attrs->mask |= ORANGEFS_ATTR_SYS_PERM;
  198. }
  199. return 0;
  200. }
  201. static int orangefs_inode_type(enum orangefs_ds_type objtype)
  202. {
  203. if (objtype == ORANGEFS_TYPE_METAFILE)
  204. return S_IFREG;
  205. else if (objtype == ORANGEFS_TYPE_DIRECTORY)
  206. return S_IFDIR;
  207. else if (objtype == ORANGEFS_TYPE_SYMLINK)
  208. return S_IFLNK;
  209. else
  210. return -1;
  211. }
  212. static int orangefs_inode_is_stale(struct inode *inode, int new,
  213. struct ORANGEFS_sys_attr_s *attrs, char *link_target)
  214. {
  215. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  216. int type = orangefs_inode_type(attrs->objtype);
  217. if (!new) {
  218. /*
  219. * If the inode type or symlink target have changed then this
  220. * inode is stale.
  221. */
  222. if (type == -1 || !(inode->i_mode & type)) {
  223. orangefs_make_bad_inode(inode);
  224. return 1;
  225. }
  226. if (type == S_IFLNK && strncmp(orangefs_inode->link_target,
  227. link_target, ORANGEFS_NAME_MAX)) {
  228. orangefs_make_bad_inode(inode);
  229. return 1;
  230. }
  231. }
  232. return 0;
  233. }
  234. int orangefs_inode_getattr(struct inode *inode, int new, int bypass)
  235. {
  236. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  237. struct orangefs_kernel_op_s *new_op;
  238. loff_t inode_size, rounded_up_size;
  239. int ret, type;
  240. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
  241. get_khandle_from_ino(inode));
  242. if (!new && !bypass) {
  243. if (time_before(jiffies, orangefs_inode->getattr_time))
  244. return 0;
  245. }
  246. new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
  247. if (!new_op)
  248. return -ENOMEM;
  249. new_op->upcall.req.getattr.refn = orangefs_inode->refn;
  250. new_op->upcall.req.getattr.mask = ORANGEFS_ATTR_SYS_ALL_NOHINT;
  251. ret = service_operation(new_op, __func__,
  252. get_interruptible_flag(inode));
  253. if (ret != 0)
  254. goto out;
  255. type = orangefs_inode_type(new_op->
  256. downcall.resp.getattr.attributes.objtype);
  257. ret = orangefs_inode_is_stale(inode, new,
  258. &new_op->downcall.resp.getattr.attributes,
  259. new_op->downcall.resp.getattr.link_target);
  260. if (ret) {
  261. ret = -ESTALE;
  262. goto out;
  263. }
  264. switch (type) {
  265. case S_IFREG:
  266. inode->i_flags = orangefs_inode_flags(&new_op->
  267. downcall.resp.getattr.attributes);
  268. inode_size = (loff_t)new_op->
  269. downcall.resp.getattr.attributes.size;
  270. rounded_up_size =
  271. (inode_size + (4096 - (inode_size % 4096)));
  272. inode->i_size = inode_size;
  273. orangefs_inode->blksize =
  274. new_op->downcall.resp.getattr.attributes.blksize;
  275. spin_lock(&inode->i_lock);
  276. inode->i_bytes = inode_size;
  277. inode->i_blocks =
  278. (unsigned long)(rounded_up_size / 512);
  279. spin_unlock(&inode->i_lock);
  280. break;
  281. case S_IFDIR:
  282. inode->i_size = PAGE_SIZE;
  283. orangefs_inode->blksize = i_blocksize(inode);
  284. spin_lock(&inode->i_lock);
  285. inode_set_bytes(inode, inode->i_size);
  286. spin_unlock(&inode->i_lock);
  287. set_nlink(inode, 1);
  288. break;
  289. case S_IFLNK:
  290. if (new) {
  291. inode->i_size = (loff_t)strlen(new_op->
  292. downcall.resp.getattr.link_target);
  293. orangefs_inode->blksize = i_blocksize(inode);
  294. ret = strscpy(orangefs_inode->link_target,
  295. new_op->downcall.resp.getattr.link_target,
  296. ORANGEFS_NAME_MAX);
  297. if (ret == -E2BIG) {
  298. ret = -EIO;
  299. goto out;
  300. }
  301. inode->i_link = orangefs_inode->link_target;
  302. }
  303. break;
  304. }
  305. inode->i_uid = make_kuid(&init_user_ns, new_op->
  306. downcall.resp.getattr.attributes.owner);
  307. inode->i_gid = make_kgid(&init_user_ns, new_op->
  308. downcall.resp.getattr.attributes.group);
  309. inode->i_atime.tv_sec = (time64_t)new_op->
  310. downcall.resp.getattr.attributes.atime;
  311. inode->i_mtime.tv_sec = (time64_t)new_op->
  312. downcall.resp.getattr.attributes.mtime;
  313. inode->i_ctime.tv_sec = (time64_t)new_op->
  314. downcall.resp.getattr.attributes.ctime;
  315. inode->i_atime.tv_nsec = 0;
  316. inode->i_mtime.tv_nsec = 0;
  317. inode->i_ctime.tv_nsec = 0;
  318. /* special case: mark the root inode as sticky */
  319. inode->i_mode = type | (is_root_handle(inode) ? S_ISVTX : 0) |
  320. orangefs_inode_perms(&new_op->downcall.resp.getattr.attributes);
  321. orangefs_inode->getattr_time = jiffies +
  322. orangefs_getattr_timeout_msecs*HZ/1000;
  323. ret = 0;
  324. out:
  325. op_release(new_op);
  326. return ret;
  327. }
  328. int orangefs_inode_check_changed(struct inode *inode)
  329. {
  330. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  331. struct orangefs_kernel_op_s *new_op;
  332. int ret;
  333. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
  334. get_khandle_from_ino(inode));
  335. new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
  336. if (!new_op)
  337. return -ENOMEM;
  338. new_op->upcall.req.getattr.refn = orangefs_inode->refn;
  339. new_op->upcall.req.getattr.mask = ORANGEFS_ATTR_SYS_TYPE |
  340. ORANGEFS_ATTR_SYS_LNK_TARGET;
  341. ret = service_operation(new_op, __func__,
  342. get_interruptible_flag(inode));
  343. if (ret != 0)
  344. goto out;
  345. ret = orangefs_inode_is_stale(inode, 0,
  346. &new_op->downcall.resp.getattr.attributes,
  347. new_op->downcall.resp.getattr.link_target);
  348. out:
  349. op_release(new_op);
  350. return ret;
  351. }
  352. /*
  353. * issues a orangefs setattr request to make sure the new attribute values
  354. * take effect if successful. returns 0 on success; -errno otherwise
  355. */
  356. int orangefs_inode_setattr(struct inode *inode, struct iattr *iattr)
  357. {
  358. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  359. struct orangefs_kernel_op_s *new_op;
  360. int ret;
  361. new_op = op_alloc(ORANGEFS_VFS_OP_SETATTR);
  362. if (!new_op)
  363. return -ENOMEM;
  364. new_op->upcall.req.setattr.refn = orangefs_inode->refn;
  365. ret = copy_attributes_from_inode(inode,
  366. &new_op->upcall.req.setattr.attributes,
  367. iattr);
  368. if (ret >= 0) {
  369. ret = service_operation(new_op, __func__,
  370. get_interruptible_flag(inode));
  371. gossip_debug(GOSSIP_UTILS_DEBUG,
  372. "orangefs_inode_setattr: returning %d\n",
  373. ret);
  374. }
  375. op_release(new_op);
  376. /*
  377. * successful setattr should clear the atime, mtime and
  378. * ctime flags.
  379. */
  380. if (ret == 0) {
  381. ClearAtimeFlag(orangefs_inode);
  382. ClearMtimeFlag(orangefs_inode);
  383. ClearCtimeFlag(orangefs_inode);
  384. ClearModeFlag(orangefs_inode);
  385. orangefs_inode->getattr_time = jiffies - 1;
  386. }
  387. return ret;
  388. }
  389. int orangefs_flush_inode(struct inode *inode)
  390. {
  391. /*
  392. * If it is a dirty inode, this function gets called.
  393. * Gather all the information that needs to be setattr'ed
  394. * Right now, this will only be used for mode, atime, mtime
  395. * and/or ctime.
  396. */
  397. struct iattr wbattr;
  398. int ret;
  399. int mtime_flag;
  400. int ctime_flag;
  401. int atime_flag;
  402. int mode_flag;
  403. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  404. memset(&wbattr, 0, sizeof(wbattr));
  405. /*
  406. * check inode flags up front, and clear them if they are set. This
  407. * will prevent multiple processes from all trying to flush the same
  408. * inode if they call close() simultaneously
  409. */
  410. mtime_flag = MtimeFlag(orangefs_inode);
  411. ClearMtimeFlag(orangefs_inode);
  412. ctime_flag = CtimeFlag(orangefs_inode);
  413. ClearCtimeFlag(orangefs_inode);
  414. atime_flag = AtimeFlag(orangefs_inode);
  415. ClearAtimeFlag(orangefs_inode);
  416. mode_flag = ModeFlag(orangefs_inode);
  417. ClearModeFlag(orangefs_inode);
  418. /* -- Lazy atime,mtime and ctime update --
  419. * Note: all times are dictated by server in the new scheme
  420. * and not by the clients
  421. *
  422. * Also mode updates are being handled now..
  423. */
  424. if (mtime_flag)
  425. wbattr.ia_valid |= ATTR_MTIME;
  426. if (ctime_flag)
  427. wbattr.ia_valid |= ATTR_CTIME;
  428. if (atime_flag)
  429. wbattr.ia_valid |= ATTR_ATIME;
  430. if (mode_flag) {
  431. wbattr.ia_mode = inode->i_mode;
  432. wbattr.ia_valid |= ATTR_MODE;
  433. }
  434. gossip_debug(GOSSIP_UTILS_DEBUG,
  435. "*********** orangefs_flush_inode: %pU "
  436. "(ia_valid %d)\n",
  437. get_khandle_from_ino(inode),
  438. wbattr.ia_valid);
  439. if (wbattr.ia_valid == 0) {
  440. gossip_debug(GOSSIP_UTILS_DEBUG,
  441. "orangefs_flush_inode skipping setattr()\n");
  442. return 0;
  443. }
  444. gossip_debug(GOSSIP_UTILS_DEBUG,
  445. "orangefs_flush_inode (%pU) writing mode %o\n",
  446. get_khandle_from_ino(inode),
  447. inode->i_mode);
  448. ret = orangefs_inode_setattr(inode, &wbattr);
  449. return ret;
  450. }
  451. int orangefs_unmount_sb(struct super_block *sb)
  452. {
  453. int ret = -EINVAL;
  454. struct orangefs_kernel_op_s *new_op = NULL;
  455. gossip_debug(GOSSIP_UTILS_DEBUG,
  456. "orangefs_unmount_sb called on sb %p\n",
  457. sb);
  458. new_op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
  459. if (!new_op)
  460. return -ENOMEM;
  461. new_op->upcall.req.fs_umount.id = ORANGEFS_SB(sb)->id;
  462. new_op->upcall.req.fs_umount.fs_id = ORANGEFS_SB(sb)->fs_id;
  463. strncpy(new_op->upcall.req.fs_umount.orangefs_config_server,
  464. ORANGEFS_SB(sb)->devname,
  465. ORANGEFS_MAX_SERVER_ADDR_LEN);
  466. gossip_debug(GOSSIP_UTILS_DEBUG,
  467. "Attempting ORANGEFS Unmount via host %s\n",
  468. new_op->upcall.req.fs_umount.orangefs_config_server);
  469. ret = service_operation(new_op, "orangefs_fs_umount", 0);
  470. gossip_debug(GOSSIP_UTILS_DEBUG,
  471. "orangefs_unmount: got return value of %d\n", ret);
  472. if (ret)
  473. sb = ERR_PTR(ret);
  474. else
  475. ORANGEFS_SB(sb)->mount_pending = 1;
  476. op_release(new_op);
  477. return ret;
  478. }
  479. void orangefs_make_bad_inode(struct inode *inode)
  480. {
  481. if (is_root_handle(inode)) {
  482. /*
  483. * if this occurs, the pvfs2-client-core was killed but we
  484. * can't afford to lose the inode operations and such
  485. * associated with the root handle in any case.
  486. */
  487. gossip_debug(GOSSIP_UTILS_DEBUG,
  488. "*** NOT making bad root inode %pU\n",
  489. get_khandle_from_ino(inode));
  490. } else {
  491. gossip_debug(GOSSIP_UTILS_DEBUG,
  492. "*** making bad inode %pU\n",
  493. get_khandle_from_ino(inode));
  494. make_bad_inode(inode);
  495. }
  496. }
  497. /*
  498. * The following is a very dirty hack that is now a permanent part of the
  499. * ORANGEFS protocol. See protocol.h for more error definitions.
  500. */
  501. /* The order matches include/orangefs-types.h in the OrangeFS source. */
  502. static int PINT_errno_mapping[] = {
  503. 0, EPERM, ENOENT, EINTR, EIO, ENXIO, EBADF, EAGAIN, ENOMEM,
  504. EFAULT, EBUSY, EEXIST, ENODEV, ENOTDIR, EISDIR, EINVAL, EMFILE,
  505. EFBIG, ENOSPC, EROFS, EMLINK, EPIPE, EDEADLK, ENAMETOOLONG,
  506. ENOLCK, ENOSYS, ENOTEMPTY, ELOOP, EWOULDBLOCK, ENOMSG, EUNATCH,
  507. EBADR, EDEADLOCK, ENODATA, ETIME, ENONET, EREMOTE, ECOMM,
  508. EPROTO, EBADMSG, EOVERFLOW, ERESTART, EMSGSIZE, EPROTOTYPE,
  509. ENOPROTOOPT, EPROTONOSUPPORT, EOPNOTSUPP, EADDRINUSE,
  510. EADDRNOTAVAIL, ENETDOWN, ENETUNREACH, ENETRESET, ENOBUFS,
  511. ETIMEDOUT, ECONNREFUSED, EHOSTDOWN, EHOSTUNREACH, EALREADY,
  512. EACCES, ECONNRESET, ERANGE
  513. };
  514. int orangefs_normalize_to_errno(__s32 error_code)
  515. {
  516. __u32 i;
  517. /* Success */
  518. if (error_code == 0) {
  519. return 0;
  520. /*
  521. * This shouldn't ever happen. If it does it should be fixed on the
  522. * server.
  523. */
  524. } else if (error_code > 0) {
  525. gossip_err("orangefs: error status receieved.\n");
  526. gossip_err("orangefs: assuming error code is inverted.\n");
  527. error_code = -error_code;
  528. }
  529. /*
  530. * XXX: This is very bad since error codes from ORANGEFS may not be
  531. * suitable for return into userspace.
  532. */
  533. /*
  534. * Convert ORANGEFS error values into errno values suitable for return
  535. * from the kernel.
  536. */
  537. if ((-error_code) & ORANGEFS_NON_ERRNO_ERROR_BIT) {
  538. if (((-error_code) &
  539. (ORANGEFS_ERROR_NUMBER_BITS|ORANGEFS_NON_ERRNO_ERROR_BIT|
  540. ORANGEFS_ERROR_BIT)) == ORANGEFS_ECANCEL) {
  541. /*
  542. * cancellation error codes generally correspond to
  543. * a timeout from the client's perspective
  544. */
  545. error_code = -ETIMEDOUT;
  546. } else {
  547. /* assume a default error code */
  548. gossip_err("orangefs: warning: got error code without errno equivalent: %d.\n", error_code);
  549. error_code = -EINVAL;
  550. }
  551. /* Convert ORANGEFS encoded errno values into regular errno values. */
  552. } else if ((-error_code) & ORANGEFS_ERROR_BIT) {
  553. i = (-error_code) & ~(ORANGEFS_ERROR_BIT|ORANGEFS_ERROR_CLASS_BITS);
  554. if (i < sizeof(PINT_errno_mapping)/sizeof(*PINT_errno_mapping))
  555. error_code = -PINT_errno_mapping[i];
  556. else
  557. error_code = -EINVAL;
  558. /*
  559. * Only ORANGEFS protocol error codes should ever come here. Otherwise
  560. * there is a bug somewhere.
  561. */
  562. } else {
  563. gossip_err("orangefs: orangefs_normalize_to_errno: got error code which is not from ORANGEFS.\n");
  564. }
  565. return error_code;
  566. }
  567. #define NUM_MODES 11
  568. __s32 ORANGEFS_util_translate_mode(int mode)
  569. {
  570. int ret = 0;
  571. int i = 0;
  572. static int modes[NUM_MODES] = {
  573. S_IXOTH, S_IWOTH, S_IROTH,
  574. S_IXGRP, S_IWGRP, S_IRGRP,
  575. S_IXUSR, S_IWUSR, S_IRUSR,
  576. S_ISGID, S_ISUID
  577. };
  578. static int orangefs_modes[NUM_MODES] = {
  579. ORANGEFS_O_EXECUTE, ORANGEFS_O_WRITE, ORANGEFS_O_READ,
  580. ORANGEFS_G_EXECUTE, ORANGEFS_G_WRITE, ORANGEFS_G_READ,
  581. ORANGEFS_U_EXECUTE, ORANGEFS_U_WRITE, ORANGEFS_U_READ,
  582. ORANGEFS_G_SGID, ORANGEFS_U_SUID
  583. };
  584. for (i = 0; i < NUM_MODES; i++)
  585. if (mode & modes[i])
  586. ret |= orangefs_modes[i];
  587. return ret;
  588. }
  589. #undef NUM_MODES