devorangefs-req.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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.
  183. */
  184. /* XXX: is there a better way to detect this? */
  185. } else if (ret == -1 &&
  186. !(op->upcall.type ==
  187. ORANGEFS_VFS_OP_FS_MOUNT ||
  188. op->upcall.type ==
  189. ORANGEFS_VFS_OP_GETATTR)) {
  190. gossip_debug(GOSSIP_DEV_DEBUG,
  191. "orangefs: skipping op tag %llu %s\n",
  192. llu(op->tag), get_opname_string(op));
  193. gossip_err(
  194. "orangefs: ERROR: fs_mount_pending %d\n",
  195. fsid);
  196. spin_unlock(&op->lock);
  197. continue;
  198. }
  199. }
  200. /*
  201. * Either this op does not pertain to a filesystem, is mounting
  202. * a filesystem, or pertains to a mounted filesystem. Let it
  203. * through.
  204. */
  205. cur_op = op;
  206. break;
  207. }
  208. /*
  209. * At this point we either have a valid op and can continue or have not
  210. * found an op and must ask the client to try again later.
  211. */
  212. if (!cur_op) {
  213. spin_unlock(&orangefs_request_list_lock);
  214. return -EAGAIN;
  215. }
  216. gossip_debug(GOSSIP_DEV_DEBUG, "%s: reading op tag %llu %s\n",
  217. __func__,
  218. llu(cur_op->tag),
  219. get_opname_string(cur_op));
  220. /*
  221. * Such an op should never be on the list in the first place. If so, we
  222. * will abort.
  223. */
  224. if (op_state_in_progress(cur_op) || op_state_serviced(cur_op)) {
  225. gossip_err("orangefs: ERROR: Current op already queued.\n");
  226. list_del_init(&cur_op->list);
  227. spin_unlock(&cur_op->lock);
  228. spin_unlock(&orangefs_request_list_lock);
  229. return -EAGAIN;
  230. }
  231. list_del_init(&cur_op->list);
  232. spin_unlock(&orangefs_request_list_lock);
  233. spin_unlock(&cur_op->lock);
  234. /* Push the upcall out. */
  235. ret = copy_to_user(buf, &proto_ver, sizeof(__s32));
  236. if (ret != 0)
  237. goto error;
  238. ret = copy_to_user(buf+sizeof(__s32), &magic, sizeof(__s32));
  239. if (ret != 0)
  240. goto error;
  241. ret = copy_to_user(buf+2 * sizeof(__s32), &cur_op->tag, sizeof(__u64));
  242. if (ret != 0)
  243. goto error;
  244. ret = copy_to_user(buf+2*sizeof(__s32)+sizeof(__u64), &cur_op->upcall,
  245. sizeof(struct orangefs_upcall_s));
  246. if (ret != 0)
  247. goto error;
  248. spin_lock(&orangefs_htable_ops_in_progress_lock);
  249. spin_lock(&cur_op->lock);
  250. if (unlikely(op_state_given_up(cur_op))) {
  251. spin_unlock(&cur_op->lock);
  252. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  253. complete(&cur_op->waitq);
  254. goto restart;
  255. }
  256. /*
  257. * Set the operation to be in progress and move it between lists since
  258. * it has been sent to the client.
  259. */
  260. set_op_state_inprogress(cur_op);
  261. gossip_debug(GOSSIP_DEV_DEBUG,
  262. "%s: 1 op:%s: op_state:%d: process:%s:\n",
  263. __func__,
  264. get_opname_string(cur_op),
  265. cur_op->op_state,
  266. current->comm);
  267. orangefs_devreq_add_op(cur_op);
  268. spin_unlock(&cur_op->lock);
  269. spin_unlock(&orangefs_htable_ops_in_progress_lock);
  270. /* The client only asks to read one size buffer. */
  271. return MAX_DEV_REQ_UPSIZE;
  272. error:
  273. /*
  274. * We were unable to copy the op data to the client. Put the op back in
  275. * list. If client has crashed, the op will be purged later when the
  276. * device is released.
  277. */
  278. gossip_err("orangefs: Failed to copy data to user space\n");
  279. spin_lock(&orangefs_request_list_lock);
  280. spin_lock(&cur_op->lock);
  281. if (likely(!op_state_given_up(cur_op))) {
  282. set_op_state_waiting(cur_op);
  283. gossip_debug(GOSSIP_DEV_DEBUG,
  284. "%s: 2 op:%s: op_state:%d: process:%s:\n",
  285. __func__,
  286. get_opname_string(cur_op),
  287. cur_op->op_state,
  288. current->comm);
  289. list_add(&cur_op->list, &orangefs_request_list);
  290. spin_unlock(&cur_op->lock);
  291. } else {
  292. spin_unlock(&cur_op->lock);
  293. complete(&cur_op->waitq);
  294. }
  295. spin_unlock(&orangefs_request_list_lock);
  296. return -EFAULT;
  297. }
  298. /*
  299. * Function for writev() callers into the device.
  300. *
  301. * Userspace should have written:
  302. * - __u32 version
  303. * - __u32 magic
  304. * - __u64 tag
  305. * - struct orangefs_downcall_s
  306. * - trailer buffer (in the case of READDIR operations)
  307. */
  308. static ssize_t orangefs_devreq_write_iter(struct kiocb *iocb,
  309. struct iov_iter *iter)
  310. {
  311. ssize_t ret;
  312. struct orangefs_kernel_op_s *op = NULL;
  313. struct {
  314. __u32 version;
  315. __u32 magic;
  316. __u64 tag;
  317. } head;
  318. int total = ret = iov_iter_count(iter);
  319. int downcall_size = sizeof(struct orangefs_downcall_s);
  320. int head_size = sizeof(head);
  321. gossip_debug(GOSSIP_DEV_DEBUG, "%s: total:%d: ret:%zd:\n",
  322. __func__,
  323. total,
  324. ret);
  325. if (total < MAX_DEV_REQ_DOWNSIZE) {
  326. gossip_err("%s: total:%d: must be at least:%u:\n",
  327. __func__,
  328. total,
  329. (unsigned int) MAX_DEV_REQ_DOWNSIZE);
  330. return -EFAULT;
  331. }
  332. if (!copy_from_iter_full(&head, head_size, iter)) {
  333. gossip_err("%s: failed to copy head.\n", __func__);
  334. return -EFAULT;
  335. }
  336. if (head.version < ORANGEFS_MINIMUM_USERSPACE_VERSION) {
  337. gossip_err("%s: userspace claims version"
  338. "%d, minimum version required: %d.\n",
  339. __func__,
  340. head.version,
  341. ORANGEFS_MINIMUM_USERSPACE_VERSION);
  342. return -EPROTO;
  343. }
  344. if (head.magic != ORANGEFS_DEVREQ_MAGIC) {
  345. gossip_err("Error: Device magic number does not match.\n");
  346. return -EPROTO;
  347. }
  348. if (!orangefs_userspace_version) {
  349. orangefs_userspace_version = head.version;
  350. } else if (orangefs_userspace_version != head.version) {
  351. gossip_err("Error: userspace version changes\n");
  352. return -EPROTO;
  353. }
  354. /* remove the op from the in progress hash table */
  355. op = orangefs_devreq_remove_op(head.tag);
  356. if (!op) {
  357. gossip_err("WARNING: No one's waiting for tag %llu\n",
  358. llu(head.tag));
  359. return ret;
  360. }
  361. if (!copy_from_iter_full(&op->downcall, downcall_size, iter)) {
  362. gossip_err("%s: failed to copy downcall.\n", __func__);
  363. goto Efault;
  364. }
  365. if (op->downcall.status)
  366. goto wakeup;
  367. /*
  368. * We've successfully peeled off the head and the downcall.
  369. * Something has gone awry if total doesn't equal the
  370. * sum of head_size, downcall_size and trailer_size.
  371. */
  372. if ((head_size + downcall_size + op->downcall.trailer_size) != total) {
  373. gossip_err("%s: funky write, head_size:%d"
  374. ": downcall_size:%d: trailer_size:%lld"
  375. ": total size:%d:\n",
  376. __func__,
  377. head_size,
  378. downcall_size,
  379. op->downcall.trailer_size,
  380. total);
  381. goto Efault;
  382. }
  383. /* Only READDIR operations should have trailers. */
  384. if ((op->downcall.type != ORANGEFS_VFS_OP_READDIR) &&
  385. (op->downcall.trailer_size != 0)) {
  386. gossip_err("%s: %x operation with trailer.",
  387. __func__,
  388. op->downcall.type);
  389. goto Efault;
  390. }
  391. /* READDIR operations should always have trailers. */
  392. if ((op->downcall.type == ORANGEFS_VFS_OP_READDIR) &&
  393. (op->downcall.trailer_size == 0)) {
  394. gossip_err("%s: %x operation with no trailer.",
  395. __func__,
  396. op->downcall.type);
  397. goto Efault;
  398. }
  399. if (op->downcall.type != ORANGEFS_VFS_OP_READDIR)
  400. goto wakeup;
  401. op->downcall.trailer_buf =
  402. vmalloc(op->downcall.trailer_size);
  403. if (op->downcall.trailer_buf == NULL) {
  404. gossip_err("%s: failed trailer vmalloc.\n",
  405. __func__);
  406. goto Enomem;
  407. }
  408. memset(op->downcall.trailer_buf, 0, op->downcall.trailer_size);
  409. if (!copy_from_iter_full(op->downcall.trailer_buf,
  410. op->downcall.trailer_size, iter)) {
  411. gossip_err("%s: failed to copy trailer.\n", __func__);
  412. vfree(op->downcall.trailer_buf);
  413. goto Efault;
  414. }
  415. wakeup:
  416. /*
  417. * Return to vfs waitqueue, and back to service_operation
  418. * through wait_for_matching_downcall.
  419. */
  420. spin_lock(&op->lock);
  421. if (unlikely(op_is_cancel(op))) {
  422. spin_unlock(&op->lock);
  423. put_cancel(op);
  424. } else if (unlikely(op_state_given_up(op))) {
  425. spin_unlock(&op->lock);
  426. complete(&op->waitq);
  427. } else {
  428. set_op_state_serviced(op);
  429. gossip_debug(GOSSIP_DEV_DEBUG,
  430. "%s: op:%s: op_state:%d: process:%s:\n",
  431. __func__,
  432. get_opname_string(op),
  433. op->op_state,
  434. current->comm);
  435. spin_unlock(&op->lock);
  436. }
  437. return ret;
  438. Efault:
  439. op->downcall.status = -(ORANGEFS_ERROR_BIT | 9);
  440. ret = -EFAULT;
  441. goto wakeup;
  442. Enomem:
  443. op->downcall.status = -(ORANGEFS_ERROR_BIT | 8);
  444. ret = -ENOMEM;
  445. goto wakeup;
  446. }
  447. /*
  448. * NOTE: gets called when the last reference to this device is dropped.
  449. * Using the open_access_count variable, we enforce a reference count
  450. * on this file so that it can be opened by only one process at a time.
  451. * the devreq_mutex is used to make sure all i/o has completed
  452. * before we call orangefs_bufmap_finalize, and similar such tricky
  453. * situations
  454. */
  455. static int orangefs_devreq_release(struct inode *inode, struct file *file)
  456. {
  457. int unmounted = 0;
  458. gossip_debug(GOSSIP_DEV_DEBUG,
  459. "%s:pvfs2-client-core: exiting, closing device\n",
  460. __func__);
  461. mutex_lock(&devreq_mutex);
  462. orangefs_bufmap_finalize();
  463. open_access_count = -1;
  464. unmounted = mark_all_pending_mounts();
  465. gossip_debug(GOSSIP_DEV_DEBUG, "ORANGEFS Device Close: Filesystem(s) %s\n",
  466. (unmounted ? "UNMOUNTED" : "MOUNTED"));
  467. purge_waiting_ops();
  468. purge_inprogress_ops();
  469. orangefs_bufmap_run_down();
  470. gossip_debug(GOSSIP_DEV_DEBUG,
  471. "pvfs2-client-core: device close complete\n");
  472. open_access_count = 0;
  473. orangefs_userspace_version = 0;
  474. mutex_unlock(&devreq_mutex);
  475. return 0;
  476. }
  477. int is_daemon_in_service(void)
  478. {
  479. int in_service;
  480. /*
  481. * What this function does is checks if client-core is alive
  482. * based on the access count we maintain on the device.
  483. */
  484. mutex_lock(&devreq_mutex);
  485. in_service = open_access_count == 1 ? 0 : -EIO;
  486. mutex_unlock(&devreq_mutex);
  487. return in_service;
  488. }
  489. bool __is_daemon_in_service(void)
  490. {
  491. return open_access_count == 1;
  492. }
  493. static inline long check_ioctl_command(unsigned int command)
  494. {
  495. /* Check for valid ioctl codes */
  496. if (_IOC_TYPE(command) != ORANGEFS_DEV_MAGIC) {
  497. gossip_err("device ioctl magic numbers don't match! Did you rebuild pvfs2-client-core/libpvfs2? [cmd %x, magic %x != %x]\n",
  498. command,
  499. _IOC_TYPE(command),
  500. ORANGEFS_DEV_MAGIC);
  501. return -EINVAL;
  502. }
  503. /* and valid ioctl commands */
  504. if (_IOC_NR(command) >= ORANGEFS_DEV_MAXNR || _IOC_NR(command) <= 0) {
  505. gossip_err("Invalid ioctl command number [%d >= %d]\n",
  506. _IOC_NR(command), ORANGEFS_DEV_MAXNR);
  507. return -ENOIOCTLCMD;
  508. }
  509. return 0;
  510. }
  511. static long dispatch_ioctl_command(unsigned int command, unsigned long arg)
  512. {
  513. static __s32 magic = ORANGEFS_DEVREQ_MAGIC;
  514. static __s32 max_up_size = MAX_DEV_REQ_UPSIZE;
  515. static __s32 max_down_size = MAX_DEV_REQ_DOWNSIZE;
  516. struct ORANGEFS_dev_map_desc user_desc;
  517. int ret = 0;
  518. int upstream_kmod = 1;
  519. struct orangefs_sb_info_s *orangefs_sb;
  520. /* mtmoore: add locking here */
  521. switch (command) {
  522. case ORANGEFS_DEV_GET_MAGIC:
  523. return ((put_user(magic, (__s32 __user *) arg) == -EFAULT) ?
  524. -EIO :
  525. 0);
  526. case ORANGEFS_DEV_GET_MAX_UPSIZE:
  527. return ((put_user(max_up_size,
  528. (__s32 __user *) arg) == -EFAULT) ?
  529. -EIO :
  530. 0);
  531. case ORANGEFS_DEV_GET_MAX_DOWNSIZE:
  532. return ((put_user(max_down_size,
  533. (__s32 __user *) arg) == -EFAULT) ?
  534. -EIO :
  535. 0);
  536. case ORANGEFS_DEV_MAP:
  537. ret = copy_from_user(&user_desc,
  538. (struct ORANGEFS_dev_map_desc __user *)
  539. arg,
  540. sizeof(struct ORANGEFS_dev_map_desc));
  541. /* WTF -EIO and not -EFAULT? */
  542. return ret ? -EIO : orangefs_bufmap_initialize(&user_desc);
  543. case ORANGEFS_DEV_REMOUNT_ALL:
  544. gossip_debug(GOSSIP_DEV_DEBUG,
  545. "%s: got ORANGEFS_DEV_REMOUNT_ALL\n",
  546. __func__);
  547. /*
  548. * remount all mounted orangefs volumes to regain the lost
  549. * dynamic mount tables (if any) -- NOTE: this is done
  550. * without keeping the superblock list locked due to the
  551. * upcall/downcall waiting. also, the request mutex is
  552. * used to ensure that no operations will be serviced until
  553. * all of the remounts are serviced (to avoid ops between
  554. * mounts to fail)
  555. */
  556. ret = mutex_lock_interruptible(&orangefs_request_mutex);
  557. if (ret < 0)
  558. return ret;
  559. gossip_debug(GOSSIP_DEV_DEBUG,
  560. "%s: priority remount in progress\n",
  561. __func__);
  562. spin_lock(&orangefs_superblocks_lock);
  563. list_for_each_entry(orangefs_sb, &orangefs_superblocks, list) {
  564. /*
  565. * We have to drop the spinlock, so entries can be
  566. * removed. They can't be freed, though, so we just
  567. * keep the forward pointers and zero the back ones -
  568. * that way we can get to the rest of the list.
  569. */
  570. if (!orangefs_sb->list.prev)
  571. continue;
  572. gossip_debug(GOSSIP_DEV_DEBUG,
  573. "%s: Remounting SB %p\n",
  574. __func__,
  575. orangefs_sb);
  576. spin_unlock(&orangefs_superblocks_lock);
  577. ret = orangefs_remount(orangefs_sb);
  578. spin_lock(&orangefs_superblocks_lock);
  579. if (ret) {
  580. gossip_debug(GOSSIP_DEV_DEBUG,
  581. "SB %p remount failed\n",
  582. orangefs_sb);
  583. break;
  584. }
  585. }
  586. spin_unlock(&orangefs_superblocks_lock);
  587. gossip_debug(GOSSIP_DEV_DEBUG,
  588. "%s: priority remount complete\n",
  589. __func__);
  590. mutex_unlock(&orangefs_request_mutex);
  591. return ret;
  592. case ORANGEFS_DEV_UPSTREAM:
  593. ret = copy_to_user((void __user *)arg,
  594. &upstream_kmod,
  595. sizeof(upstream_kmod));
  596. if (ret != 0)
  597. return -EIO;
  598. else
  599. return ret;
  600. case ORANGEFS_DEV_CLIENT_MASK:
  601. return orangefs_debugfs_new_client_mask((void __user *)arg);
  602. case ORANGEFS_DEV_CLIENT_STRING:
  603. return orangefs_debugfs_new_client_string((void __user *)arg);
  604. case ORANGEFS_DEV_DEBUG:
  605. return orangefs_debugfs_new_debug((void __user *)arg);
  606. default:
  607. return -ENOIOCTLCMD;
  608. }
  609. return -ENOIOCTLCMD;
  610. }
  611. static long orangefs_devreq_ioctl(struct file *file,
  612. unsigned int command, unsigned long arg)
  613. {
  614. long ret;
  615. /* Check for properly constructed commands */
  616. ret = check_ioctl_command(command);
  617. if (ret < 0)
  618. return (int)ret;
  619. return (int)dispatch_ioctl_command(command, arg);
  620. }
  621. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  622. /* Compat structure for the ORANGEFS_DEV_MAP ioctl */
  623. struct ORANGEFS_dev_map_desc32 {
  624. compat_uptr_t ptr;
  625. __s32 total_size;
  626. __s32 size;
  627. __s32 count;
  628. };
  629. static unsigned long translate_dev_map26(unsigned long args, long *error)
  630. {
  631. struct ORANGEFS_dev_map_desc32 __user *p32 = (void __user *)args;
  632. /*
  633. * Depending on the architecture, allocate some space on the
  634. * user-call-stack based on our expected layout.
  635. */
  636. struct ORANGEFS_dev_map_desc __user *p =
  637. compat_alloc_user_space(sizeof(*p));
  638. compat_uptr_t addr;
  639. *error = 0;
  640. /* get the ptr from the 32 bit user-space */
  641. if (get_user(addr, &p32->ptr))
  642. goto err;
  643. /* try to put that into a 64-bit layout */
  644. if (put_user(compat_ptr(addr), &p->ptr))
  645. goto err;
  646. /* copy the remaining fields */
  647. if (copy_in_user(&p->total_size, &p32->total_size, sizeof(__s32)))
  648. goto err;
  649. if (copy_in_user(&p->size, &p32->size, sizeof(__s32)))
  650. goto err;
  651. if (copy_in_user(&p->count, &p32->count, sizeof(__s32)))
  652. goto err;
  653. return (unsigned long)p;
  654. err:
  655. *error = -EFAULT;
  656. return 0;
  657. }
  658. /*
  659. * 32 bit user-space apps' ioctl handlers when kernel modules
  660. * is compiled as a 64 bit one
  661. */
  662. static long orangefs_devreq_compat_ioctl(struct file *filp, unsigned int cmd,
  663. unsigned long args)
  664. {
  665. long ret;
  666. unsigned long arg = args;
  667. /* Check for properly constructed commands */
  668. ret = check_ioctl_command(cmd);
  669. if (ret < 0)
  670. return ret;
  671. if (cmd == ORANGEFS_DEV_MAP) {
  672. /*
  673. * convert the arguments to what we expect internally
  674. * in kernel space
  675. */
  676. arg = translate_dev_map26(args, &ret);
  677. if (ret < 0) {
  678. gossip_err("Could not translate dev map\n");
  679. return ret;
  680. }
  681. }
  682. /* no other ioctl requires translation */
  683. return dispatch_ioctl_command(cmd, arg);
  684. }
  685. #endif /* CONFIG_COMPAT is in .config */
  686. /* the assigned character device major number */
  687. static int orangefs_dev_major;
  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. }
  718. static unsigned int orangefs_devreq_poll(struct file *file,
  719. struct poll_table_struct *poll_table)
  720. {
  721. int poll_revent_mask = 0;
  722. poll_wait(file, &orangefs_request_list_waitq, poll_table);
  723. if (!list_empty(&orangefs_request_list))
  724. poll_revent_mask |= POLL_IN;
  725. return poll_revent_mask;
  726. }
  727. const struct file_operations orangefs_devreq_file_operations = {
  728. .owner = THIS_MODULE,
  729. .read = orangefs_devreq_read,
  730. .write_iter = orangefs_devreq_write_iter,
  731. .open = orangefs_devreq_open,
  732. .release = orangefs_devreq_release,
  733. .unlocked_ioctl = orangefs_devreq_ioctl,
  734. #ifdef CONFIG_COMPAT /* CONFIG_COMPAT is in .config */
  735. .compat_ioctl = orangefs_devreq_compat_ioctl,
  736. #endif
  737. .poll = orangefs_devreq_poll
  738. };