orangefs-utils.c 27 KB

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