devorangefs-req.c 23 KB

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