devorangefs-req.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * (C) 2001 Clemson University and The University of Chicago
  4. *
  5. * Changes by Acxiom Corporation to add protocol version to kernel
  6. * communication, Copyright Acxiom Corporation, 2005.
  7. *
  8. * See COPYING in top-level directory.
  9. */
  10. #include "protocol.h"
  11. #include "orangefs-kernel.h"
  12. #include "orangefs-dev-proto.h"
  13. #include "orangefs-bufmap.h"
  14. #include "orangefs-debugfs.h"
  15. #include <linux/debugfs.h>
  16. #include <linux/slab.h>
  17. /* this file implements the /dev/pvfs2-req device node */
  18. uint32_t orangefs_userspace_version;
  19. static int open_access_count;
  20. static DEFINE_MUTEX(devreq_mutex);
  21. #define DUMP_DEVICE_ERROR() \
  22. do { \
  23. gossip_err("*****************************************************\n");\
  24. gossip_err("ORANGEFS Device Error: You cannot open the device file "); \
  25. gossip_err("\n/dev/%s more than once. Please make sure that\nthere " \
  26. "are no ", ORANGEFS_REQDEVICE_NAME); \
  27. gossip_err("instances of a program using this device\ncurrently " \
  28. "running. (You must verify this!)\n"); \
  29. gossip_err("For example, you can use the lsof program as follows:\n");\
  30. gossip_err("'lsof | grep %s' (run this as root)\n", \
  31. ORANGEFS_REQDEVICE_NAME); \
  32. gossip_err(" open_access_count = %d\n", open_access_count); \
  33. gossip_err("*****************************************************\n");\
  34. } while (0)
  35. static int hash_func(__u64 tag, int table_size)
  36. {
  37. return do_div(tag, (unsigned int)table_size);
  38. }
  39. static void orangefs_devreq_add_op(struct orangefs_kernel_op_s *op)
  40. {
  41. int index = hash_func(op->tag, hash_table_size);
  42. list_add_tail(&op->list, &orangefs_htable_ops_in_progress[index]);
  43. }
  44. /*
  45. * find the op with this tag and remove it from the in progress
  46. * hash table.
  47. */
  48. static struct orangefs_kernel_op_s *orangefs_devreq_remove_op(__u64 tag)
  49. {
  50. struct orangefs_kernel_op_s *op, *next;
  51. int index;
  52. index = hash_func(tag, hash_table_size);
  53. spin_lock(&orangefs_htable_ops_in_progress_lock);
  54. list_for_each_entry_safe(op,
  55. next,
  56. &orangefs_htable_ops_in_progress[index],
  57. list) {
  58. if (op->tag == tag && !op_state_purged(op) &&
  59. !op_state_given_up(op)) {
  60. list_del_init(&op->list);
  61. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  62. return op;
  63. }
  64. }
  65. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  66. return NULL;
  67. }
  68. /* Returns whether any FS are still pending remounted */
  69. static int mark_all_pending_mounts(void)
  70. {
  71. int unmounted = 1;
  72. struct orangefs_sb_info_s *orangefs_sb = NULL;
  73. spin_lock(&orangefs_superblocks_lock);
  74. list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
  75. /* All of these file system require a remount */
  76. orangefs_sb->mount_pending = 1;
  77. unmounted = 0;
  78. }
  79. spin_unlock(&orangefs_superblocks_lock);
  80. return unmounted;
  81. }
  82. /*
  83. * Determine if a given file system needs to be remounted or not
  84. * Returns -1 on error
  85. * 0 if already mounted
  86. * 1 if needs remount
  87. */
  88. static int fs_mount_pending(__s32 fsid)
  89. {
  90. int mount_pending = -1;
  91. struct orangefs_sb_info_s *orangefs_sb = NULL;
  92. spin_lock(&orangefs_superblocks_lock);
  93. list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
  94. if (orangefs_sb->fs_id == fsid) {
  95. mount_pending = orangefs_sb->mount_pending;
  96. break;
  97. }
  98. }
  99. spin_unlock(&orangefs_superblocks_lock);
  100. return mount_pending;
  101. }
  102. static int orangefs_devreq_open(struct inode *inode, struct file *file)
  103. {
  104. int ret = -EINVAL;
  105. /* in order to ensure that the filesystem driver sees correct UIDs */
  106. if (file->f_cred->user_ns != &init_user_ns) {
  107. gossip_err("%s: device cannot be opened outside init_user_ns\n",
  108. __func__);
  109. goto out;
  110. }
  111. if (!(file->f_flags & O_NONBLOCK)) {
  112. gossip_err("%s: device cannot be opened in blocking mode\n",
  113. __func__);
  114. goto out;
  115. }
  116. ret = -EACCES;
  117. gossip_debug(GOSSIP_DEV_DEBUG, "client-core: opening device\n");
  118. mutex_lock(&devreq_mutex);
  119. if (open_access_count == 0) {
  120. open_access_count = 1;
  121. ret = 0;
  122. } else {
  123. DUMP_DEVICE_ERROR();
  124. }
  125. mutex_unlock(&devreq_mutex);
  126. out:
  127. gossip_debug(GOSSIP_DEV_DEBUG,
  128. "pvfs2-client-core: open device complete (ret = %d)\n",
  129. ret);
  130. return ret;
  131. }
  132. /* Function for read() callers into the device */
  133. static ssize_t orangefs_devreq_read(struct file *file,
  134. char __user *buf,
  135. size_t count, loff_t *offset)
  136. {
  137. struct orangefs_kernel_op_s *op, *temp;
  138. __s32 proto_ver = ORANGEFS_KERNEL_PROTO_VERSION;
  139. static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
  140. struct orangefs_kernel_op_s *cur_op;
  141. unsigned long ret;
  142. /* We do not support blocking IO. */
  143. if (!(file->f_flags & O_NONBLOCK)) {
  144. gossip_err("%s: blocking read from client-core.\n",
  145. __func__);
  146. return -EINVAL;
  147. }
  148. /*
  149. * The client will do an ioctl to find MAX_DEV_REQ_UPSIZE, then
  150. * always read with that size buffer.
  151. */
  152. if (count != MAX_DEV_REQ_UPSIZE) {
  153. gossip_err("orangefs: client-core tried to read wrong size\n");
  154. return -EINVAL;
  155. }
  156. /* Check for an empty list before locking. */
  157. if (list_empty(&orangefs_request_list))
  158. return -EAGAIN;
  159. restart:
  160. cur_op = NULL;
  161. /* Get next op (if any) from top of list. */
  162. spin_lock(&orangefs_request_list_lock);
  163. list_for_each_entry_safe(op, temp, &orangefs_request_list, list) {
  164. __s32 fsid;
  165. /* This lock is held past the end of the loop when we break. */
  166. spin_lock(&op->lock);
  167. if (unlikely(op_state_purged(op) || op_state_given_up(op))) {
  168. spin_unlock(&op->lock);
  169. continue;
  170. }
  171. fsid = fsid_of_op(op);
  172. if (fsid != ORANGEFS_FS_ID_NULL) {
  173. int ret;
  174. /* Skip ops whose filesystem needs to be mounted. */
  175. ret = fs_mount_pending(fsid);
  176. if (ret == 1) {
  177. gossip_debug(GOSSIP_DEV_DEBUG,
  178. "%s: mount pending, skipping op tag "
  179. "%llu %s\n",
  180. __func__,
  181. llu(op->tag),
  182. get_opname_string(op));
  183. spin_unlock(&op->lock);
  184. continue;
  185. /*
  186. * Skip ops whose filesystem we don't know about unless
  187. * it is being mounted or unmounted. It is possible for
  188. * a filesystem we don't know about to be unmounted if
  189. * it fails to mount in the kernel after userspace has
  190. * been sent the mount request.
  191. */
  192. /* XXX: is there a better way to detect this? */
  193. } else if (ret == -1 &&
  194. !(op->upcall.type ==
  195. ORANGEFS_VFS_OP_FS_MOUNT ||
  196. op->upcall.type ==
  197. ORANGEFS_VFS_OP_GETATTR ||
  198. op->upcall.type ==
  199. ORANGEFS_VFS_OP_FS_UMOUNT)) {
  200. gossip_debug(GOSSIP_DEV_DEBUG,
  201. "orangefs: skipping op tag %llu %s\n",
  202. llu(op->tag), get_opname_string(op));
  203. gossip_err(
  204. "orangefs: ERROR: fs_mount_pending %d\n",
  205. fsid);
  206. spin_unlock(&op->lock);
  207. continue;
  208. }
  209. }
  210. /*
  211. * Either this op does not pertain to a filesystem, is mounting
  212. * a filesystem, or pertains to a mounted filesystem. Let it
  213. * through.
  214. */
  215. cur_op = op;
  216. break;
  217. }
  218. /*
  219. * At this point we either have a valid op and can continue or have not
  220. * found an op and must ask the client to try again later.
  221. */
  222. if (!cur_op) {
  223. spin_unlock(&orangefs_request_list_lock);
  224. return -EAGAIN;
  225. }
  226. gossip_debug(GOSSIP_DEV_DEBUG, "%s: reading op tag %llu %s\n",
  227. __func__,
  228. llu(cur_op->tag),
  229. get_opname_string(cur_op));
  230. /*
  231. * Such an op should never be on the list in the first place. If so, we
  232. * will abort.
  233. */
  234. if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
  235. gossip_err("orangefs: ERROR: Current op already queued.\n");
  236. list_del_init(&cur_op->list);
  237. spin_unlock(&cur_op->lock);
  238. spin_unlock(&orangefs_request_list_lock);
  239. return -EAGAIN;
  240. }
  241. list_del_init(&cur_op->list);
  242. spin_unlock(&orangefs_request_list_lock);
  243. spin_unlock(&cur_op->lock);
  244. /* Push the upcall out. */
  245. ret = copy_to_user(buf, &proto_ver, sizeof(__s32));
  246. if (ret != 0)
  247. goto error;
  248. ret = copy_to_user(buf + sizeof(__s32), &magic, sizeof(__s32));
  249. if (ret != 0)
  250. goto error;
  251. ret = copy_to_user(buf + 2 * sizeof(__s32),
  252. &cur_op->tag,
  253. sizeof(__u64));
  254. if (ret != 0)
  255. goto error;
  256. ret = copy_to_user(buf + 2 * sizeof(__s32) + sizeof(__u64),
  257. &cur_op->upcall,
  258. sizeof(struct orangefs_upcall_s));
  259. if (ret != 0)
  260. goto error;
  261. spin_lock(&orangefs_htable_ops_in_progress_lock);
  262. spin_lock(&cur_op->lock);
  263. if (unlikely(op_state_given_up(cur_op))) {
  264. spin_unlock(&cur_op->lock);
  265. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  266. complete(&cur_op->waitq);
  267. goto restart;
  268. }
  269. /*
  270. * Set the operation to be in progress and move it between lists since
  271. * it has been sent to the client.
  272. */
  273. set_op_state_inprogress(cur_op);
  274. gossip_debug(GOSSIP_DEV_DEBUG,
  275. "%s: 1 op:%s: op_state:%d: process:%s:\n",
  276. __func__,
  277. get_opname_string(cur_op),
  278. cur_op->op_state,
  279. current->comm);
  280. orangefs_devreq_add_op(cur_op);
  281. spin_unlock(&cur_op->lock);
  282. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  283. /* The client only asks to read one size buffer. */
  284. return MAX_DEV_REQ_UPSIZE;
  285. error:
  286. /*
  287. * We were unable to copy the op data to the client. Put the op back in
  288. * list. If client has crashed, the op will be purged later when the
  289. * device is released.
  290. */
  291. gossip_err("orangefs: Failed to copy data to user space\n");
  292. spin_lock(&orangefs_request_list_lock);
  293. spin_lock(&cur_op->lock);
  294. if (likely(!op_state_given_up(cur_op))) {
  295. set_op_state_waiting(cur_op);
  296. gossip_debug(GOSSIP_DEV_DEBUG,
  297. "%s: 2 op:%s: op_state:%d: process:%s:\n",
  298. __func__,
  299. get_opname_string(cur_op),
  300. cur_op->op_state,
  301. current->comm);
  302. list_add(&cur_op->list, &orangefs_request_list);
  303. spin_unlock(&cur_op->lock);
  304. } else {
  305. spin_unlock(&cur_op->lock);
  306. complete(&cur_op->waitq);
  307. }
  308. spin_unlock(&orangefs_request_list_lock);
  309. return -EFAULT;
  310. }
  311. /*
  312. * Function for writev() callers into the device.
  313. *
  314. * Userspace should have written:
  315. * - __u32 version
  316. * - __u32 magic
  317. * - __u64 tag
  318. * - struct orangefs_downcall_s
  319. * - trailer buffer (in the case of READDIR operations)
  320. */
  321. static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
  322. struct iov_iter *iter)
  323. {
  324. ssize_t ret;
  325. struct orangefs_kernel_op_s *op = NULL;
  326. struct {
  327. __u32 version;
  328. __u32 magic;
  329. __u64 tag;
  330. } head;
  331. int total = ret = iov_iter_count(iter);
  332. int downcall_size = sizeof(struct orangefs_downcall_s);
  333. int head_size = sizeof(head);
  334. gossip_debug(GOSSIP_DEV_DEBUG, "%s: total:%d: ret:%zd:\n",
  335. __func__,
  336. total,
  337. ret);
  338. if (total < MAX_DEV_REQ_DOWNSIZE) {
  339. gossip_err("%s: total:%d: must be at least:%u:\n",
  340. __func__,
  341. total,
  342. (unsigned int) MAX_DEV_REQ_DOWNSIZE);
  343. return -EFAULT;
  344. }
  345. if (!copy_from_iter_full(&head, head_size, iter)) {
  346. gossip_err("%s: failed to copy head.\n", __func__);
  347. return -EFAULT;
  348. }
  349. if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
  350. gossip_err("%s: userspace claims version"
  351. "%d, minimum version required: %d.\n",
  352. __func__,
  353. head.version,
  354. ORANGEFS_MINIMUM_USERSPACE_VERSION);
  355. return -EPROTO;
  356. }
  357. if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
  358. gossip_err("Error: Device magic number does not match.\n");
  359. return -EPROTO;
  360. }
  361. if (!orangefs_userspace_version) {
  362. orangefs_userspace_version = head.version;
  363. } else if (orangefs_userspace_version != head.version) {
  364. gossip_err("Error: userspace version changes\n");
  365. return -EPROTO;
  366. }
  367. /* remove the op from the in progress hash table */
  368. op = orangefs_devreq_remove_op(head.tag);
  369. if (!op) {
  370. gossip_debug(GOSSIP_DEV_DEBUG,
  371. "%s: No one's waiting for tag %llu\n",
  372. __func__, llu(head.tag));
  373. return ret;
  374. }
  375. if (!copy_from_iter_full(&op->downcall, downcall_size, iter)) {
  376. gossip_err("%s: failed to copy downcall.\n", __func__);
  377. goto Efault;
  378. }
  379. if (op->downcall.status)
  380. goto wakeup;
  381. /*
  382. * We've successfully peeled off the head and the downcall.
  383. * Something has gone awry if total doesn't equal the
  384. * sum of head_size, downcall_size and trailer_size.
  385. */
  386. if ((head_size + downcall_size + op->downcall.trailer_size) != total) {
  387. gossip_err("%s: funky write, head_size:%d"
  388. ": downcall_size:%d: trailer_size:%lld"
  389. ": total size:%d:\n",
  390. __func__,
  391. head_size,
  392. downcall_size,
  393. op->downcall.trailer_size,
  394. total);
  395. goto Efault;
  396. }
  397. /* Only READDIR operations should have trailers. */
  398. if ((op->downcall.type != ORANGEFS_VFS_OP_READDIR) &&
  399. (op->downcall.trailer_size != 0)) {
  400. gossip_err("%s: %x operation with trailer.",
  401. __func__,
  402. op->downcall.type);
  403. goto Efault;
  404. }
  405. /* READDIR operations should always have trailers. */
  406. if ((op->downcall.type == ORANGEFS_VFS_OP_READDIR) &&
  407. (op->downcall.trailer_size == 0)) {
  408. gossip_err("%s: %x operation with no trailer.",
  409. __func__,
  410. op->downcall.type);
  411. goto Efault;
  412. }
  413. if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
  414. goto wakeup;
  415. op->downcall.trailer_buf = vzalloc(op->downcall.trailer_size);
  416. if (!op->downcall.trailer_buf)
  417. goto Enomem;
  418. if (!copy_from_iter_full(op->downcall.trailer_buf,
  419. op->downcall.trailer_size, iter)) {
  420. gossip_err("%s: failed to copy trailer.\n", __func__);
  421. vfree(op->downcall.trailer_buf);
  422. goto Efault;
  423. }
  424. wakeup:
  425. /*
  426. * Return to vfs waitqueue, and back to service_operation
  427. * through wait_for_matching_downcall.
  428. */
  429. spin_lock(&op->lock);
  430. if (unlikely(op_is_cancel(op))) {
  431. spin_unlock(&op->lock);
  432. put_cancel(op);
  433. } else if (unlikely(op_state_given_up(op))) {
  434. spin_unlock(&op->lock);
  435. complete(&op->waitq);
  436. } else {
  437. set_op_state_serviced(op);
  438. gossip_debug(GOSSIP_DEV_DEBUG,
  439. "%s: op:%s: op_state:%d: process:%s:\n",
  440. __func__,
  441. get_opname_string(op),
  442. op->op_state,
  443. current->comm);
  444. spin_unlock(&op->lock);
  445. }
  446. return ret;
  447. Efault:
  448. op->downcall.status = -(ORANGEFS_ERROR_BIT | 9);
  449. ret = -EFAULT;
  450. goto wakeup;
  451. Enomem:
  452. op->downcall.status = -(ORANGEFS_ERROR_BIT | 8);
  453. ret = -ENOMEM;
  454. goto wakeup;
  455. }
  456. /*
  457. * NOTE: gets called when the last reference to this device is dropped.
  458. * Using the open_access_count variable, we enforce a reference count
  459. * on this file so that it can be opened by only one process at a time.
  460. * the devreq_mutex is used to make sure all i/o has completed
  461. * before we call orangefs_bufmap_finalize, and similar such tricky
  462. * situations
  463. */
  464. static int orangefs_devreq_release(struct inode *inode, struct file *file)
  465. {
  466. int unmounted = 0;
  467. gossip_debug(GOSSIP_DEV_DEBUG,
  468. "%s:pvfs2-client-core: exiting, closing device\n",
  469. __func__);
  470. mutex_lock(&devreq_mutex);
  471. orangefs_bufmap_finalize();
  472. open_access_count = -1;
  473. unmounted = mark_all_pending_mounts();
  474. gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
  475. (unmounted ? "UNMOUNTED" : "MOUNTED"));
  476. purge_waiting_ops();
  477. purge_inprogress_ops();
  478. orangefs_bufmap_run_down();
  479. gossip_debug(GOSSIP_DEV_DEBUG,
  480. "pvfs2-client-core: device close complete\n");
  481. open_access_count = 0;
  482. orangefs_userspace_version = 0;
  483. mutex_unlock(&devreq_mutex);
  484. return 0;
  485. }
  486. int is_daemon_in_service(void)
  487. {
  488. int in_service;
  489. /*
  490. * What this function does is checks if client-core is alive
  491. * based on the access count we maintain on the device.
  492. */
  493. mutex_lock(&devreq_mutex);
  494. in_service = open_access_count == 1 ? 0 : -EIO;
  495. mutex_unlock(&devreq_mutex);
  496. return in_service;
  497. }
  498. bool __is_daemon_in_service(void)
  499. {
  500. return open_access_count == 1;
  501. }
  502. static inline long check_ioctl_command(unsigned int command)
  503. {
  504. /* Check for valid ioctl codes */
  505. if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
  506. gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
  507. command,
  508. _IOC_TYPE(command),
  509. ORANGEFS_DEV_MAGIC);
  510. return -EINVAL;
  511. }
  512. /* and valid ioctl commands */
  513. if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
  514. gossip_err("Invalid ioctl command number [%d >= %d]\n",
  515. _IOC_NR(command), ORANGEFS_DEV_MAXNR);
  516. return -ENOIOCTLCMD;
  517. }
  518. return 0;
  519. }
  520. static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
  521. {
  522. static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
  523. static __s32 max_up_size = MAX_DEV_REQ_UPSIZE;
  524. static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE;
  525. struct ORANGEFS_dev_map_desc user_desc;
  526. int ret = 0;
  527. int upstream_kmod = 1;
  528. struct orangefs_sb_info_s *orangefs_sb;
  529. /* mtmoore: add locking here */
  530. switch (command) {
  531. case ORANGEFS_DEV_GET_MAGIC:
  532. return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
  533. -EIO :
  534. 0);
  535. case ORANGEFS_DEV_GET_MAX_UPSIZE:
  536. return ((put_user(max_up_size,
  537. (__s32 __user *) arg) == -EFAULT) ?
  538. -EIO :
  539. 0);
  540. case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
  541. return ((put_user(max_down_size,
  542. (__s32 __user *) arg) == -EFAULT) ?
  543. -EIO :
  544. 0);
  545. case ORANGEFS_DEV_MAP:
  546. ret = copy_from_user(&user_desc,
  547. (struct ORANGEFS_dev_map_desc __user *)
  548. arg,
  549. sizeof(struct ORANGEFS_dev_map_desc));
  550. /* WTF -EIO and not -EFAULT? */
  551. return ret ? -EIO : orangefs_bufmap_initialize(&user_desc);
  552. case ORANGEFS_DEV_REMOUNT_ALL:
  553. gossip_debug(GOSSIP_DEV_DEBUG,
  554. "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
  555. __func__);
  556. /*
  557. * remount all mounted orangefs volumes to regain the lost
  558. * dynamic mount tables (if any) -- NOTE: this is done
  559. * without keeping the superblock list locked due to the
  560. * upcall/downcall waiting. also, the request mutex is
  561. * used to ensure that no operations will be serviced until
  562. * all of the remounts are serviced (to avoid ops between
  563. * mounts to fail)
  564. */
  565. ret = mutex_lock_interruptible(&orangefs_request_mutex);
  566. if (ret < 0)
  567. return ret;
  568. gossip_debug(GOSSIP_DEV_DEBUG,
  569. "%s: priority remount in progress\n",
  570. __func__);
  571. spin_lock(&orangefs_superblocks_lock);
  572. list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
  573. /*
  574. * We have to drop the spinlock, so entries can be
  575. * removed. They can't be freed, though, so we just
  576. * keep the forward pointers and zero the back ones -
  577. * that way we can get to the rest of the list.
  578. */
  579. if (!orangefs_sb->list.prev)
  580. continue;
  581. gossip_debug(GOSSIP_DEV_DEBUG,
  582. "%s: Remounting SB %p\n",
  583. __func__,
  584. orangefs_sb);
  585. spin_unlock(&orangefs_superblocks_lock);
  586. ret = orangefs_remount(orangefs_sb);
  587. spin_lock(&orangefs_superblocks_lock);
  588. if (ret) {
  589. gossip_debug(GOSSIP_DEV_DEBUG,
  590. "SB %p remount failed\n",
  591. orangefs_sb);
  592. break;
  593. }
  594. }
  595. spin_unlock(&orangefs_superblocks_lock);
  596. gossip_debug(GOSSIP_DEV_DEBUG,
  597. "%s: priority remount complete\n",
  598. __func__);
  599. mutex_unlock(&orangefs_request_mutex);
  600. return ret;
  601. case ORANGEFS_DEV_UPSTREAM:
  602. ret = copy_to_user((void __user *)arg,
  603. &upstream_kmod,
  604. sizeof(upstream_kmod));
  605. if (ret != 0)
  606. return -EIO;
  607. else
  608. return ret;
  609. case ORANGEFS_DEV_CLIENT_MASK:
  610. return orangefs_debugfs_new_client_mask((void __user *)arg);
  611. case ORANGEFS_DEV_CLIENT_STRING:
  612. return orangefs_debugfs_new_client_string((void __user *)arg);
  613. case ORANGEFS_DEV_DEBUG:
  614. return orangefs_debugfs_new_debug((void __user *)arg);
  615. default:
  616. return -ENOIOCTLCMD;
  617. }
  618. return -ENOIOCTLCMD;
  619. }
  620. static long orangefs_devreq_ioctl(struct file *file,
  621. unsigned int command, unsigned long arg)
  622. {
  623. long ret;
  624. /* Check for properly constructed commands */
  625. ret = check_ioctl_command(command);
  626. if (ret < 0)
  627. return (int)ret;
  628. return (int)dispatch_ioctl_command(command, arg);
  629. }
  630. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  631. /* Compat structure for the ORANGEFS_DEV_MAP ioctl */
  632. struct ORANGEFS_dev_map_desc32 {
  633. compat_uptr_t ptr;
  634. __s32 total_size;
  635. __s32 size;
  636. __s32 count;
  637. };
  638. /*
  639. * 32 bit user-space apps' ioctl handlers when kernel modules
  640. * is compiled as a 64 bit one
  641. */
  642. static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
  643. unsigned long args)
  644. {
  645. long ret;
  646. /* Check for properly constructed commands */
  647. ret = check_ioctl_command(cmd);
  648. if (ret < 0)
  649. return ret;
  650. if (cmd == ORANGEFS_DEV_MAP) {
  651. struct ORANGEFS_dev_map_desc desc;
  652. struct ORANGEFS_dev_map_desc32 d32;
  653. if (copy_from_user(&d32, (void __user *)args, sizeof(d32)))
  654. return -EFAULT;
  655. desc.ptr = compat_ptr(d32.ptr);
  656. desc.total_size = d32.total_size;
  657. desc.size = d32.size;
  658. desc.count = d32.count;
  659. return orangefs_bufmap_initialize(&desc);
  660. }
  661. /* no other ioctl requires translation */
  662. return dispatch_ioctl_command(cmd, args);
  663. }
  664. #endif /* CONFIG_COMPAT is in .config */
  665. static __poll_t orangefs_devreq_poll(struct file *file,
  666. struct poll_table_struct *poll_table)
  667. {
  668. __poll_t poll_revent_mask = 0;
  669. poll_wait(file, &orangefs_request_list_waitq, poll_table);
  670. if (!list_empty(&orangefs_request_list))
  671. poll_revent_mask |= EPOLLIN;
  672. return poll_revent_mask;
  673. }
  674. /* the assigned character device major number */
  675. static int orangefs_dev_major;
  676. static const struct file_operations orangefs_devreq_file_operations = {
  677. .owner = THIS_MODULE,
  678. .read = orangefs_devreq_read,
  679. .write_iter = orangefs_devreq_write_iter,
  680. .open = orangefs_devreq_open,
  681. .release = orangefs_devreq_release,
  682. .unlocked_ioctl = orangefs_devreq_ioctl,
  683. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  684. .compat_ioctl = orangefs_devreq_compat_ioctl,
  685. #endif
  686. .poll = orangefs_devreq_poll
  687. };
  688. /*
  689. * Initialize orangefs device specific state:
  690. * Must be called at module load time only
  691. */
  692. int orangefs_dev_init(void)
  693. {
  694. /* register orangefs-req device */
  695. orangefs_dev_major = register_chrdev(0,
  696. ORANGEFS_REQDEVICE_NAME,
  697. &orangefs_devreq_file_operations);
  698. if (orangefs_dev_major < 0) {
  699. gossip_debug(GOSSIP_DEV_DEBUG,
  700. "Failed to register /dev/%s (error %d)\n",
  701. ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
  702. return orangefs_dev_major;
  703. }
  704. gossip_debug(GOSSIP_DEV_DEBUG,
  705. "*** /dev/%s character device registered ***\n",
  706. ORANGEFS_REQDEVICE_NAME);
  707. gossip_debug(GOSSIP_DEV_DEBUG, "'mknod /dev/%s c %d 0'.\n",
  708. ORANGEFS_REQDEVICE_NAME, orangefs_dev_major);
  709. return 0;
  710. }
  711. void orangefs_dev_cleanup(void)
  712. {
  713. unregister_chrdev(orangefs_dev_major, ORANGEFS_REQDEVICE_NAME);
  714. gossip_debug(GOSSIP_DEV_DEBUG,
  715. "*** /dev/%s character device unregistered ***\n",
  716. ORANGEFS_REQDEVICE_NAME);
  717. }