pvfs2-utils.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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 "pvfs2-kernel.h"
  8. #include "pvfs2-dev-proto.h"
  9. #include "pvfs2-bufmap.h"
  10. __s32 fsid_of_op(struct pvfs2_kernel_op_s *op)
  11. {
  12. __s32 fsid = PVFS_FS_ID_NULL;
  13. if (op) {
  14. switch (op->upcall.type) {
  15. case PVFS2_VFS_OP_FILE_IO:
  16. fsid = op->upcall.req.io.refn.fs_id;
  17. break;
  18. case PVFS2_VFS_OP_LOOKUP:
  19. fsid = op->upcall.req.lookup.parent_refn.fs_id;
  20. break;
  21. case PVFS2_VFS_OP_CREATE:
  22. fsid = op->upcall.req.create.parent_refn.fs_id;
  23. break;
  24. case PVFS2_VFS_OP_GETATTR:
  25. fsid = op->upcall.req.getattr.refn.fs_id;
  26. break;
  27. case PVFS2_VFS_OP_REMOVE:
  28. fsid = op->upcall.req.remove.parent_refn.fs_id;
  29. break;
  30. case PVFS2_VFS_OP_MKDIR:
  31. fsid = op->upcall.req.mkdir.parent_refn.fs_id;
  32. break;
  33. case PVFS2_VFS_OP_READDIR:
  34. fsid = op->upcall.req.readdir.refn.fs_id;
  35. break;
  36. case PVFS2_VFS_OP_SETATTR:
  37. fsid = op->upcall.req.setattr.refn.fs_id;
  38. break;
  39. case PVFS2_VFS_OP_SYMLINK:
  40. fsid = op->upcall.req.sym.parent_refn.fs_id;
  41. break;
  42. case PVFS2_VFS_OP_RENAME:
  43. fsid = op->upcall.req.rename.old_parent_refn.fs_id;
  44. break;
  45. case PVFS2_VFS_OP_STATFS:
  46. fsid = op->upcall.req.statfs.fs_id;
  47. break;
  48. case PVFS2_VFS_OP_TRUNCATE:
  49. fsid = op->upcall.req.truncate.refn.fs_id;
  50. break;
  51. case PVFS2_VFS_OP_MMAP_RA_FLUSH:
  52. fsid = op->upcall.req.ra_cache_flush.refn.fs_id;
  53. break;
  54. case PVFS2_VFS_OP_FS_UMOUNT:
  55. fsid = op->upcall.req.fs_umount.fs_id;
  56. break;
  57. case PVFS2_VFS_OP_GETXATTR:
  58. fsid = op->upcall.req.getxattr.refn.fs_id;
  59. break;
  60. case PVFS2_VFS_OP_SETXATTR:
  61. fsid = op->upcall.req.setxattr.refn.fs_id;
  62. break;
  63. case PVFS2_VFS_OP_LISTXATTR:
  64. fsid = op->upcall.req.listxattr.refn.fs_id;
  65. break;
  66. case PVFS2_VFS_OP_REMOVEXATTR:
  67. fsid = op->upcall.req.removexattr.refn.fs_id;
  68. break;
  69. case PVFS2_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 void pvfs2_set_inode_flags(struct inode *inode,
  79. struct PVFS_sys_attr_s *attrs)
  80. {
  81. if (attrs->flags & PVFS_IMMUTABLE_FL)
  82. inode->i_flags |= S_IMMUTABLE;
  83. else
  84. inode->i_flags &= ~S_IMMUTABLE;
  85. if (attrs->flags & PVFS_APPEND_FL)
  86. inode->i_flags |= S_APPEND;
  87. else
  88. inode->i_flags &= ~S_APPEND;
  89. if (attrs->flags & PVFS_NOATIME_FL)
  90. inode->i_flags |= S_NOATIME;
  91. else
  92. inode->i_flags &= ~S_NOATIME;
  93. }
  94. /* NOTE: symname is ignored unless the inode is a sym link */
  95. static int copy_attributes_to_inode(struct inode *inode,
  96. struct PVFS_sys_attr_s *attrs,
  97. char *symname)
  98. {
  99. int ret = -1;
  100. int perm_mode = 0;
  101. struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
  102. loff_t inode_size = 0;
  103. loff_t rounded_up_size = 0;
  104. /*
  105. * arbitrarily set the inode block size; FIXME: we need to
  106. * resolve the difference between the reported inode blocksize
  107. * and the PAGE_CACHE_SIZE, since our block count will always
  108. * be wrong.
  109. *
  110. * For now, we're setting the block count to be the proper
  111. * number assuming the block size is 512 bytes, and the size is
  112. * rounded up to the nearest 4K. This is apparently required
  113. * to get proper size reports from the 'du' shell utility.
  114. *
  115. * changing the inode->i_blkbits to something other than
  116. * PAGE_CACHE_SHIFT breaks mmap/execution as we depend on that.
  117. */
  118. gossip_debug(GOSSIP_UTILS_DEBUG,
  119. "attrs->mask = %x (objtype = %s)\n",
  120. attrs->mask,
  121. attrs->objtype == PVFS_TYPE_METAFILE ? "file" :
  122. attrs->objtype == PVFS_TYPE_DIRECTORY ? "directory" :
  123. attrs->objtype == PVFS_TYPE_SYMLINK ? "symlink" :
  124. "invalid/unknown");
  125. switch (attrs->objtype) {
  126. case PVFS_TYPE_METAFILE:
  127. pvfs2_set_inode_flags(inode, attrs);
  128. if (attrs->mask & PVFS_ATTR_SYS_SIZE) {
  129. inode_size = (loff_t) attrs->size;
  130. rounded_up_size =
  131. (inode_size + (4096 - (inode_size % 4096)));
  132. pvfs2_lock_inode(inode);
  133. inode->i_bytes = inode_size;
  134. inode->i_blocks =
  135. (unsigned long)(rounded_up_size / 512);
  136. pvfs2_unlock_inode(inode);
  137. /*
  138. * NOTE: make sure all the places we're called
  139. * from have the inode->i_sem lock. We're fine
  140. * in 99% of the cases since we're mostly
  141. * called from a lookup.
  142. */
  143. inode->i_size = inode_size;
  144. }
  145. break;
  146. case PVFS_TYPE_SYMLINK:
  147. if (symname != NULL) {
  148. inode->i_size = (loff_t) strlen(symname);
  149. break;
  150. }
  151. /*FALLTHRU*/
  152. default:
  153. inode->i_size = PAGE_CACHE_SIZE;
  154. pvfs2_lock_inode(inode);
  155. inode_set_bytes(inode, inode->i_size);
  156. pvfs2_unlock_inode(inode);
  157. break;
  158. }
  159. inode->i_uid = make_kuid(&init_user_ns, attrs->owner);
  160. inode->i_gid = make_kgid(&init_user_ns, attrs->group);
  161. inode->i_atime.tv_sec = (time_t) attrs->atime;
  162. inode->i_mtime.tv_sec = (time_t) attrs->mtime;
  163. inode->i_ctime.tv_sec = (time_t) attrs->ctime;
  164. inode->i_atime.tv_nsec = 0;
  165. inode->i_mtime.tv_nsec = 0;
  166. inode->i_ctime.tv_nsec = 0;
  167. if (attrs->perms & PVFS_O_EXECUTE)
  168. perm_mode |= S_IXOTH;
  169. if (attrs->perms & PVFS_O_WRITE)
  170. perm_mode |= S_IWOTH;
  171. if (attrs->perms & PVFS_O_READ)
  172. perm_mode |= S_IROTH;
  173. if (attrs->perms & PVFS_G_EXECUTE)
  174. perm_mode |= S_IXGRP;
  175. if (attrs->perms & PVFS_G_WRITE)
  176. perm_mode |= S_IWGRP;
  177. if (attrs->perms & PVFS_G_READ)
  178. perm_mode |= S_IRGRP;
  179. if (attrs->perms & PVFS_U_EXECUTE)
  180. perm_mode |= S_IXUSR;
  181. if (attrs->perms & PVFS_U_WRITE)
  182. perm_mode |= S_IWUSR;
  183. if (attrs->perms & PVFS_U_READ)
  184. perm_mode |= S_IRUSR;
  185. if (attrs->perms & PVFS_G_SGID)
  186. perm_mode |= S_ISGID;
  187. if (attrs->perms & PVFS_U_SUID)
  188. perm_mode |= S_ISUID;
  189. inode->i_mode = perm_mode;
  190. if (is_root_handle(inode)) {
  191. /* special case: mark the root inode as sticky */
  192. inode->i_mode |= S_ISVTX;
  193. gossip_debug(GOSSIP_UTILS_DEBUG,
  194. "Marking inode %pU as sticky\n",
  195. get_khandle_from_ino(inode));
  196. }
  197. switch (attrs->objtype) {
  198. case PVFS_TYPE_METAFILE:
  199. inode->i_mode |= S_IFREG;
  200. ret = 0;
  201. break;
  202. case PVFS_TYPE_DIRECTORY:
  203. inode->i_mode |= S_IFDIR;
  204. /* NOTE: we have no good way to keep nlink consistent
  205. * for directories across clients; keep constant at 1.
  206. * Why 1? If we go with 2, then find(1) gets confused
  207. * and won't work properly withouth the -noleaf option
  208. */
  209. set_nlink(inode, 1);
  210. ret = 0;
  211. break;
  212. case PVFS_TYPE_SYMLINK:
  213. inode->i_mode |= S_IFLNK;
  214. /* copy link target to inode private data */
  215. if (pvfs2_inode && symname) {
  216. strncpy(pvfs2_inode->link_target,
  217. symname,
  218. PVFS_NAME_MAX);
  219. gossip_debug(GOSSIP_UTILS_DEBUG,
  220. "Copied attr link target %s\n",
  221. pvfs2_inode->link_target);
  222. }
  223. gossip_debug(GOSSIP_UTILS_DEBUG,
  224. "symlink mode %o\n",
  225. inode->i_mode);
  226. ret = 0;
  227. break;
  228. default:
  229. gossip_err("pvfs2: copy_attributes_to_inode: got invalid attribute type %x\n",
  230. attrs->objtype);
  231. }
  232. gossip_debug(GOSSIP_UTILS_DEBUG,
  233. "pvfs2: copy_attributes_to_inode: setting i_mode to %o, i_size to %lu\n",
  234. inode->i_mode,
  235. (unsigned long)i_size_read(inode));
  236. return ret;
  237. }
  238. /*
  239. * NOTE: in kernel land, we never use the sys_attr->link_target for
  240. * anything, so don't bother copying it into the sys_attr object here.
  241. */
  242. static inline int copy_attributes_from_inode(struct inode *inode,
  243. struct PVFS_sys_attr_s *attrs,
  244. struct iattr *iattr)
  245. {
  246. umode_t tmp_mode;
  247. if (!iattr || !inode || !attrs) {
  248. gossip_err("NULL iattr (%p), inode (%p), attrs (%p) "
  249. "in copy_attributes_from_inode!\n",
  250. iattr,
  251. inode,
  252. attrs);
  253. return -EINVAL;
  254. }
  255. /*
  256. * We need to be careful to only copy the attributes out of the
  257. * iattr object that we know are valid.
  258. */
  259. attrs->mask = 0;
  260. if (iattr->ia_valid & ATTR_UID) {
  261. attrs->owner = from_kuid(current_user_ns(), iattr->ia_uid);
  262. attrs->mask |= PVFS_ATTR_SYS_UID;
  263. gossip_debug(GOSSIP_UTILS_DEBUG, "(UID) %d\n", attrs->owner);
  264. }
  265. if (iattr->ia_valid & ATTR_GID) {
  266. attrs->group = from_kgid(current_user_ns(), iattr->ia_gid);
  267. attrs->mask |= PVFS_ATTR_SYS_GID;
  268. gossip_debug(GOSSIP_UTILS_DEBUG, "(GID) %d\n", attrs->group);
  269. }
  270. if (iattr->ia_valid & ATTR_ATIME) {
  271. attrs->mask |= PVFS_ATTR_SYS_ATIME;
  272. if (iattr->ia_valid & ATTR_ATIME_SET) {
  273. attrs->atime =
  274. pvfs2_convert_time_field((void *)&iattr->ia_atime);
  275. attrs->mask |= PVFS_ATTR_SYS_ATIME_SET;
  276. }
  277. }
  278. if (iattr->ia_valid & ATTR_MTIME) {
  279. attrs->mask |= PVFS_ATTR_SYS_MTIME;
  280. if (iattr->ia_valid & ATTR_MTIME_SET) {
  281. attrs->mtime =
  282. pvfs2_convert_time_field((void *)&iattr->ia_mtime);
  283. attrs->mask |= PVFS_ATTR_SYS_MTIME_SET;
  284. }
  285. }
  286. if (iattr->ia_valid & ATTR_CTIME)
  287. attrs->mask |= PVFS_ATTR_SYS_CTIME;
  288. /*
  289. * PVFS2 cannot set size with a setattr operation. Probably not likely
  290. * to be requested through the VFS, but just in case, don't worry about
  291. * ATTR_SIZE
  292. */
  293. if (iattr->ia_valid & ATTR_MODE) {
  294. tmp_mode = iattr->ia_mode;
  295. if (tmp_mode & (S_ISVTX)) {
  296. if (is_root_handle(inode)) {
  297. /*
  298. * allow sticky bit to be set on root (since
  299. * it shows up that way by default anyhow),
  300. * but don't show it to the server
  301. */
  302. tmp_mode -= S_ISVTX;
  303. } else {
  304. gossip_debug(GOSSIP_UTILS_DEBUG,
  305. "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
  306. return -EINVAL;
  307. }
  308. }
  309. if (tmp_mode & (S_ISUID)) {
  310. gossip_debug(GOSSIP_UTILS_DEBUG,
  311. "Attempting to set setuid bit (not supported); returning EINVAL.\n");
  312. return -EINVAL;
  313. }
  314. attrs->perms = PVFS_util_translate_mode(tmp_mode);
  315. attrs->mask |= PVFS_ATTR_SYS_PERM;
  316. }
  317. return 0;
  318. }
  319. /*
  320. * issues a pvfs2 getattr request and fills in the appropriate inode
  321. * attributes if successful. returns 0 on success; -errno otherwise
  322. */
  323. int pvfs2_inode_getattr(struct inode *inode, __u32 getattr_mask)
  324. {
  325. struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
  326. struct pvfs2_kernel_op_s *new_op;
  327. int ret = -EINVAL;
  328. gossip_debug(GOSSIP_UTILS_DEBUG,
  329. "%s: called on inode %pU\n",
  330. __func__,
  331. get_khandle_from_ino(inode));
  332. new_op = op_alloc(PVFS2_VFS_OP_GETATTR);
  333. if (!new_op)
  334. return -ENOMEM;
  335. new_op->upcall.req.getattr.refn = pvfs2_inode->refn;
  336. new_op->upcall.req.getattr.mask = getattr_mask;
  337. ret = service_operation(new_op, __func__,
  338. get_interruptible_flag(inode));
  339. if (ret != 0)
  340. goto out;
  341. if (copy_attributes_to_inode(inode,
  342. &new_op->downcall.resp.getattr.attributes,
  343. new_op->downcall.resp.getattr.link_target)) {
  344. gossip_err("%s: failed to copy attributes\n", __func__);
  345. ret = -ENOENT;
  346. goto out;
  347. }
  348. /*
  349. * Store blksize in pvfs2 specific part of inode structure; we are
  350. * only going to use this to report to stat to make sure it doesn't
  351. * perturb any inode related code paths.
  352. */
  353. if (new_op->downcall.resp.getattr.attributes.objtype ==
  354. PVFS_TYPE_METAFILE) {
  355. pvfs2_inode->blksize =
  356. new_op->downcall.resp.getattr.attributes.blksize;
  357. } else {
  358. /* mimic behavior of generic_fillattr() for other types. */
  359. pvfs2_inode->blksize = (1 << inode->i_blkbits);
  360. }
  361. out:
  362. gossip_debug(GOSSIP_UTILS_DEBUG,
  363. "Getattr on handle %pU, "
  364. "fsid %d\n (inode ct = %d) returned %d\n",
  365. &pvfs2_inode->refn.khandle,
  366. pvfs2_inode->refn.fs_id,
  367. (int)atomic_read(&inode->i_count),
  368. ret);
  369. op_release(new_op);
  370. return ret;
  371. }
  372. /*
  373. * issues a pvfs2 setattr request to make sure the new attribute values
  374. * take effect if successful. returns 0 on success; -errno otherwise
  375. */
  376. int pvfs2_inode_setattr(struct inode *inode, struct iattr *iattr)
  377. {
  378. struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
  379. struct pvfs2_kernel_op_s *new_op;
  380. int ret;
  381. new_op = op_alloc(PVFS2_VFS_OP_SETATTR);
  382. if (!new_op)
  383. return -ENOMEM;
  384. new_op->upcall.req.setattr.refn = pvfs2_inode->refn;
  385. ret = copy_attributes_from_inode(inode,
  386. &new_op->upcall.req.setattr.attributes,
  387. iattr);
  388. if (ret < 0) {
  389. op_release(new_op);
  390. return ret;
  391. }
  392. ret = service_operation(new_op, __func__,
  393. get_interruptible_flag(inode));
  394. gossip_debug(GOSSIP_UTILS_DEBUG,
  395. "pvfs2_inode_setattr: returning %d\n",
  396. ret);
  397. /* when request is serviced properly, free req op struct */
  398. op_release(new_op);
  399. /*
  400. * successful setattr should clear the atime, mtime and
  401. * ctime flags.
  402. */
  403. if (ret == 0) {
  404. ClearAtimeFlag(pvfs2_inode);
  405. ClearMtimeFlag(pvfs2_inode);
  406. ClearCtimeFlag(pvfs2_inode);
  407. ClearModeFlag(pvfs2_inode);
  408. }
  409. return ret;
  410. }
  411. int pvfs2_flush_inode(struct inode *inode)
  412. {
  413. /*
  414. * If it is a dirty inode, this function gets called.
  415. * Gather all the information that needs to be setattr'ed
  416. * Right now, this will only be used for mode, atime, mtime
  417. * and/or ctime.
  418. */
  419. struct iattr wbattr;
  420. int ret;
  421. int mtime_flag;
  422. int ctime_flag;
  423. int atime_flag;
  424. int mode_flag;
  425. struct pvfs2_inode_s *pvfs2_inode = PVFS2_I(inode);
  426. memset(&wbattr, 0, sizeof(wbattr));
  427. /*
  428. * check inode flags up front, and clear them if they are set. This
  429. * will prevent multiple processes from all trying to flush the same
  430. * inode if they call close() simultaneously
  431. */
  432. mtime_flag = MtimeFlag(pvfs2_inode);
  433. ClearMtimeFlag(pvfs2_inode);
  434. ctime_flag = CtimeFlag(pvfs2_inode);
  435. ClearCtimeFlag(pvfs2_inode);
  436. atime_flag = AtimeFlag(pvfs2_inode);
  437. ClearAtimeFlag(pvfs2_inode);
  438. mode_flag = ModeFlag(pvfs2_inode);
  439. ClearModeFlag(pvfs2_inode);
  440. /* -- Lazy atime,mtime and ctime update --
  441. * Note: all times are dictated by server in the new scheme
  442. * and not by the clients
  443. *
  444. * Also mode updates are being handled now..
  445. */
  446. if (mtime_flag)
  447. wbattr.ia_valid |= ATTR_MTIME;
  448. if (ctime_flag)
  449. wbattr.ia_valid |= ATTR_CTIME;
  450. if (atime_flag)
  451. wbattr.ia_valid |= ATTR_ATIME;
  452. if (mode_flag) {
  453. wbattr.ia_mode = inode->i_mode;
  454. wbattr.ia_valid |= ATTR_MODE;
  455. }
  456. gossip_debug(GOSSIP_UTILS_DEBUG,
  457. "*********** pvfs2_flush_inode: %pU "
  458. "(ia_valid %d)\n",
  459. get_khandle_from_ino(inode),
  460. wbattr.ia_valid);
  461. if (wbattr.ia_valid == 0) {
  462. gossip_debug(GOSSIP_UTILS_DEBUG,
  463. "pvfs2_flush_inode skipping setattr()\n");
  464. return 0;
  465. }
  466. gossip_debug(GOSSIP_UTILS_DEBUG,
  467. "pvfs2_flush_inode (%pU) writing mode %o\n",
  468. get_khandle_from_ino(inode),
  469. inode->i_mode);
  470. ret = pvfs2_inode_setattr(inode, &wbattr);
  471. return ret;
  472. }
  473. int pvfs2_unmount_sb(struct super_block *sb)
  474. {
  475. int ret = -EINVAL;
  476. struct pvfs2_kernel_op_s *new_op = NULL;
  477. gossip_debug(GOSSIP_UTILS_DEBUG,
  478. "pvfs2_unmount_sb called on sb %p\n",
  479. sb);
  480. new_op = op_alloc(PVFS2_VFS_OP_FS_UMOUNT);
  481. if (!new_op)
  482. return -ENOMEM;
  483. new_op->upcall.req.fs_umount.id = PVFS2_SB(sb)->id;
  484. new_op->upcall.req.fs_umount.fs_id = PVFS2_SB(sb)->fs_id;
  485. strncpy(new_op->upcall.req.fs_umount.pvfs2_config_server,
  486. PVFS2_SB(sb)->devname,
  487. PVFS_MAX_SERVER_ADDR_LEN);
  488. gossip_debug(GOSSIP_UTILS_DEBUG,
  489. "Attempting PVFS2 Unmount via host %s\n",
  490. new_op->upcall.req.fs_umount.pvfs2_config_server);
  491. ret = service_operation(new_op, "pvfs2_fs_umount", 0);
  492. gossip_debug(GOSSIP_UTILS_DEBUG,
  493. "pvfs2_unmount: got return value of %d\n", ret);
  494. if (ret)
  495. sb = ERR_PTR(ret);
  496. else
  497. PVFS2_SB(sb)->mount_pending = 1;
  498. op_release(new_op);
  499. return ret;
  500. }
  501. /*
  502. * NOTE: on successful cancellation, be sure to return -EINTR, as
  503. * that's the return value the caller expects
  504. */
  505. int pvfs2_cancel_op_in_progress(__u64 tag)
  506. {
  507. int ret = -EINVAL;
  508. struct pvfs2_kernel_op_s *new_op = NULL;
  509. gossip_debug(GOSSIP_UTILS_DEBUG,
  510. "pvfs2_cancel_op_in_progress called on tag %llu\n",
  511. llu(tag));
  512. new_op = op_alloc(PVFS2_VFS_OP_CANCEL);
  513. if (!new_op)
  514. return -ENOMEM;
  515. new_op->upcall.req.cancel.op_tag = tag;
  516. gossip_debug(GOSSIP_UTILS_DEBUG,
  517. "Attempting PVFS2 operation cancellation of tag %llu\n",
  518. llu(new_op->upcall.req.cancel.op_tag));
  519. ret = service_operation(new_op, "pvfs2_cancel", PVFS2_OP_CANCELLATION);
  520. gossip_debug(GOSSIP_UTILS_DEBUG,
  521. "pvfs2_cancel_op_in_progress: got return value of %d\n",
  522. ret);
  523. op_release(new_op);
  524. return ret;
  525. }
  526. void pvfs2_op_initialize(struct pvfs2_kernel_op_s *op)
  527. {
  528. if (op) {
  529. spin_lock(&op->lock);
  530. op->io_completed = 0;
  531. op->upcall.type = PVFS2_VFS_OP_INVALID;
  532. op->downcall.type = PVFS2_VFS_OP_INVALID;
  533. op->downcall.status = -1;
  534. op->op_state = OP_VFS_STATE_UNKNOWN;
  535. op->tag = 0;
  536. spin_unlock(&op->lock);
  537. }
  538. }
  539. void pvfs2_make_bad_inode(struct inode *inode)
  540. {
  541. if (is_root_handle(inode)) {
  542. /*
  543. * if this occurs, the pvfs2-client-core was killed but we
  544. * can't afford to lose the inode operations and such
  545. * associated with the root handle in any case.
  546. */
  547. gossip_debug(GOSSIP_UTILS_DEBUG,
  548. "*** NOT making bad root inode %pU\n",
  549. get_khandle_from_ino(inode));
  550. } else {
  551. gossip_debug(GOSSIP_UTILS_DEBUG,
  552. "*** making bad inode %pU\n",
  553. get_khandle_from_ino(inode));
  554. make_bad_inode(inode);
  555. }
  556. }
  557. /* Block all blockable signals... */
  558. void block_signals(sigset_t *orig_sigset)
  559. {
  560. sigset_t mask;
  561. /*
  562. * Initialize all entries in the signal set to the
  563. * inverse of the given mask.
  564. */
  565. siginitsetinv(&mask, sigmask(SIGKILL));
  566. /* Block 'em Danno... */
  567. sigprocmask(SIG_BLOCK, &mask, orig_sigset);
  568. }
  569. /* set the signal mask to the given template... */
  570. void set_signals(sigset_t *sigset)
  571. {
  572. sigprocmask(SIG_SETMASK, sigset, NULL);
  573. }
  574. __u64 pvfs2_convert_time_field(void *time_ptr)
  575. {
  576. __u64 pvfs2_time;
  577. struct timespec *tspec = (struct timespec *)time_ptr;
  578. pvfs2_time = (__u64) ((time_t) tspec->tv_sec);
  579. return pvfs2_time;
  580. }
  581. /*
  582. * The following is a very dirty hack that is now a permanent part of the
  583. * PVFS2 protocol. See protocol.h for more error definitions.
  584. */
  585. /* The order matches include/pvfs2-types.h in the OrangeFS source. */
  586. static int PINT_errno_mapping[] = {
  587. 0, EPERM, ENOENT, EINTR, EIO, ENXIO, EBADF, EAGAIN, ENOMEM,
  588. EFAULT, EBUSY, EEXIST, ENODEV, ENOTDIR, EISDIR, EINVAL, EMFILE,
  589. EFBIG, ENOSPC, EROFS, EMLINK, EPIPE, EDEADLK, ENAMETOOLONG,
  590. ENOLCK, ENOSYS, ENOTEMPTY, ELOOP, EWOULDBLOCK, ENOMSG, EUNATCH,
  591. EBADR, EDEADLOCK, ENODATA, ETIME, ENONET, EREMOTE, ECOMM,
  592. EPROTO, EBADMSG, EOVERFLOW, ERESTART, EMSGSIZE, EPROTOTYPE,
  593. ENOPROTOOPT, EPROTONOSUPPORT, EOPNOTSUPP, EADDRINUSE,
  594. EADDRNOTAVAIL, ENETDOWN, ENETUNREACH, ENETRESET, ENOBUFS,
  595. ETIMEDOUT, ECONNREFUSED, EHOSTDOWN, EHOSTUNREACH, EALREADY,
  596. EACCES, ECONNRESET, ERANGE
  597. };
  598. int pvfs2_normalize_to_errno(__s32 error_code)
  599. {
  600. __u32 i;
  601. /* Success */
  602. if (error_code == 0) {
  603. return 0;
  604. /*
  605. * This shouldn't ever happen. If it does it should be fixed on the
  606. * server.
  607. */
  608. } else if (error_code > 0) {
  609. gossip_err("pvfs2: error status receieved.\n");
  610. gossip_err("pvfs2: assuming error code is inverted.\n");
  611. error_code = -error_code;
  612. }
  613. /*
  614. * XXX: This is very bad since error codes from PVFS2 may not be
  615. * suitable for return into userspace.
  616. */
  617. /*
  618. * Convert PVFS2 error values into errno values suitable for return
  619. * from the kernel.
  620. */
  621. if ((-error_code) & PVFS_NON_ERRNO_ERROR_BIT) {
  622. if (((-error_code) &
  623. (PVFS_ERROR_NUMBER_BITS|PVFS_NON_ERRNO_ERROR_BIT|
  624. PVFS_ERROR_BIT)) == PVFS_ECANCEL) {
  625. /*
  626. * cancellation error codes generally correspond to
  627. * a timeout from the client's perspective
  628. */
  629. error_code = -ETIMEDOUT;
  630. } else {
  631. /* assume a default error code */
  632. gossip_err("pvfs2: warning: got error code without errno equivalent: %d.\n", error_code);
  633. error_code = -EINVAL;
  634. }
  635. /* Convert PVFS2 encoded errno values into regular errno values. */
  636. } else if ((-error_code) & PVFS_ERROR_BIT) {
  637. i = (-error_code) & ~(PVFS_ERROR_BIT|PVFS_ERROR_CLASS_BITS);
  638. if (i < sizeof(PINT_errno_mapping)/sizeof(*PINT_errno_mapping))
  639. error_code = -PINT_errno_mapping[i];
  640. else
  641. error_code = -EINVAL;
  642. /*
  643. * Only PVFS2 protocol error codes should ever come here. Otherwise
  644. * there is a bug somewhere.
  645. */
  646. } else {
  647. gossip_err("pvfs2: pvfs2_normalize_to_errno: got error code which is not from PVFS2.\n");
  648. }
  649. return error_code;
  650. }
  651. #define NUM_MODES 11
  652. __s32 PVFS_util_translate_mode(int mode)
  653. {
  654. int ret = 0;
  655. int i = 0;
  656. static int modes[NUM_MODES] = {
  657. S_IXOTH, S_IWOTH, S_IROTH,
  658. S_IXGRP, S_IWGRP, S_IRGRP,
  659. S_IXUSR, S_IWUSR, S_IRUSR,
  660. S_ISGID, S_ISUID
  661. };
  662. static int pvfs2_modes[NUM_MODES] = {
  663. PVFS_O_EXECUTE, PVFS_O_WRITE, PVFS_O_READ,
  664. PVFS_G_EXECUTE, PVFS_G_WRITE, PVFS_G_READ,
  665. PVFS_U_EXECUTE, PVFS_U_WRITE, PVFS_U_READ,
  666. PVFS_G_SGID, PVFS_U_SUID
  667. };
  668. for (i = 0; i < NUM_MODES; i++)
  669. if (mode & modes[i])
  670. ret |= pvfs2_modes[i];
  671. return ret;
  672. }
  673. #undef NUM_MODES
  674. /*
  675. * After obtaining a string representation of the client's debug
  676. * keywords and their associated masks, this function is called to build an
  677. * array of these values.
  678. */
  679. int orangefs_prepare_cdm_array(char *debug_array_string)
  680. {
  681. int i;
  682. int rc = -EINVAL;
  683. char *cds_head = NULL;
  684. char *cds_delimiter = NULL;
  685. int keyword_len = 0;
  686. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  687. /*
  688. * figure out how many elements the cdm_array needs.
  689. */
  690. for (i = 0; i < strlen(debug_array_string); i++)
  691. if (debug_array_string[i] == '\n')
  692. cdm_element_count++;
  693. if (!cdm_element_count) {
  694. pr_info("No elements in client debug array string!\n");
  695. goto out;
  696. }
  697. cdm_array =
  698. kzalloc(cdm_element_count * sizeof(struct client_debug_mask),
  699. GFP_KERNEL);
  700. if (!cdm_array) {
  701. pr_info("malloc failed for cdm_array!\n");
  702. rc = -ENOMEM;
  703. goto out;
  704. }
  705. cds_head = debug_array_string;
  706. for (i = 0; i < cdm_element_count; i++) {
  707. cds_delimiter = strchr(cds_head, '\n');
  708. *cds_delimiter = '\0';
  709. keyword_len = strcspn(cds_head, " ");
  710. cdm_array[i].keyword = kzalloc(keyword_len + 1, GFP_KERNEL);
  711. if (!cdm_array[i].keyword) {
  712. rc = -ENOMEM;
  713. goto out;
  714. }
  715. sscanf(cds_head,
  716. "%s %llx %llx",
  717. cdm_array[i].keyword,
  718. (unsigned long long *)&(cdm_array[i].mask1),
  719. (unsigned long long *)&(cdm_array[i].mask2));
  720. if (!strcmp(cdm_array[i].keyword, PVFS2_VERBOSE))
  721. client_verbose_index = i;
  722. if (!strcmp(cdm_array[i].keyword, PVFS2_ALL))
  723. client_all_index = i;
  724. cds_head = cds_delimiter + 1;
  725. }
  726. rc = cdm_element_count;
  727. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: rc:%d:\n", __func__, rc);
  728. out:
  729. return rc;
  730. }
  731. /*
  732. * /sys/kernel/debug/orangefs/debug-help can be catted to
  733. * see all the available kernel and client debug keywords.
  734. *
  735. * When the kernel boots, we have no idea what keywords the
  736. * client supports, nor their associated masks.
  737. *
  738. * We pass through this function once at boot and stamp a
  739. * boilerplate "we don't know" message for the client in the
  740. * debug-help file. We pass through here again when the client
  741. * starts and then we can fill out the debug-help file fully.
  742. *
  743. * The client might be restarted any number of times between
  744. * reboots, we only build the debug-help file the first time.
  745. */
  746. int orangefs_prepare_debugfs_help_string(int at_boot)
  747. {
  748. int rc = -EINVAL;
  749. int i;
  750. int byte_count = 0;
  751. char *client_title = "Client Debug Keywords:\n";
  752. char *kernel_title = "Kernel Debug Keywords:\n";
  753. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  754. if (at_boot) {
  755. byte_count += strlen(HELP_STRING_UNINITIALIZED);
  756. client_title = HELP_STRING_UNINITIALIZED;
  757. } else {
  758. /*
  759. * fill the client keyword/mask array and remember
  760. * how many elements there were.
  761. */
  762. cdm_element_count =
  763. orangefs_prepare_cdm_array(client_debug_array_string);
  764. if (cdm_element_count <= 0)
  765. goto out;
  766. /* Count the bytes destined for debug_help_string. */
  767. byte_count += strlen(client_title);
  768. for (i = 0; i < cdm_element_count; i++) {
  769. byte_count += strlen(cdm_array[i].keyword + 2);
  770. if (byte_count >= DEBUG_HELP_STRING_SIZE) {
  771. pr_info("%s: overflow 1!\n", __func__);
  772. goto out;
  773. }
  774. }
  775. gossip_debug(GOSSIP_UTILS_DEBUG,
  776. "%s: cdm_element_count:%d:\n",
  777. __func__,
  778. cdm_element_count);
  779. }
  780. byte_count += strlen(kernel_title);
  781. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  782. byte_count +=
  783. strlen(s_kmod_keyword_mask_map[i].keyword + 2);
  784. if (byte_count >= DEBUG_HELP_STRING_SIZE) {
  785. pr_info("%s: overflow 2!\n", __func__);
  786. goto out;
  787. }
  788. }
  789. /* build debug_help_string. */
  790. debug_help_string = kzalloc(DEBUG_HELP_STRING_SIZE, GFP_KERNEL);
  791. if (!debug_help_string) {
  792. rc = -ENOMEM;
  793. goto out;
  794. }
  795. strcat(debug_help_string, client_title);
  796. if (!at_boot) {
  797. for (i = 0; i < cdm_element_count; i++) {
  798. strcat(debug_help_string, "\t");
  799. strcat(debug_help_string, cdm_array[i].keyword);
  800. strcat(debug_help_string, "\n");
  801. }
  802. }
  803. strcat(debug_help_string, "\n");
  804. strcat(debug_help_string, kernel_title);
  805. for (i = 0; i < num_kmod_keyword_mask_map; i++) {
  806. strcat(debug_help_string, "\t");
  807. strcat(debug_help_string, s_kmod_keyword_mask_map[i].keyword);
  808. strcat(debug_help_string, "\n");
  809. }
  810. rc = 0;
  811. out:
  812. return rc;
  813. }
  814. /*
  815. * kernel = type 0
  816. * client = type 1
  817. */
  818. void debug_mask_to_string(void *mask, int type)
  819. {
  820. int i;
  821. int len = 0;
  822. char *debug_string;
  823. int element_count = 0;
  824. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  825. if (type) {
  826. debug_string = client_debug_string;
  827. element_count = cdm_element_count;
  828. } else {
  829. debug_string = kernel_debug_string;
  830. element_count = num_kmod_keyword_mask_map;
  831. }
  832. memset(debug_string, 0, PVFS2_MAX_DEBUG_STRING_LEN);
  833. /*
  834. * Some keywords, like "all" or "verbose", are amalgams of
  835. * numerous other keywords. Make a special check for those
  836. * before grinding through the whole mask only to find out
  837. * later...
  838. */
  839. if (check_amalgam_keyword(mask, type))
  840. goto out;
  841. /* Build the debug string. */
  842. for (i = 0; i < element_count; i++)
  843. if (type)
  844. do_c_string(mask, i);
  845. else
  846. do_k_string(mask, i);
  847. len = strlen(debug_string);
  848. if ((len) && (type))
  849. client_debug_string[len - 1] = '\0';
  850. else if (len)
  851. kernel_debug_string[len - 1] = '\0';
  852. else if (type)
  853. strcpy(client_debug_string, "none");
  854. else
  855. strcpy(kernel_debug_string, "none");
  856. out:
  857. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: string:%s:\n", __func__, debug_string);
  858. return;
  859. }
  860. void do_k_string(void *k_mask, int index)
  861. {
  862. __u64 *mask = (__u64 *) k_mask;
  863. if (keyword_is_amalgam((char *) s_kmod_keyword_mask_map[index].keyword))
  864. goto out;
  865. if (*mask & s_kmod_keyword_mask_map[index].mask_val) {
  866. if ((strlen(kernel_debug_string) +
  867. strlen(s_kmod_keyword_mask_map[index].keyword))
  868. < PVFS2_MAX_DEBUG_STRING_LEN - 1) {
  869. strcat(kernel_debug_string,
  870. s_kmod_keyword_mask_map[index].keyword);
  871. strcat(kernel_debug_string, ",");
  872. } else {
  873. gossip_err("%s: overflow!\n", __func__);
  874. strcpy(kernel_debug_string, PVFS2_ALL);
  875. goto out;
  876. }
  877. }
  878. out:
  879. return;
  880. }
  881. void do_c_string(void *c_mask, int index)
  882. {
  883. struct client_debug_mask *mask = (struct client_debug_mask *) c_mask;
  884. if (keyword_is_amalgam(cdm_array[index].keyword))
  885. goto out;
  886. if ((mask->mask1 & cdm_array[index].mask1) ||
  887. (mask->mask2 & cdm_array[index].mask2)) {
  888. if ((strlen(client_debug_string) +
  889. strlen(cdm_array[index].keyword) + 1)
  890. < PVFS2_MAX_DEBUG_STRING_LEN - 2) {
  891. strcat(client_debug_string,
  892. cdm_array[index].keyword);
  893. strcat(client_debug_string, ",");
  894. } else {
  895. gossip_err("%s: overflow!\n", __func__);
  896. strcpy(client_debug_string, PVFS2_ALL);
  897. goto out;
  898. }
  899. }
  900. out:
  901. return;
  902. }
  903. int keyword_is_amalgam(char *keyword)
  904. {
  905. int rc = 0;
  906. if ((!strcmp(keyword, PVFS2_ALL)) || (!strcmp(keyword, PVFS2_VERBOSE)))
  907. rc = 1;
  908. return rc;
  909. }
  910. /*
  911. * kernel = type 0
  912. * client = type 1
  913. *
  914. * return 1 if we found an amalgam.
  915. */
  916. int check_amalgam_keyword(void *mask, int type)
  917. {
  918. __u64 *k_mask;
  919. struct client_debug_mask *c_mask;
  920. int k_all_index = num_kmod_keyword_mask_map - 1;
  921. int rc = 0;
  922. if (type) {
  923. c_mask = (struct client_debug_mask *) mask;
  924. if ((c_mask->mask1 == cdm_array[client_all_index].mask1) &&
  925. (c_mask->mask2 == cdm_array[client_all_index].mask2)) {
  926. strcpy(client_debug_string, PVFS2_ALL);
  927. rc = 1;
  928. goto out;
  929. }
  930. if ((c_mask->mask1 == cdm_array[client_verbose_index].mask1) &&
  931. (c_mask->mask2 == cdm_array[client_verbose_index].mask2)) {
  932. strcpy(client_debug_string, PVFS2_VERBOSE);
  933. rc = 1;
  934. goto out;
  935. }
  936. } else {
  937. k_mask = (__u64 *) mask;
  938. if (*k_mask >= s_kmod_keyword_mask_map[k_all_index].mask_val) {
  939. strcpy(kernel_debug_string, PVFS2_ALL);
  940. rc = 1;
  941. goto out;
  942. }
  943. }
  944. out:
  945. return rc;
  946. }
  947. /*
  948. * kernel = type 0
  949. * client = type 1
  950. */
  951. void debug_string_to_mask(char *debug_string, void *mask, int type)
  952. {
  953. char *unchecked_keyword;
  954. int i;
  955. char *strsep_fodder = kstrdup(debug_string, GFP_KERNEL);
  956. char *original_pointer;
  957. int element_count = 0;
  958. struct client_debug_mask *c_mask;
  959. __u64 *k_mask;
  960. gossip_debug(GOSSIP_UTILS_DEBUG, "%s: start\n", __func__);
  961. if (type) {
  962. c_mask = (struct client_debug_mask *)mask;
  963. element_count = cdm_element_count;
  964. } else {
  965. k_mask = (__u64 *)mask;
  966. *k_mask = 0;
  967. element_count = num_kmod_keyword_mask_map;
  968. }
  969. original_pointer = strsep_fodder;
  970. while ((unchecked_keyword = strsep(&strsep_fodder, ",")))
  971. if (strlen(unchecked_keyword)) {
  972. for (i = 0; i < element_count; i++)
  973. if (type)
  974. do_c_mask(i,
  975. unchecked_keyword,
  976. &c_mask);
  977. else
  978. do_k_mask(i,
  979. unchecked_keyword,
  980. &k_mask);
  981. }
  982. kfree(original_pointer);
  983. }
  984. void do_c_mask(int i,
  985. char *unchecked_keyword,
  986. struct client_debug_mask **sane_mask)
  987. {
  988. if (!strcmp(cdm_array[i].keyword, unchecked_keyword)) {
  989. (**sane_mask).mask1 = (**sane_mask).mask1 | cdm_array[i].mask1;
  990. (**sane_mask).mask2 = (**sane_mask).mask2 | cdm_array[i].mask2;
  991. }
  992. }
  993. void do_k_mask(int i, char *unchecked_keyword, __u64 **sane_mask)
  994. {
  995. if (!strcmp(s_kmod_keyword_mask_map[i].keyword, unchecked_keyword))
  996. **sane_mask = (**sane_mask) |
  997. s_kmod_keyword_mask_map[i].mask_val;
  998. }