devpvfs2-req.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  1. /*
  2. * (C) 2001 Clemson University and The University of Chicago
  3. *
  4. * Changes by Acxiom Corporation to add protocol version to kernel
  5. * communication, Copyright Acxiom Corporation, 2005.
  6. *
  7. * See COPYING in top-level directory.
  8. */
  9. #include "protocol.h"
  10. #include "pvfs2-kernel.h"
  11. #include "pvfs2-dev-proto.h"
  12. #include "pvfs2-bufmap.h"
  13. #include <linux/debugfs.h>
  14. #include <linux/slab.h>
  15. /* this file implements the /dev/pvfs2-req device node */
  16. static int open_access_count;
  17. #define DUMP_DEVICE_ERROR() \
  18. do { \
  19. gossip_err("*****************************************************\n");\
  20. gossip_err("PVFS2 Device Error: You cannot open the device file "); \
  21. gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
  22. "are no ", PVFS2_REQDEVICE_NAME); \
  23. gossip_err("instances of a program using this device\ncurrently " \
  24. "running. (You must verify this!)\n"); \
  25. gossip_err("For example, you can use the lsof program as follows:\n");\
  26. gossip_err("'lsof | grep %s' (run this as root)\n", \
  27. PVFS2_REQDEVICE_NAME); \
  28. gossip_err(" open_access_count = %d\n", open_access_count); \
  29. gossip_err("*****************************************************\n");\
  30. } while (0)
  31. static int hash_func(__u64 tag, int table_size)
  32. {
  33. return do_div(tag, (unsigned int)table_size);
  34. }
  35. static void pvfs2_devreq_add_op(struct pvfs2_kernel_op_s *op)
  36. {
  37. int index = hash_func(op->tag, hash_table_size);
  38. spin_lock(&htable_ops_in_progress_lock);
  39. list_add_tail(&op->list, &htable_ops_in_progress[index]);
  40. spin_unlock(&htable_ops_in_progress_lock);
  41. }
  42. static struct pvfs2_kernel_op_s *pvfs2_devreq_remove_op(__u64 tag)
  43. {
  44. struct pvfs2_kernel_op_s *op, *next;
  45. int index;
  46. index = hash_func(tag, hash_table_size);
  47. spin_lock(&htable_ops_in_progress_lock);
  48. list_for_each_entry_safe(op,
  49. next,
  50. &htable_ops_in_progress[index],
  51. list) {
  52. if (op->tag == tag) {
  53. list_del(&op->list);
  54. spin_unlock(&htable_ops_in_progress_lock);
  55. return op;
  56. }
  57. }
  58. spin_unlock(&htable_ops_in_progress_lock);
  59. return NULL;
  60. }
  61. static int pvfs2_devreq_open(struct inode *inode, struct file *file)
  62. {
  63. int ret = -EINVAL;
  64. if (!(file->f_flags & O_NONBLOCK)) {
  65. gossip_err("pvfs2: device cannot be opened in blocking mode\n");
  66. goto out;
  67. }
  68. ret = -EACCES;
  69. gossip_debug(GOSSIP_DEV_DEBUG, "pvfs2-client-core: opening device\n");
  70. mutex_lock(&devreq_mutex);
  71. if (open_access_count == 0) {
  72. ret = generic_file_open(inode, file);
  73. if (ret == 0)
  74. open_access_count++;
  75. } else {
  76. DUMP_DEVICE_ERROR();
  77. }
  78. mutex_unlock(&devreq_mutex);
  79. out:
  80. gossip_debug(GOSSIP_DEV_DEBUG,
  81. "pvfs2-client-core: open device complete (ret = %d)\n",
  82. ret);
  83. return ret;
  84. }
  85. static ssize_t pvfs2_devreq_read(struct file *file,
  86. char __user *buf,
  87. size_t count, loff_t *offset)
  88. {
  89. int ret = 0;
  90. ssize_t len = 0;
  91. struct pvfs2_kernel_op_s *cur_op = NULL;
  92. static __s32 magic = PVFS2_DEVREQ_MAGIC;
  93. __s32 proto_ver = PVFS_KERNEL_PROTO_VERSION;
  94. if (!(file->f_flags & O_NONBLOCK)) {
  95. /* We do not support blocking reads/opens any more */
  96. gossip_err("pvfs2: blocking reads are not supported! (pvfs2-client-core bug)\n");
  97. return -EINVAL;
  98. } else {
  99. struct pvfs2_kernel_op_s *op = NULL, *temp = NULL;
  100. /* get next op (if any) from top of list */
  101. spin_lock(&pvfs2_request_list_lock);
  102. list_for_each_entry_safe(op, temp, &pvfs2_request_list, list) {
  103. __s32 fsid = fsid_of_op(op);
  104. /*
  105. * Check if this op's fsid is known and needs
  106. * remounting
  107. */
  108. if (fsid != PVFS_FS_ID_NULL &&
  109. fs_mount_pending(fsid) == 1) {
  110. gossip_debug(GOSSIP_DEV_DEBUG,
  111. "Skipping op tag %llu %s\n",
  112. llu(op->tag),
  113. get_opname_string(op));
  114. continue;
  115. } else {
  116. /*
  117. * op does not belong to any particular fsid
  118. * or already mounted.. let it through
  119. */
  120. cur_op = op;
  121. spin_lock(&cur_op->lock);
  122. list_del(&cur_op->list);
  123. cur_op->op_linger_tmp--;
  124. /*
  125. * if there is a trailer, re-add it to
  126. * the request list.
  127. */
  128. if (cur_op->op_linger == 2 &&
  129. cur_op->op_linger_tmp == 1) {
  130. if (cur_op->upcall.trailer_size <= 0 ||
  131. cur_op->upcall.trailer_buf == NULL)
  132. gossip_err("BUG:trailer_size is %ld and trailer buf is %p\n", (long)cur_op->upcall.trailer_size, cur_op->upcall.trailer_buf);
  133. /* re-add it to the head of the list */
  134. list_add(&cur_op->list,
  135. &pvfs2_request_list);
  136. }
  137. spin_unlock(&cur_op->lock);
  138. break;
  139. }
  140. }
  141. spin_unlock(&pvfs2_request_list_lock);
  142. }
  143. if (cur_op) {
  144. spin_lock(&cur_op->lock);
  145. gossip_debug(GOSSIP_DEV_DEBUG,
  146. "client-core: reading op tag %llu %s\n",
  147. llu(cur_op->tag), get_opname_string(cur_op));
  148. if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
  149. if (cur_op->op_linger == 1)
  150. gossip_err("WARNING: Current op already queued...skipping\n");
  151. } else if (cur_op->op_linger == 1 ||
  152. (cur_op->op_linger == 2 &&
  153. cur_op->op_linger_tmp == 0)) {
  154. /*
  155. * atomically move the operation to the
  156. * htable_ops_in_progress
  157. */
  158. set_op_state_inprogress(cur_op);
  159. pvfs2_devreq_add_op(cur_op);
  160. }
  161. spin_unlock(&cur_op->lock);
  162. /* 2 cases
  163. * a) OPs with no trailers
  164. * b) OPs with trailers, Stage 1
  165. * Either way push the upcall out
  166. */
  167. if (cur_op->op_linger == 1 ||
  168. (cur_op->op_linger == 2 && cur_op->op_linger_tmp == 1)) {
  169. len = MAX_ALIGNED_DEV_REQ_UPSIZE;
  170. if ((size_t) len <= count) {
  171. ret = copy_to_user(buf,
  172. &proto_ver,
  173. sizeof(__s32));
  174. if (ret == 0) {
  175. ret = copy_to_user(buf + sizeof(__s32),
  176. &magic,
  177. sizeof(__s32));
  178. if (ret == 0) {
  179. ret = copy_to_user(buf+2 * sizeof(__s32),
  180. &cur_op->tag,
  181. sizeof(__u64));
  182. if (ret == 0) {
  183. ret = copy_to_user(
  184. buf +
  185. 2 *
  186. sizeof(__s32) +
  187. sizeof(__u64),
  188. &cur_op->upcall,
  189. sizeof(struct pvfs2_upcall_s));
  190. }
  191. }
  192. }
  193. if (ret) {
  194. gossip_err("Failed to copy data to user space\n");
  195. len = -EFAULT;
  196. }
  197. } else {
  198. gossip_err
  199. ("Failed to copy data to user space\n");
  200. len = -EIO;
  201. }
  202. }
  203. /* Stage 2: Push the trailer out */
  204. else if (cur_op->op_linger == 2 && cur_op->op_linger_tmp == 0) {
  205. len = cur_op->upcall.trailer_size;
  206. if ((size_t) len <= count) {
  207. ret = copy_to_user(buf,
  208. cur_op->upcall.trailer_buf,
  209. len);
  210. if (ret) {
  211. gossip_err("Failed to copy trailer to user space\n");
  212. len = -EFAULT;
  213. }
  214. } else {
  215. gossip_err("Read buffer for trailer is too small (%ld as opposed to %ld)\n",
  216. (long)count,
  217. (long)len);
  218. len = -EIO;
  219. }
  220. } else {
  221. gossip_err("cur_op: %p (op_linger %d), (op_linger_tmp %d), erroneous request list?\n",
  222. cur_op,
  223. cur_op->op_linger,
  224. cur_op->op_linger_tmp);
  225. len = 0;
  226. }
  227. } else if (file->f_flags & O_NONBLOCK) {
  228. /*
  229. * if in non-blocking mode, return EAGAIN since no requests are
  230. * ready yet
  231. */
  232. len = -EAGAIN;
  233. }
  234. return len;
  235. }
  236. /* Function for writev() callers into the device */
  237. static ssize_t pvfs2_devreq_writev(struct file *file,
  238. const struct iovec *iov,
  239. size_t count,
  240. loff_t *offset)
  241. {
  242. struct pvfs2_kernel_op_s *op = NULL;
  243. void *buffer = NULL;
  244. void *ptr = NULL;
  245. unsigned long i = 0;
  246. static int max_downsize = MAX_ALIGNED_DEV_REQ_DOWNSIZE;
  247. int ret = 0, num_remaining = max_downsize;
  248. int notrailer_count = 4; /* num elements in iovec without trailer */
  249. int payload_size = 0;
  250. __s32 magic = 0;
  251. __s32 proto_ver = 0;
  252. __u64 tag = 0;
  253. ssize_t total_returned_size = 0;
  254. /* Either there is a trailer or there isn't */
  255. if (count != notrailer_count && count != (notrailer_count + 1)) {
  256. gossip_err("Error: Number of iov vectors is (%zu) and notrailer count is %d\n",
  257. count,
  258. notrailer_count);
  259. return -EPROTO;
  260. }
  261. buffer = dev_req_alloc();
  262. if (!buffer)
  263. return -ENOMEM;
  264. ptr = buffer;
  265. for (i = 0; i < notrailer_count; i++) {
  266. if (iov[i].iov_len > num_remaining) {
  267. gossip_err
  268. ("writev error: Freeing buffer and returning\n");
  269. dev_req_release(buffer);
  270. return -EMSGSIZE;
  271. }
  272. ret = copy_from_user(ptr, iov[i].iov_base, iov[i].iov_len);
  273. if (ret) {
  274. gossip_err("Failed to copy data from user space\n");
  275. dev_req_release(buffer);
  276. return -EIO;
  277. }
  278. num_remaining -= iov[i].iov_len;
  279. ptr += iov[i].iov_len;
  280. payload_size += iov[i].iov_len;
  281. }
  282. total_returned_size = payload_size;
  283. /* these elements are currently 8 byte aligned (8 bytes for (version +
  284. * magic) 8 bytes for tag). If you add another element, either
  285. * make it 8 bytes big, or use get_unaligned when asigning.
  286. */
  287. ptr = buffer;
  288. proto_ver = *((__s32 *) ptr);
  289. ptr += sizeof(__s32);
  290. magic = *((__s32 *) ptr);
  291. ptr += sizeof(__s32);
  292. tag = *((__u64 *) ptr);
  293. ptr += sizeof(__u64);
  294. if (magic != PVFS2_DEVREQ_MAGIC) {
  295. gossip_err("Error: Device magic number does not match.\n");
  296. dev_req_release(buffer);
  297. return -EPROTO;
  298. }
  299. /*
  300. * proto_ver = 20902 for 2.9.2
  301. */
  302. op = pvfs2_devreq_remove_op(tag);
  303. if (op) {
  304. /* Increase ref count! */
  305. get_op(op);
  306. /* cut off magic and tag from payload size */
  307. payload_size -= (2 * sizeof(__s32) + sizeof(__u64));
  308. if (payload_size <= sizeof(struct pvfs2_downcall_s))
  309. /* copy the passed in downcall into the op */
  310. memcpy(&op->downcall,
  311. ptr,
  312. sizeof(struct pvfs2_downcall_s));
  313. else
  314. gossip_debug(GOSSIP_DEV_DEBUG,
  315. "writev: Ignoring %d bytes\n",
  316. payload_size);
  317. /* Do not allocate needlessly if client-core forgets
  318. * to reset trailer size on op errors.
  319. */
  320. if (op->downcall.status == 0 && op->downcall.trailer_size > 0) {
  321. gossip_debug(GOSSIP_DEV_DEBUG,
  322. "writev: trailer size %ld\n",
  323. (unsigned long)op->downcall.trailer_size);
  324. if (count != (notrailer_count + 1)) {
  325. gossip_err("Error: trailer size (%ld) is non-zero, no trailer elements though? (%zu)\n", (unsigned long)op->downcall.trailer_size, count);
  326. dev_req_release(buffer);
  327. put_op(op);
  328. return -EPROTO;
  329. }
  330. if (iov[notrailer_count].iov_len >
  331. op->downcall.trailer_size) {
  332. gossip_err("writev error: trailer size (%ld) != iov_len (%ld)\n", (unsigned long)op->downcall.trailer_size, (unsigned long)iov[notrailer_count].iov_len);
  333. dev_req_release(buffer);
  334. put_op(op);
  335. return -EMSGSIZE;
  336. }
  337. /* Allocate a buffer large enough to hold the
  338. * trailer bytes.
  339. */
  340. op->downcall.trailer_buf =
  341. vmalloc(op->downcall.trailer_size);
  342. if (op->downcall.trailer_buf != NULL) {
  343. gossip_debug(GOSSIP_DEV_DEBUG, "vmalloc: %p\n",
  344. op->downcall.trailer_buf);
  345. ret = copy_from_user(op->downcall.trailer_buf,
  346. iov[notrailer_count].
  347. iov_base,
  348. iov[notrailer_count].
  349. iov_len);
  350. if (ret) {
  351. gossip_err("Failed to copy trailer data from user space\n");
  352. dev_req_release(buffer);
  353. gossip_debug(GOSSIP_DEV_DEBUG,
  354. "vfree: %p\n",
  355. op->downcall.trailer_buf);
  356. vfree(op->downcall.trailer_buf);
  357. op->downcall.trailer_buf = NULL;
  358. put_op(op);
  359. return -EIO;
  360. }
  361. } else {
  362. /* Change downcall status */
  363. op->downcall.status = -ENOMEM;
  364. gossip_err("writev: could not vmalloc for trailer!\n");
  365. }
  366. }
  367. /* if this operation is an I/O operation and if it was
  368. * initiated on behalf of a *synchronous* VFS I/O operation,
  369. * only then we need to wait
  370. * for all data to be copied before we can return to avoid
  371. * buffer corruption and races that can pull the buffers
  372. * out from under us.
  373. *
  374. * Essentially we're synchronizing with other parts of the
  375. * vfs implicitly by not allowing the user space
  376. * application reading/writing this device to return until
  377. * the buffers are done being used.
  378. */
  379. if ((op->upcall.type == PVFS2_VFS_OP_FILE_IO &&
  380. op->upcall.req.io.async_vfs_io == PVFS_VFS_SYNC_IO) ||
  381. op->upcall.type == PVFS2_VFS_OP_FILE_IOX) {
  382. int timed_out = 0;
  383. DECLARE_WAITQUEUE(wait_entry, current);
  384. /* tell the vfs op waiting on a waitqueue
  385. * that this op is done
  386. */
  387. spin_lock(&op->lock);
  388. set_op_state_serviced(op);
  389. spin_unlock(&op->lock);
  390. add_wait_queue_exclusive(&op->io_completion_waitq,
  391. &wait_entry);
  392. wake_up_interruptible(&op->waitq);
  393. while (1) {
  394. set_current_state(TASK_INTERRUPTIBLE);
  395. spin_lock(&op->lock);
  396. if (op->io_completed) {
  397. spin_unlock(&op->lock);
  398. break;
  399. }
  400. spin_unlock(&op->lock);
  401. if (!signal_pending(current)) {
  402. int timeout =
  403. MSECS_TO_JIFFIES(1000 *
  404. op_timeout_secs);
  405. if (!schedule_timeout(timeout)) {
  406. gossip_debug(GOSSIP_DEV_DEBUG, "*** I/O wait time is up\n");
  407. timed_out = 1;
  408. break;
  409. }
  410. continue;
  411. }
  412. gossip_debug(GOSSIP_DEV_DEBUG, "*** signal on I/O wait -- aborting\n");
  413. break;
  414. }
  415. set_current_state(TASK_RUNNING);
  416. remove_wait_queue(&op->io_completion_waitq,
  417. &wait_entry);
  418. /* NOTE: for I/O operations we handle releasing the op
  419. * object except in the case of timeout. the reason we
  420. * can't free the op in timeout cases is that the op
  421. * service logic in the vfs retries operations using
  422. * the same op ptr, thus it can't be freed.
  423. */
  424. if (!timed_out)
  425. op_release(op);
  426. } else {
  427. /*
  428. * tell the vfs op waiting on a waitqueue that
  429. * this op is done
  430. */
  431. spin_lock(&op->lock);
  432. set_op_state_serviced(op);
  433. spin_unlock(&op->lock);
  434. /*
  435. * for every other operation (i.e. non-I/O), we need to
  436. * wake up the callers for downcall completion
  437. * notification
  438. */
  439. wake_up_interruptible(&op->waitq);
  440. }
  441. } else {
  442. /* ignore downcalls that we're not interested in */
  443. gossip_debug(GOSSIP_DEV_DEBUG,
  444. "WARNING: No one's waiting for tag %llu\n",
  445. llu(tag));
  446. }
  447. dev_req_release(buffer);
  448. return total_returned_size;
  449. }
  450. static ssize_t pvfs2_devreq_write_iter(struct kiocb *iocb,
  451. struct iov_iter *iter)
  452. {
  453. return pvfs2_devreq_writev(iocb->ki_filp,
  454. iter->iov,
  455. iter->nr_segs,
  456. &iocb->ki_pos);
  457. }
  458. /* Returns whether any FS are still pending remounted */
  459. static int mark_all_pending_mounts(void)
  460. {
  461. int unmounted = 1;
  462. struct pvfs2_sb_info_s *pvfs2_sb = NULL;
  463. spin_lock(&pvfs2_superblocks_lock);
  464. list_for_each_entry(pvfs2_sb, &pvfs2_superblocks, list) {
  465. /* All of these file system require a remount */
  466. pvfs2_sb->mount_pending = 1;
  467. unmounted = 0;
  468. }
  469. spin_unlock(&pvfs2_superblocks_lock);
  470. return unmounted;
  471. }
  472. /*
  473. * Determine if a given file system needs to be remounted or not
  474. * Returns -1 on error
  475. * 0 if already mounted
  476. * 1 if needs remount
  477. */
  478. int fs_mount_pending(__s32 fsid)
  479. {
  480. int mount_pending = -1;
  481. struct pvfs2_sb_info_s *pvfs2_sb = NULL;
  482. spin_lock(&pvfs2_superblocks_lock);
  483. list_for_each_entry(pvfs2_sb, &pvfs2_superblocks, list) {
  484. if (pvfs2_sb->fs_id == fsid) {
  485. mount_pending = pvfs2_sb->mount_pending;
  486. break;
  487. }
  488. }
  489. spin_unlock(&pvfs2_superblocks_lock);
  490. return mount_pending;
  491. }
  492. /*
  493. * NOTE: gets called when the last reference to this device is dropped.
  494. * Using the open_access_count variable, we enforce a reference count
  495. * on this file so that it can be opened by only one process at a time.
  496. * the devreq_mutex is used to make sure all i/o has completed
  497. * before we call pvfs_bufmap_finalize, and similar such tricky
  498. * situations
  499. */
  500. static int pvfs2_devreq_release(struct inode *inode, struct file *file)
  501. {
  502. int unmounted = 0;
  503. gossip_debug(GOSSIP_DEV_DEBUG,
  504. "%s:pvfs2-client-core: exiting, closing device\n",
  505. __func__);
  506. mutex_lock(&devreq_mutex);
  507. pvfs_bufmap_finalize();
  508. open_access_count--;
  509. unmounted = mark_all_pending_mounts();
  510. gossip_debug(GOSSIP_DEV_DEBUG, "PVFS2 Device Close: Filesystem(s) %s\n",
  511. (unmounted ? "UNMOUNTED" : "MOUNTED"));
  512. mutex_unlock(&devreq_mutex);
  513. /*
  514. * Walk through the list of ops in the request list, mark them
  515. * as purged and wake them up.
  516. */
  517. purge_waiting_ops();
  518. /*
  519. * Walk through the hash table of in progress operations; mark
  520. * them as purged and wake them up
  521. */
  522. purge_inprogress_ops();
  523. gossip_debug(GOSSIP_DEV_DEBUG,
  524. "pvfs2-client-core: device close complete\n");
  525. return 0;
  526. }
  527. int is_daemon_in_service(void)
  528. {
  529. int in_service;
  530. /*
  531. * What this function does is checks if client-core is alive
  532. * based on the access count we maintain on the device.
  533. */
  534. mutex_lock(&devreq_mutex);
  535. in_service = open_access_count == 1 ? 0 : -EIO;
  536. mutex_unlock(&devreq_mutex);
  537. return in_service;
  538. }
  539. static inline long check_ioctl_command(unsigned int command)
  540. {
  541. /* Check for valid ioctl codes */
  542. if (_IOC_TYPE(command) != PVFS_DEV_MAGIC) {
  543. gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
  544. command,
  545. _IOC_TYPE(command),
  546. PVFS_DEV_MAGIC);
  547. return -EINVAL;
  548. }
  549. /* and valid ioctl commands */
  550. if (_IOC_NR(command) >= PVFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
  551. gossip_err("Invalid ioctl command number [%d >= %d]\n",
  552. _IOC_NR(command), PVFS_DEV_MAXNR);
  553. return -ENOIOCTLCMD;
  554. }
  555. return 0;
  556. }
  557. static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
  558. {
  559. static __s32 magic = PVFS2_DEVREQ_MAGIC;
  560. static __s32 max_up_size = MAX_ALIGNED_DEV_REQ_UPSIZE;
  561. static __s32 max_down_size = MAX_ALIGNED_DEV_REQ_DOWNSIZE;
  562. struct PVFS_dev_map_desc user_desc;
  563. int ret = 0;
  564. struct dev_mask_info_s mask_info = { 0 };
  565. struct dev_mask2_info_s mask2_info = { 0, 0 };
  566. int upstream_kmod = 1;
  567. struct list_head *tmp = NULL;
  568. struct pvfs2_sb_info_s *pvfs2_sb = NULL;
  569. /* mtmoore: add locking here */
  570. switch (command) {
  571. case PVFS_DEV_GET_MAGIC:
  572. return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
  573. -EIO :
  574. 0);
  575. case PVFS_DEV_GET_MAX_UPSIZE:
  576. return ((put_user(max_up_size,
  577. (__s32 __user *) arg) == -EFAULT) ?
  578. -EIO :
  579. 0);
  580. case PVFS_DEV_GET_MAX_DOWNSIZE:
  581. return ((put_user(max_down_size,
  582. (__s32 __user *) arg) == -EFAULT) ?
  583. -EIO :
  584. 0);
  585. case PVFS_DEV_MAP:
  586. ret = copy_from_user(&user_desc,
  587. (struct PVFS_dev_map_desc __user *)
  588. arg,
  589. sizeof(struct PVFS_dev_map_desc));
  590. return ret ? -EIO : pvfs_bufmap_initialize(&user_desc);
  591. case PVFS_DEV_REMOUNT_ALL:
  592. gossip_debug(GOSSIP_DEV_DEBUG,
  593. "pvfs2_devreq_ioctl: got PVFS_DEV_REMOUNT_ALL\n");
  594. /*
  595. * remount all mounted pvfs2 volumes to regain the lost
  596. * dynamic mount tables (if any) -- NOTE: this is done
  597. * without keeping the superblock list locked due to the
  598. * upcall/downcall waiting. also, the request semaphore is
  599. * used to ensure that no operations will be serviced until
  600. * all of the remounts are serviced (to avoid ops between
  601. * mounts to fail)
  602. */
  603. ret = mutex_lock_interruptible(&request_mutex);
  604. if (ret < 0)
  605. return ret;
  606. gossip_debug(GOSSIP_DEV_DEBUG,
  607. "pvfs2_devreq_ioctl: priority remount in progress\n");
  608. list_for_each(tmp, &pvfs2_superblocks) {
  609. pvfs2_sb =
  610. list_entry(tmp, struct pvfs2_sb_info_s, list);
  611. if (pvfs2_sb && (pvfs2_sb->sb)) {
  612. gossip_debug(GOSSIP_DEV_DEBUG,
  613. "Remounting SB %p\n",
  614. pvfs2_sb);
  615. ret = pvfs2_remount(pvfs2_sb->sb);
  616. if (ret) {
  617. gossip_debug(GOSSIP_DEV_DEBUG,
  618. "SB %p remount failed\n",
  619. pvfs2_sb);
  620. break;
  621. }
  622. }
  623. }
  624. gossip_debug(GOSSIP_DEV_DEBUG,
  625. "pvfs2_devreq_ioctl: priority remount complete\n");
  626. mutex_unlock(&request_mutex);
  627. return ret;
  628. case PVFS_DEV_UPSTREAM:
  629. ret = copy_to_user((void __user *)arg,
  630. &upstream_kmod,
  631. sizeof(upstream_kmod));
  632. if (ret != 0)
  633. return -EIO;
  634. else
  635. return ret;
  636. case PVFS_DEV_CLIENT_MASK:
  637. ret = copy_from_user(&mask2_info,
  638. (void __user *)arg,
  639. sizeof(struct dev_mask2_info_s));
  640. if (ret != 0)
  641. return -EIO;
  642. client_debug_mask.mask1 = mask2_info.mask1_value;
  643. client_debug_mask.mask2 = mask2_info.mask2_value;
  644. pr_info("%s: client debug mask has been been received "
  645. ":%llx: :%llx:\n",
  646. __func__,
  647. (unsigned long long)client_debug_mask.mask1,
  648. (unsigned long long)client_debug_mask.mask2);
  649. return ret;
  650. case PVFS_DEV_CLIENT_STRING:
  651. ret = copy_from_user(&client_debug_array_string,
  652. (void __user *)arg,
  653. PVFS2_MAX_DEBUG_STRING_LEN);
  654. if (ret != 0) {
  655. pr_info("%s: "
  656. "PVFS_DEV_CLIENT_STRING: copy_from_user failed"
  657. "\n",
  658. __func__);
  659. return -EIO;
  660. }
  661. pr_info("%s: client debug array string has been been received."
  662. "\n",
  663. __func__);
  664. if (!help_string_initialized) {
  665. /* Free the "we don't know yet" default string... */
  666. kfree(debug_help_string);
  667. /* build a proper debug help string */
  668. if (orangefs_prepare_debugfs_help_string(0)) {
  669. gossip_err("%s: "
  670. "prepare_debugfs_help_string failed"
  671. "\n",
  672. __func__);
  673. return -EIO;
  674. }
  675. /* Replace the boilerplate boot-time debug-help file. */
  676. debugfs_remove(help_file_dentry);
  677. help_file_dentry =
  678. debugfs_create_file(
  679. ORANGEFS_KMOD_DEBUG_HELP_FILE,
  680. 0444,
  681. debug_dir,
  682. debug_help_string,
  683. &debug_help_fops);
  684. if (!help_file_dentry) {
  685. gossip_err("%s: debugfs_create_file failed for"
  686. " :%s:!\n",
  687. __func__,
  688. ORANGEFS_KMOD_DEBUG_HELP_FILE);
  689. return -EIO;
  690. }
  691. }
  692. debug_mask_to_string(&client_debug_mask, 1);
  693. debugfs_remove(client_debug_dentry);
  694. pvfs2_client_debug_init();
  695. help_string_initialized++;
  696. return ret;
  697. case PVFS_DEV_DEBUG:
  698. ret = copy_from_user(&mask_info,
  699. (void __user *)arg,
  700. sizeof(mask_info));
  701. if (ret != 0)
  702. return -EIO;
  703. if (mask_info.mask_type == KERNEL_MASK) {
  704. if ((mask_info.mask_value == 0)
  705. && (kernel_mask_set_mod_init)) {
  706. /*
  707. * the kernel debug mask was set when the
  708. * kernel module was loaded; don't override
  709. * it if the client-core was started without
  710. * a value for PVFS2_KMODMASK.
  711. */
  712. return 0;
  713. }
  714. debug_mask_to_string(&mask_info.mask_value,
  715. mask_info.mask_type);
  716. gossip_debug_mask = mask_info.mask_value;
  717. pr_info("PVFS: kernel debug mask has been modified to "
  718. ":%s: :%llx:\n",
  719. kernel_debug_string,
  720. (unsigned long long)gossip_debug_mask);
  721. } else if (mask_info.mask_type == CLIENT_MASK) {
  722. debug_mask_to_string(&mask_info.mask_value,
  723. mask_info.mask_type);
  724. pr_info("PVFS: client debug mask has been modified to"
  725. ":%s: :%llx:\n",
  726. client_debug_string,
  727. llu(mask_info.mask_value));
  728. } else {
  729. gossip_lerr("Invalid mask type....\n");
  730. return -EINVAL;
  731. }
  732. return ret;
  733. default:
  734. return -ENOIOCTLCMD;
  735. }
  736. return -ENOIOCTLCMD;
  737. }
  738. static long pvfs2_devreq_ioctl(struct file *file,
  739. unsigned int command, unsigned long arg)
  740. {
  741. long ret;
  742. /* Check for properly constructed commands */
  743. ret = check_ioctl_command(command);
  744. if (ret < 0)
  745. return (int)ret;
  746. return (int)dispatch_ioctl_command(command, arg);
  747. }
  748. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  749. /* Compat structure for the PVFS_DEV_MAP ioctl */
  750. struct PVFS_dev_map_desc32 {
  751. compat_uptr_t ptr;
  752. __s32 total_size;
  753. __s32 size;
  754. __s32 count;
  755. };
  756. static unsigned long translate_dev_map26(unsigned long args, long *error)
  757. {
  758. struct PVFS_dev_map_desc32 __user *p32 = (void __user *)args;
  759. /*
  760. * Depending on the architecture, allocate some space on the
  761. * user-call-stack based on our expected layout.
  762. */
  763. struct PVFS_dev_map_desc __user *p =
  764. compat_alloc_user_space(sizeof(*p));
  765. compat_uptr_t addr;
  766. *error = 0;
  767. /* get the ptr from the 32 bit user-space */
  768. if (get_user(addr, &p32->ptr))
  769. goto err;
  770. /* try to put that into a 64-bit layout */
  771. if (put_user(compat_ptr(addr), &p->ptr))
  772. goto err;
  773. /* copy the remaining fields */
  774. if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
  775. goto err;
  776. if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
  777. goto err;
  778. if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
  779. goto err;
  780. return (unsigned long)p;
  781. err:
  782. *error = -EFAULT;
  783. return 0;
  784. }
  785. /*
  786. * 32 bit user-space apps' ioctl handlers when kernel modules
  787. * is compiled as a 64 bit one
  788. */
  789. static long pvfs2_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
  790. unsigned long args)
  791. {
  792. long ret;
  793. unsigned long arg = args;
  794. /* Check for properly constructed commands */
  795. ret = check_ioctl_command(cmd);
  796. if (ret < 0)
  797. return ret;
  798. if (cmd == PVFS_DEV_MAP) {
  799. /*
  800. * convert the arguments to what we expect internally
  801. * in kernel space
  802. */
  803. arg = translate_dev_map26(args, &ret);
  804. if (ret < 0) {
  805. gossip_err("Could not translate dev map\n");
  806. return ret;
  807. }
  808. }
  809. /* no other ioctl requires translation */
  810. return dispatch_ioctl_command(cmd, arg);
  811. }
  812. #endif /* CONFIG_COMPAT is in .config */
  813. /*
  814. * The following two ioctl32 functions had been refactored into the above
  815. * CONFIG_COMPAT ifdef, but that was an over simplification that was
  816. * not noticed until we tried to compile on power pc...
  817. */
  818. #if (defined(CONFIG_COMPAT) && !defined(HAVE_REGISTER_IOCTL32_CONVERSION)) || !defined(CONFIG_COMPAT)
  819. static int pvfs2_ioctl32_init(void)
  820. {
  821. return 0;
  822. }
  823. static void pvfs2_ioctl32_cleanup(void)
  824. {
  825. return;
  826. }
  827. #endif
  828. /* the assigned character device major number */
  829. static int pvfs2_dev_major;
  830. /*
  831. * Initialize pvfs2 device specific state:
  832. * Must be called at module load time only
  833. */
  834. int pvfs2_dev_init(void)
  835. {
  836. int ret;
  837. /* register the ioctl32 sub-system */
  838. ret = pvfs2_ioctl32_init();
  839. if (ret < 0)
  840. return ret;
  841. /* register pvfs2-req device */
  842. pvfs2_dev_major = register_chrdev(0,
  843. PVFS2_REQDEVICE_NAME,
  844. &pvfs2_devreq_file_operations);
  845. if (pvfs2_dev_major < 0) {
  846. gossip_debug(GOSSIP_DEV_DEBUG,
  847. "Failed to register /dev/%s (error %d)\n",
  848. PVFS2_REQDEVICE_NAME, pvfs2_dev_major);
  849. pvfs2_ioctl32_cleanup();
  850. return pvfs2_dev_major;
  851. }
  852. gossip_debug(GOSSIP_DEV_DEBUG,
  853. "*** /dev/%s character device registered ***\n",
  854. PVFS2_REQDEVICE_NAME);
  855. gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
  856. PVFS2_REQDEVICE_NAME, pvfs2_dev_major);
  857. return 0;
  858. }
  859. void pvfs2_dev_cleanup(void)
  860. {
  861. unregister_chrdev(pvfs2_dev_major, PVFS2_REQDEVICE_NAME);
  862. gossip_debug(GOSSIP_DEV_DEBUG,
  863. "*** /dev/%s character device unregistered ***\n",
  864. PVFS2_REQDEVICE_NAME);
  865. /* unregister the ioctl32 sub-system */
  866. pvfs2_ioctl32_cleanup();
  867. }
  868. static unsigned int pvfs2_devreq_poll(struct file *file,
  869. struct poll_table_struct *poll_table)
  870. {
  871. int poll_revent_mask = 0;
  872. if (open_access_count == 1) {
  873. poll_wait(file, &pvfs2_request_list_waitq, poll_table);
  874. spin_lock(&pvfs2_request_list_lock);
  875. if (!list_empty(&pvfs2_request_list))
  876. poll_revent_mask |= POLL_IN;
  877. spin_unlock(&pvfs2_request_list_lock);
  878. }
  879. return poll_revent_mask;
  880. }
  881. const struct file_operations pvfs2_devreq_file_operations = {
  882. .owner = THIS_MODULE,
  883. .read = pvfs2_devreq_read,
  884. .write_iter = pvfs2_devreq_write_iter,
  885. .open = pvfs2_devreq_open,
  886. .release = pvfs2_devreq_release,
  887. .unlocked_ioctl = pvfs2_devreq_ioctl,
  888. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  889. .compat_ioctl = pvfs2_devreq_compat_ioctl,
  890. #endif
  891. .poll = pvfs2_devreq_poll
  892. };