orangefs-utils.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  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_MMAP_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(current_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(current_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. int orangefs_inode_getattr(struct inode *inode, int new, int size)
  213. {
  214. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  215. struct orangefs_kernel_op_s *new_op;
  216. loff_t inode_size, rounded_up_size;
  217. int ret;
  218. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
  219. get_khandle_from_ino(inode));
  220. new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
  221. if (!new_op)
  222. return -ENOMEM;
  223. new_op->upcall.req.getattr.refn = orangefs_inode->refn;
  224. new_op->upcall.req.getattr.mask = size ?
  225. ORANGEFS_ATTR_SYS_ALL_NOHINT : ORANGEFS_ATTR_SYS_ALL_NOHINT_NOSIZE;
  226. ret = service_operation(new_op, __func__,
  227. get_interruptible_flag(inode));
  228. if (ret != 0)
  229. goto out;
  230. ret = orangefs_inode_type(new_op->
  231. downcall.resp.getattr.attributes.objtype);
  232. if (!new) {
  233. /*
  234. * If the inode type or symlink target have changed then this
  235. * inode is stale.
  236. */
  237. if (ret == -1 || !(inode->i_mode & ret)) {
  238. orangefs_make_bad_inode(inode);
  239. ret = -ESTALE;
  240. goto out;
  241. }
  242. if (ret == S_IFLNK && strncmp(orangefs_inode->link_target,
  243. new_op->downcall.resp.getattr.link_target,
  244. ORANGEFS_NAME_MAX)) {
  245. orangefs_make_bad_inode(inode);
  246. ret = -ESTALE;
  247. goto out;
  248. }
  249. }
  250. switch (ret) {
  251. case S_IFREG:
  252. inode->i_flags = orangefs_inode_flags(&new_op->
  253. downcall.resp.getattr.attributes);
  254. if (size) {
  255. inode_size = (loff_t)new_op->
  256. downcall.resp.getattr.attributes.size;
  257. rounded_up_size =
  258. (inode_size + (4096 - (inode_size % 4096)));
  259. inode->i_size = inode_size;
  260. orangefs_inode->blksize =
  261. new_op->downcall.resp.getattr.attributes.blksize;
  262. spin_lock(&inode->i_lock);
  263. inode->i_bytes = inode_size;
  264. inode->i_blocks =
  265. (unsigned long)(rounded_up_size / 512);
  266. spin_unlock(&inode->i_lock);
  267. }
  268. break;
  269. case S_IFDIR:
  270. inode->i_size = PAGE_CACHE_SIZE;
  271. orangefs_inode->blksize = (1 << inode->i_blkbits);
  272. spin_lock(&inode->i_lock);
  273. inode_set_bytes(inode, inode->i_size);
  274. spin_unlock(&inode->i_lock);
  275. set_nlink(inode, 1);
  276. break;
  277. case S_IFLNK:
  278. if (new) {
  279. inode->i_size = (loff_t)strlen(new_op->
  280. downcall.resp.getattr.link_target);
  281. orangefs_inode->blksize = (1 << inode->i_blkbits);
  282. strlcpy(orangefs_inode->link_target,
  283. new_op->downcall.resp.getattr.link_target,
  284. ORANGEFS_NAME_MAX);
  285. }
  286. break;
  287. }
  288. inode->i_uid = make_kuid(&init_user_ns, new_op->
  289. downcall.resp.getattr.attributes.owner);
  290. inode->i_gid = make_kgid(&init_user_ns, new_op->
  291. downcall.resp.getattr.attributes.group);
  292. inode->i_atime.tv_sec = (time64_t)new_op->
  293. downcall.resp.getattr.attributes.atime;
  294. inode->i_mtime.tv_sec = (time64_t)new_op->
  295. downcall.resp.getattr.attributes.mtime;
  296. inode->i_ctime.tv_sec = (time64_t)new_op->
  297. downcall.resp.getattr.attributes.ctime;
  298. inode->i_atime.tv_nsec = 0;
  299. inode->i_mtime.tv_nsec = 0;
  300. inode->i_ctime.tv_nsec = 0;
  301. /* special case: mark the root inode as sticky */
  302. inode->i_mode = ret | (is_root_handle(inode) ? S_ISVTX : 0) |
  303. orangefs_inode_perms(&new_op->downcall.resp.getattr.attributes);
  304. ret = 0;
  305. out:
  306. op_release(new_op);
  307. return ret;
  308. }
  309. int orangefs_inode_check_changed(struct inode *inode)
  310. {
  311. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  312. struct orangefs_kernel_op_s *new_op;
  313. int ret;
  314. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: called on inode %pU\n", __func__,
  315. get_khandle_from_ino(inode));
  316. new_op = op_alloc(ORANGEFS_VFS_OP_GETATTR);
  317. if (!new_op)
  318. return -ENOMEM;
  319. new_op->upcall.req.getattr.refn = orangefs_inode->refn;
  320. new_op->upcall.req.getattr.mask = ORANGEFS_ATTR_SYS_TYPE |
  321. ORANGEFS_ATTR_SYS_LNK_TARGET;
  322. ret = service_operation(new_op, __func__,
  323. get_interruptible_flag(inode));
  324. if (ret != 0)
  325. goto out;
  326. ret = orangefs_inode_type(new_op->
  327. downcall.resp.getattr.attributes.objtype);
  328. /*
  329. * If the inode type or symlink target have changed then this
  330. * inode is stale.
  331. */
  332. if (ret == -1 || !(inode->i_mode & ret)) {
  333. orangefs_make_bad_inode(inode);
  334. ret = 1;
  335. goto out;
  336. }
  337. if (ret == S_IFLNK && strncmp(orangefs_inode->link_target,
  338. new_op->downcall.resp.getattr.link_target,
  339. ORANGEFS_NAME_MAX)) {
  340. orangefs_make_bad_inode(inode);
  341. ret = 1;
  342. goto out;
  343. }
  344. ret = 0;
  345. out:
  346. op_release(new_op);
  347. return ret;
  348. }
  349. /*
  350. * issues a orangefs setattr request to make sure the new attribute values
  351. * take effect if successful. returns 0 on success; -errno otherwise
  352. */
  353. int orangefs_inode_setattr(struct inode *inode, struct iattr *iattr)
  354. {
  355. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  356. struct orangefs_kernel_op_s *new_op;
  357. int ret;
  358. new_op = op_alloc(ORANGEFS_VFS_OP_SETATTR);
  359. if (!new_op)
  360. return -ENOMEM;
  361. new_op->upcall.req.setattr.refn = orangefs_inode->refn;
  362. ret = copy_attributes_from_inode(inode,
  363. &new_op->upcall.req.setattr.attributes,
  364. iattr);
  365. if (ret >= 0) {
  366. ret = service_operation(new_op, __func__,
  367. get_interruptible_flag(inode));
  368. gossip_debug(GOSSIP_UTILS_DEBUG,
  369. "orangefs_inode_setattr: returning %d\n",
  370. ret);
  371. }
  372. op_release(new_op);
  373. /*
  374. * successful setattr should clear the atime, mtime and
  375. * ctime flags.
  376. */
  377. if (ret == 0) {
  378. ClearAtimeFlag(orangefs_inode);
  379. ClearMtimeFlag(orangefs_inode);
  380. ClearCtimeFlag(orangefs_inode);
  381. ClearModeFlag(orangefs_inode);
  382. }
  383. return ret;
  384. }
  385. int orangefs_flush_inode(struct inode *inode)
  386. {
  387. /*
  388. * If it is a dirty inode, this function gets called.
  389. * Gather all the information that needs to be setattr'ed
  390. * Right now, this will only be used for mode, atime, mtime
  391. * and/or ctime.
  392. */
  393. struct iattr wbattr;
  394. int ret;
  395. int mtime_flag;
  396. int ctime_flag;
  397. int atime_flag;
  398. int mode_flag;
  399. struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
  400. memset(&wbattr, 0, sizeof(wbattr));
  401. /*
  402. * check inode flags up front, and clear them if they are set. This
  403. * will prevent multiple processes from all trying to flush the same
  404. * inode if they call close() simultaneously
  405. */
  406. mtime_flag = MtimeFlag(orangefs_inode);
  407. ClearMtimeFlag(orangefs_inode);
  408. ctime_flag = CtimeFlag(orangefs_inode);
  409. ClearCtimeFlag(orangefs_inode);
  410. atime_flag = AtimeFlag(orangefs_inode);
  411. ClearAtimeFlag(orangefs_inode);
  412. mode_flag = ModeFlag(orangefs_inode);
  413. ClearModeFlag(orangefs_inode);
  414. /* -- Lazy atime,mtime and ctime update --
  415. * Note: all times are dictated by server in the new scheme
  416. * and not by the clients
  417. *
  418. * Also mode updates are being handled now..
  419. */
  420. if (mtime_flag)
  421. wbattr.ia_valid |= ATTR_MTIME;
  422. if (ctime_flag)
  423. wbattr.ia_valid |= ATTR_CTIME;
  424. if (atime_flag)
  425. wbattr.ia_valid |= ATTR_ATIME;
  426. if (mode_flag) {
  427. wbattr.ia_mode = inode->i_mode;
  428. wbattr.ia_valid |= ATTR_MODE;
  429. }
  430. gossip_debug(GOSSIP_UTILS_DEBUG,
  431. "*********** orangefs_flush_inode: %pU "
  432. "(ia_valid %d)\n",
  433. get_khandle_from_ino(inode),
  434. wbattr.ia_valid);
  435. if (wbattr.ia_valid == 0) {
  436. gossip_debug(GOSSIP_UTILS_DEBUG,
  437. "orangefs_flush_inode skipping setattr()\n");
  438. return 0;
  439. }
  440. gossip_debug(GOSSIP_UTILS_DEBUG,
  441. "orangefs_flush_inode (%pU) writing mode %o\n",
  442. get_khandle_from_ino(inode),
  443. inode->i_mode);
  444. ret = orangefs_inode_setattr(inode, &wbattr);
  445. return ret;
  446. }
  447. int orangefs_unmount_sb(struct super_block *sb)
  448. {
  449. int ret = -EINVAL;
  450. struct orangefs_kernel_op_s *new_op = NULL;
  451. gossip_debug(GOSSIP_UTILS_DEBUG,
  452. "orangefs_unmount_sb called on sb %p\n",
  453. sb);
  454. new_op = op_alloc(ORANGEFS_VFS_OP_FS_UMOUNT);
  455. if (!new_op)
  456. return -ENOMEM;
  457. new_op->upcall.req.fs_umount.id = ORANGEFS_SB(sb)->id;
  458. new_op->upcall.req.fs_umount.fs_id = ORANGEFS_SB(sb)->fs_id;
  459. strncpy(new_op->upcall.req.fs_umount.orangefs_config_server,
  460. ORANGEFS_SB(sb)->devname,
  461. ORANGEFS_MAX_SERVER_ADDR_LEN);
  462. gossip_debug(GOSSIP_UTILS_DEBUG,
  463. "Attempting ORANGEFS Unmount via host %s\n",
  464. new_op->upcall.req.fs_umount.orangefs_config_server);
  465. ret = service_operation(new_op, "orangefs_fs_umount", 0);
  466. gossip_debug(GOSSIP_UTILS_DEBUG,
  467. "orangefs_unmount: got return value of %d\n", ret);
  468. if (ret)
  469. sb = ERR_PTR(ret);
  470. else
  471. ORANGEFS_SB(sb)->mount_pending = 1;
  472. op_release(new_op);
  473. return ret;
  474. }
  475. void orangefs_make_bad_inode(struct inode *inode)
  476. {
  477. if (is_root_handle(inode)) {
  478. /*
  479. * if this occurs, the pvfs2-client-core was killed but we
  480. * can't afford to lose the inode operations and such
  481. * associated with the root handle in any case.
  482. */
  483. gossip_debug(GOSSIP_UTILS_DEBUG,
  484. "*** NOT making bad root inode %pU\n",
  485. get_khandle_from_ino(inode));
  486. } else {
  487. gossip_debug(GOSSIP_UTILS_DEBUG,
  488. "*** making bad inode %pU\n",
  489. get_khandle_from_ino(inode));
  490. make_bad_inode(inode);
  491. }
  492. }
  493. /*
  494. * The following is a very dirty hack that is now a permanent part of the
  495. * ORANGEFS protocol. See protocol.h for more error definitions.
  496. */
  497. /* The order matches include/orangefs-types.h in the OrangeFS source. */
  498. static int PINT_errno_mapping[] = {
  499. 0, EPERM, ENOENT, EINTR, EIO, ENXIO, EBADF, EAGAIN, ENOMEM,
  500. EFAULT, EBUSY, EEXIST, ENODEV, ENOTDIR, EISDIR, EINVAL, EMFILE,
  501. EFBIG, ENOSPC, EROFS, EMLINK, EPIPE, EDEADLK, ENAMETOOLONG,
  502. ENOLCK, ENOSYS, ENOTEMPTY, ELOOP, EWOULDBLOCK, ENOMSG, EUNATCH,
  503. EBADR, EDEADLOCK, ENODATA, ETIME, ENONET, EREMOTE, ECOMM,
  504. EPROTO, EBADMSG, EOVERFLOW, ERESTART, EMSGSIZE, EPROTOTYPE,
  505. ENOPROTOOPT, EPROTONOSUPPORT, EOPNOTSUPP, EADDRINUSE,
  506. EADDRNOTAVAIL, ENETDOWN, ENETUNREACH, ENETRESET, ENOBUFS,
  507. ETIMEDOUT, ECONNREFUSED, EHOSTDOWN, EHOSTUNREACH, EALREADY,
  508. EACCES, ECONNRESET, ERANGE
  509. };
  510. int orangefs_normalize_to_errno(__s32 error_code)
  511. {
  512. __u32 i;
  513. /* Success */
  514. if (error_code == 0) {
  515. return 0;
  516. /*
  517. * This shouldn't ever happen. If it does it should be fixed on the
  518. * server.
  519. */
  520. } else if (error_code > 0) {
  521. gossip_err("orangefs: error status receieved.\n");
  522. gossip_err("orangefs: assuming error code is inverted.\n");
  523. error_code = -error_code;
  524. }
  525. /*
  526. * XXX: This is very bad since error codes from ORANGEFS may not be
  527. * suitable for return into userspace.
  528. */
  529. /*
  530. * Convert ORANGEFS error values into errno values suitable for return
  531. * from the kernel.
  532. */
  533. if ((-error_code) & ORANGEFS_NON_ERRNO_ERROR_BIT) {
  534. if (((-error_code) &
  535. (ORANGEFS_ERROR_NUMBER_BITS|ORANGEFS_NON_ERRNO_ERROR_BIT|
  536. ORANGEFS_ERROR_BIT)) == ORANGEFS_ECANCEL) {
  537. /*
  538. * cancellation error codes generally correspond to
  539. * a timeout from the client's perspective
  540. */
  541. error_code = -ETIMEDOUT;
  542. } else {
  543. /* assume a default error code */
  544. gossip_err("orangefs: warning: got error code without errno equivalent: %d.\n", error_code);
  545. error_code = -EINVAL;
  546. }
  547. /* Convert ORANGEFS encoded errno values into regular errno values. */
  548. } else if ((-error_code) & ORANGEFS_ERROR_BIT) {
  549. i = (-error_code) & ~(ORANGEFS_ERROR_BIT|ORANGEFS_ERROR_CLASS_BITS);
  550. if (i < sizeof(PINT_errno_mapping)/sizeof(*PINT_errno_mapping))
  551. error_code = -PINT_errno_mapping[i];
  552. else
  553. error_code = -EINVAL;
  554. /*
  555. * Only ORANGEFS protocol error codes should ever come here. Otherwise
  556. * there is a bug somewhere.
  557. */
  558. } else {
  559. gossip_err("orangefs: orangefs_normalize_to_errno: got error code which is not from ORANGEFS.\n");
  560. }
  561. return error_code;
  562. }
  563. #define NUM_MODES 11
  564. __s32 ORANGEFS_util_translate_mode(int mode)
  565. {
  566. int ret = 0;
  567. int i = 0;
  568. static int modes[NUM_MODES] = {
  569. S_IXOTH, S_IWOTH, S_IROTH,
  570. S_IXGRP, S_IWGRP, S_IRGRP,
  571. S_IXUSR, S_IWUSR, S_IRUSR,
  572. S_ISGID, S_ISUID
  573. };
  574. static int orangefs_modes[NUM_MODES] = {
  575. ORANGEFS_O_EXECUTE, ORANGEFS_O_WRITE, ORANGEFS_O_READ,
  576. ORANGEFS_G_EXECUTE, ORANGEFS_G_WRITE, ORANGEFS_G_READ,
  577. ORANGEFS_U_EXECUTE, ORANGEFS_U_WRITE, ORANGEFS_U_READ,
  578. ORANGEFS_G_SGID, ORANGEFS_U_SUID
  579. };
  580. for (i = 0; i < NUM_MODES; i++)
  581. if (mode & modes[i])
  582. ret |= orangefs_modes[i];
  583. return ret;
  584. }
  585. #undef NUM_MODES
  586. /*
  587. * After obtaining a string representation of the client's debug
  588. * keywords and their associated masks, this function is called to build an
  589. * array of these values.
  590. */
  591. int orangefs_prepare_cdm_array(char *debug_array_string)
  592. {
  593. int i;
  594. int rc = -EINVAL;
  595. char *cds_head = NULL;
  596. char *cds_delimiter = NULL;
  597. int keyword_len = 0;
  598. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  599. /*
  600. * figure out how many elements the cdm_array needs.
  601. */
  602. for (i = 0; i < strlen(debug_array_string); i++)
  603. if (debug_array_string[i] == '\n')
  604. cdm_element_count++;
  605. if (!cdm_element_count) {
  606. pr_info("No elements in client debug array string!\n");
  607. goto out;
  608. }
  609. cdm_array =
  610. kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
  611. GFP_KERNEL);
  612. if (!cdm_array) {
  613. pr_info("malloc failed for cdm_array!\n");
  614. rc = -ENOMEM;
  615. goto out;
  616. }
  617. cds_head = debug_array_string;
  618. for (i = 0; i < cdm_element_count; i++) {
  619. cds_delimiter = strchr(cds_head, '\n');
  620. *cds_delimiter = '\0';
  621. keyword_len = strcspn(cds_head, " ");
  622. cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
  623. if (!cdm_array[i].keyword) {
  624. rc = -ENOMEM;
  625. goto out;
  626. }
  627. sscanf(cds_head,
  628. "%s %llx %llx",
  629. cdm_array[i].keyword,
  630. (unsigned long long *)&(cdm_array[i].mask1),
  631. (unsigned long long *)&(cdm_array[i].mask2));
  632. if (!strcmp(cdm_array[i].keyword, ORANGEFS_VERBOSE))
  633. client_verbose_index = i;
  634. if (!strcmp(cdm_array[i].keyword, ORANGEFS_ALL))
  635. client_all_index = i;
  636. cds_head = cds_delimiter + 1;
  637. }
  638. rc = cdm_element_count;
  639. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  640. out:
  641. return rc;
  642. }
  643. /*
  644. * /sys/kernel/debug/orangefs/debug-help can be catted to
  645. * see all the available kernel and client debug keywords.
  646. *
  647. * When the kernel boots, we have no idea what keywords the
  648. * client supports, nor their associated masks.
  649. *
  650. * We pass through this function once at boot and stamp a
  651. * boilerplate "we don't know" message for the client in the
  652. * debug-help file. We pass through here again when the client
  653. * starts and then we can fill out the debug-help file fully.
  654. *
  655. * The client might be restarted any number of times between
  656. * reboots, we only build the debug-help file the first time.
  657. */
  658. int orangefs_prepare_debugfs_help_string(int at_boot)
  659. {
  660. int rc = -EINVAL;
  661. int i;
  662. int byte_count = 0;
  663. char *client_title = "Client Debug Keywords:\n";
  664. char *kernel_title = "Kernel Debug Keywords:\n";
  665. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  666. if (at_boot) {
  667. byte_count += strlen(HELP_STRING_UNINITIALIZED);
  668. client_title = HELP_STRING_UNINITIALIZED;
  669. } else {
  670. /*
  671. * fill the client keyword/mask array and remember
  672. * how many elements there were.
  673. */
  674. cdm_element_count =
  675. orangefs_prepare_cdm_array(client_debug_array_string);
  676. if (cdm_element_count <= 0)
  677. goto out;
  678. /* Count the bytes destined for debug_help_string. */
  679. byte_count += strlen(client_title);
  680. for (i = 0; i < cdm_element_count; i++) {
  681. byte_count += strlen(cdm_array[i].keyword + 2);
  682. if (byte_count >= DEBUG_HELP_STRING_SIZE) {
  683. pr_info("%s: overflow 1!\n", __func__);
  684. goto out;
  685. }
  686. }
  687. gossip_debug(GOSSIP_UTILS_DEBUG,
  688. "%s: cdm_element_count:%d:\n",
  689. __func__,
  690. cdm_element_count);
  691. }
  692. byte_count += strlen(kernel_title);
  693. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  694. byte_count +=
  695. strlen(s_kmod_keyword_mask_map[i].keyword + 2);
  696. if (byte_count >= DEBUG_HELP_STRING_SIZE) {
  697. pr_info("%s: overflow 2!\n", __func__);
  698. goto out;
  699. }
  700. }
  701. /* build debug_help_string. */
  702. debug_help_string = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
  703. if (!debug_help_string) {
  704. rc = -ENOMEM;
  705. goto out;
  706. }
  707. strcat(debug_help_string, client_title);
  708. if (!at_boot) {
  709. for (i = 0; i < cdm_element_count; i++) {
  710. strcat(debug_help_string, "\t");
  711. strcat(debug_help_string, cdm_array[i].keyword);
  712. strcat(debug_help_string, "\n");
  713. }
  714. }
  715. strcat(debug_help_string, "\n");
  716. strcat(debug_help_string, kernel_title);
  717. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  718. strcat(debug_help_string, "\t");
  719. strcat(debug_help_string, s_kmod_keyword_mask_map[i].keyword);
  720. strcat(debug_help_string, "\n");
  721. }
  722. rc = 0;
  723. out:
  724. return rc;
  725. }
  726. /*
  727. * kernel = type 0
  728. * client = type 1
  729. */
  730. void debug_mask_to_string(void *mask, int type)
  731. {
  732. int i;
  733. int len = 0;
  734. char *debug_string;
  735. int element_count = 0;
  736. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  737. if (type) {
  738. debug_string = client_debug_string;
  739. element_count = cdm_element_count;
  740. } else {
  741. debug_string = kernel_debug_string;
  742. element_count = num_kmod_keyword_mask_map;
  743. }
  744. memset(debug_string, 0, ORANGEFS_MAX_DEBUG_STRING_LEN);
  745. /*
  746. * Some keywords, like "all" or "verbose", are amalgams of
  747. * numerous other keywords. Make a special check for those
  748. * before grinding through the whole mask only to find out
  749. * later...
  750. */
  751. if (check_amalgam_keyword(mask, type))
  752. goto out;
  753. /* Build the debug string. */
  754. for (i = 0; i < element_count; i++)
  755. if (type)
  756. do_c_string(mask, i);
  757. else
  758. do_k_string(mask, i);
  759. len = strlen(debug_string);
  760. if ((len) && (type))
  761. client_debug_string[len - 1] = '\0';
  762. else if (len)
  763. kernel_debug_string[len - 1] = '\0';
  764. else if (type)
  765. strcpy(client_debug_string, "none");
  766. else
  767. strcpy(kernel_debug_string, "none");
  768. out:
  769. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
  770. return;
  771. }
  772. void do_k_string(void *k_mask, int index)
  773. {
  774. __u64 *mask = (__u64 *) k_mask;
  775. if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
  776. goto out;
  777. if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
  778. if ((strlen(kernel_debug_string) +
  779. strlen(s_kmod_keyword_mask_map[index].keyword))
  780. < ORANGEFS_MAX_DEBUG_STRING_LEN - 1) {
  781. strcat(kernel_debug_string,
  782. s_kmod_keyword_mask_map[index].keyword);
  783. strcat(kernel_debug_string, ",");
  784. } else {
  785. gossip_err("%s: overflow!\n", __func__);
  786. strcpy(kernel_debug_string, ORANGEFS_ALL);
  787. goto out;
  788. }
  789. }
  790. out:
  791. return;
  792. }
  793. void do_c_string(void *c_mask, int index)
  794. {
  795. struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
  796. if (keyword_is_amalgam(cdm_array[index].keyword))
  797. goto out;
  798. if ((mask->mask1 & cdm_array[index].mask1) ||
  799. (mask->mask2 & cdm_array[index].mask2)) {
  800. if ((strlen(client_debug_string) +
  801. strlen(cdm_array[index].keyword) + 1)
  802. < ORANGEFS_MAX_DEBUG_STRING_LEN - 2) {
  803. strcat(client_debug_string,
  804. cdm_array[index].keyword);
  805. strcat(client_debug_string, ",");
  806. } else {
  807. gossip_err("%s: overflow!\n", __func__);
  808. strcpy(client_debug_string, ORANGEFS_ALL);
  809. goto out;
  810. }
  811. }
  812. out:
  813. return;
  814. }
  815. int keyword_is_amalgam(char *keyword)
  816. {
  817. int rc = 0;
  818. if ((!strcmp(keyword, ORANGEFS_ALL)) || (!strcmp(keyword, ORANGEFS_VERBOSE)))
  819. rc = 1;
  820. return rc;
  821. }
  822. /*
  823. * kernel = type 0
  824. * client = type 1
  825. *
  826. * return 1 if we found an amalgam.
  827. */
  828. int check_amalgam_keyword(void *mask, int type)
  829. {
  830. __u64 *k_mask;
  831. struct client_debug_mask *c_mask;
  832. int k_all_index = num_kmod_keyword_mask_map - 1;
  833. int rc = 0;
  834. if (type) {
  835. c_mask = (struct client_debug_mask *) mask;
  836. if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
  837. (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
  838. strcpy(client_debug_string, ORANGEFS_ALL);
  839. rc = 1;
  840. goto out;
  841. }
  842. if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
  843. (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
  844. strcpy(client_debug_string, ORANGEFS_VERBOSE);
  845. rc = 1;
  846. goto out;
  847. }
  848. } else {
  849. k_mask = (__u64 *) mask;
  850. if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
  851. strcpy(kernel_debug_string, ORANGEFS_ALL);
  852. rc = 1;
  853. goto out;
  854. }
  855. }
  856. out:
  857. return rc;
  858. }
  859. /*
  860. * kernel = type 0
  861. * client = type 1
  862. */
  863. void debug_string_to_mask(char *debug_string, void *mask, int type)
  864. {
  865. char *unchecked_keyword;
  866. int i;
  867. char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
  868. char *original_pointer;
  869. int element_count = 0;
  870. struct client_debug_mask *c_mask;
  871. __u64 *k_mask;
  872. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  873. if (type) {
  874. c_mask = (struct client_debug_mask *)mask;
  875. element_count = cdm_element_count;
  876. } else {
  877. k_mask = (__u64 *)mask;
  878. *k_mask = 0;
  879. element_count = num_kmod_keyword_mask_map;
  880. }
  881. original_pointer = strsep_fodder;
  882. while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
  883. if (strlen(unchecked_keyword)) {
  884. for (i = 0; i < element_count; i++)
  885. if (type)
  886. do_c_mask(i,
  887. unchecked_keyword,
  888. &c_mask);
  889. else
  890. do_k_mask(i,
  891. unchecked_keyword,
  892. &k_mask);
  893. }
  894. kfree(original_pointer);
  895. }
  896. void do_c_mask(int i,
  897. char *unchecked_keyword,
  898. struct client_debug_mask **sane_mask)
  899. {
  900. if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
  901. (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
  902. (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
  903. }
  904. }
  905. void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
  906. {
  907. if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
  908. **sane_mask = (**sane_mask) |
  909. s_kmod_keyword_mask_map[i].mask_val;
  910. }