device.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  5. ** Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
  6. **
  7. ** This copyrighted material is made available to anyone wishing to use,
  8. ** modify, copy, or redistribute it subject to the terms and conditions
  9. ** of the GNU General Public License v.2.
  10. **
  11. *******************************************************************************
  12. ******************************************************************************/
  13. /*
  14. * device.c
  15. *
  16. * This is the userland interface to the DLM.
  17. *
  18. * The locking is done via a misc char device (find the
  19. * registered minor number in /proc/misc).
  20. *
  21. * User code should not use this interface directly but
  22. * call the library routines in libdlm.a instead.
  23. *
  24. */
  25. #include <linux/miscdevice.h>
  26. #include <linux/init.h>
  27. #include <linux/wait.h>
  28. #include <linux/module.h>
  29. #include <linux/file.h>
  30. #include <linux/fs.h>
  31. #include <linux/poll.h>
  32. #include <linux/signal.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/idr.h>
  35. #include <linux/dlm.h>
  36. #include <linux/dlm_device.h>
  37. #include "lvb_table.h"
  38. static struct file_operations _dlm_fops;
  39. static const char *name_prefix="dlm";
  40. static struct list_head user_ls_list;
  41. static struct mutex user_ls_lock;
  42. /* Lock infos are stored in here indexed by lock ID */
  43. static DEFINE_IDR(lockinfo_idr);
  44. static rwlock_t lockinfo_lock;
  45. /* Flags in li_flags */
  46. #define LI_FLAG_COMPLETE 1
  47. #define LI_FLAG_FIRSTLOCK 2
  48. #define LI_FLAG_PERSISTENT 3
  49. #define LI_FLAG_ONLIST 4
  50. /* flags in ls_flags*/
  51. #define LS_FLAG_DELETED 1
  52. #define LS_FLAG_AUTOFREE 2
  53. #define LOCKINFO_MAGIC 0x53595324
  54. struct lock_info {
  55. uint32_t li_magic;
  56. uint8_t li_cmd;
  57. int8_t li_grmode;
  58. int8_t li_rqmode;
  59. struct dlm_lksb li_lksb;
  60. wait_queue_head_t li_waitq;
  61. unsigned long li_flags;
  62. void __user *li_castparam;
  63. void __user *li_castaddr;
  64. void __user *li_bastparam;
  65. void __user *li_bastaddr;
  66. void __user *li_pend_bastparam;
  67. void __user *li_pend_bastaddr;
  68. struct list_head li_ownerqueue;
  69. struct file_info *li_file;
  70. struct dlm_lksb __user *li_user_lksb;
  71. struct semaphore li_firstlock;
  72. };
  73. /* A queued AST no less */
  74. struct ast_info {
  75. struct dlm_lock_result result;
  76. struct list_head list;
  77. uint32_t lvb_updated;
  78. uint32_t progress; /* How much has been read */
  79. };
  80. /* One of these per userland lockspace */
  81. struct user_ls {
  82. void *ls_lockspace;
  83. atomic_t ls_refcnt;
  84. long ls_flags;
  85. /* Passed into misc_register() */
  86. struct miscdevice ls_miscinfo;
  87. struct list_head ls_list;
  88. };
  89. /* misc_device info for the control device */
  90. static struct miscdevice ctl_device;
  91. /*
  92. * Stuff we hang off the file struct.
  93. * The first two are to cope with unlocking all the
  94. * locks help by a process when it dies.
  95. */
  96. struct file_info {
  97. struct list_head fi_li_list; /* List of active lock_infos */
  98. spinlock_t fi_li_lock;
  99. struct list_head fi_ast_list; /* Queue of ASTs to be delivered */
  100. spinlock_t fi_ast_lock;
  101. wait_queue_head_t fi_wait;
  102. struct user_ls *fi_ls;
  103. atomic_t fi_refcnt; /* Number of users */
  104. unsigned long fi_flags; /* Bit 1 means the device is open */
  105. };
  106. /* get and put ops for file_info.
  107. Actually I don't really like "get" and "put", but everyone
  108. else seems to use them and I can't think of anything
  109. nicer at the moment */
  110. static void get_file_info(struct file_info *f)
  111. {
  112. atomic_inc(&f->fi_refcnt);
  113. }
  114. static void put_file_info(struct file_info *f)
  115. {
  116. if (atomic_dec_and_test(&f->fi_refcnt))
  117. kfree(f);
  118. }
  119. static void release_lockinfo(struct lock_info *li)
  120. {
  121. put_file_info(li->li_file);
  122. write_lock(&lockinfo_lock);
  123. idr_remove(&lockinfo_idr, li->li_lksb.sb_lkid);
  124. write_unlock(&lockinfo_lock);
  125. if (li->li_lksb.sb_lvbptr)
  126. kfree(li->li_lksb.sb_lvbptr);
  127. kfree(li);
  128. module_put(THIS_MODULE);
  129. }
  130. static struct lock_info *get_lockinfo(uint32_t lockid)
  131. {
  132. struct lock_info *li;
  133. read_lock(&lockinfo_lock);
  134. li = idr_find(&lockinfo_idr, lockid);
  135. read_unlock(&lockinfo_lock);
  136. return li;
  137. }
  138. static int add_lockinfo(struct lock_info *li)
  139. {
  140. int n;
  141. int r;
  142. int ret = -EINVAL;
  143. write_lock(&lockinfo_lock);
  144. if (idr_find(&lockinfo_idr, li->li_lksb.sb_lkid))
  145. goto out_up;
  146. ret = -ENOMEM;
  147. r = idr_pre_get(&lockinfo_idr, GFP_KERNEL);
  148. if (!r)
  149. goto out_up;
  150. r = idr_get_new_above(&lockinfo_idr, li, li->li_lksb.sb_lkid, &n);
  151. if (r)
  152. goto out_up;
  153. if (n != li->li_lksb.sb_lkid) {
  154. idr_remove(&lockinfo_idr, n);
  155. goto out_up;
  156. }
  157. ret = 0;
  158. out_up:
  159. write_unlock(&lockinfo_lock);
  160. return ret;
  161. }
  162. static struct user_ls *__find_lockspace(int minor)
  163. {
  164. struct user_ls *lsinfo;
  165. list_for_each_entry(lsinfo, &user_ls_list, ls_list) {
  166. if (lsinfo->ls_miscinfo.minor == minor)
  167. return lsinfo;
  168. }
  169. return NULL;
  170. }
  171. /* Find a lockspace struct given the device minor number */
  172. static struct user_ls *find_lockspace(int minor)
  173. {
  174. struct user_ls *lsinfo;
  175. mutex_lock(&user_ls_lock);
  176. lsinfo = __find_lockspace(minor);
  177. mutex_unlock(&user_ls_lock);
  178. return lsinfo;
  179. }
  180. static void add_lockspace_to_list(struct user_ls *lsinfo)
  181. {
  182. mutex_lock(&user_ls_lock);
  183. list_add(&lsinfo->ls_list, &user_ls_list);
  184. mutex_unlock(&user_ls_lock);
  185. }
  186. /* Register a lockspace with the DLM and create a misc
  187. device for userland to access it */
  188. static int register_lockspace(char *name, struct user_ls **ls, int flags)
  189. {
  190. struct user_ls *newls;
  191. int status;
  192. int namelen;
  193. namelen = strlen(name)+strlen(name_prefix)+2;
  194. newls = kzalloc(sizeof(struct user_ls), GFP_KERNEL);
  195. if (!newls)
  196. return -ENOMEM;
  197. newls->ls_miscinfo.name = kzalloc(namelen, GFP_KERNEL);
  198. if (!newls->ls_miscinfo.name) {
  199. kfree(newls);
  200. return -ENOMEM;
  201. }
  202. status = dlm_new_lockspace(name, strlen(name), &newls->ls_lockspace, 0,
  203. DLM_USER_LVB_LEN);
  204. if (status != 0) {
  205. kfree(newls->ls_miscinfo.name);
  206. kfree(newls);
  207. return status;
  208. }
  209. snprintf((char*)newls->ls_miscinfo.name, namelen, "%s_%s",
  210. name_prefix, name);
  211. newls->ls_miscinfo.fops = &_dlm_fops;
  212. newls->ls_miscinfo.minor = MISC_DYNAMIC_MINOR;
  213. status = misc_register(&newls->ls_miscinfo);
  214. if (status) {
  215. printk(KERN_ERR "dlm: misc register failed for %s\n", name);
  216. dlm_release_lockspace(newls->ls_lockspace, 0);
  217. kfree(newls->ls_miscinfo.name);
  218. kfree(newls);
  219. return status;
  220. }
  221. if (flags & DLM_USER_LSFLG_AUTOFREE)
  222. set_bit(LS_FLAG_AUTOFREE, &newls->ls_flags);
  223. add_lockspace_to_list(newls);
  224. *ls = newls;
  225. return 0;
  226. }
  227. /* Called with the user_ls_lock mutex held */
  228. static int unregister_lockspace(struct user_ls *lsinfo, int force)
  229. {
  230. int status;
  231. status = dlm_release_lockspace(lsinfo->ls_lockspace, force);
  232. if (status)
  233. return status;
  234. status = misc_deregister(&lsinfo->ls_miscinfo);
  235. if (status)
  236. return status;
  237. list_del(&lsinfo->ls_list);
  238. set_bit(LS_FLAG_DELETED, &lsinfo->ls_flags);
  239. lsinfo->ls_lockspace = NULL;
  240. if (atomic_read(&lsinfo->ls_refcnt) == 0) {
  241. kfree(lsinfo->ls_miscinfo.name);
  242. kfree(lsinfo);
  243. }
  244. return 0;
  245. }
  246. /* Add it to userland's AST queue */
  247. static void add_to_astqueue(struct lock_info *li, void *astaddr, void *astparam,
  248. int lvb_updated)
  249. {
  250. struct ast_info *ast = kzalloc(sizeof(struct ast_info), GFP_KERNEL);
  251. if (!ast)
  252. return;
  253. ast->result.user_astparam = astparam;
  254. ast->result.user_astaddr = astaddr;
  255. ast->result.user_lksb = li->li_user_lksb;
  256. memcpy(&ast->result.lksb, &li->li_lksb, sizeof(struct dlm_lksb));
  257. ast->lvb_updated = lvb_updated;
  258. spin_lock(&li->li_file->fi_ast_lock);
  259. list_add_tail(&ast->list, &li->li_file->fi_ast_list);
  260. spin_unlock(&li->li_file->fi_ast_lock);
  261. wake_up_interruptible(&li->li_file->fi_wait);
  262. }
  263. static void bast_routine(void *param, int mode)
  264. {
  265. struct lock_info *li = param;
  266. if (li && li->li_bastaddr)
  267. add_to_astqueue(li, li->li_bastaddr, li->li_bastparam, 0);
  268. }
  269. /*
  270. * This is the kernel's AST routine.
  271. * All lock, unlock & query operations complete here.
  272. * The only syncronous ops are those done during device close.
  273. */
  274. static void ast_routine(void *param)
  275. {
  276. struct lock_info *li = param;
  277. /* Param may be NULL if a persistent lock is unlocked by someone else */
  278. if (!li)
  279. return;
  280. /* If this is a succesful conversion then activate the blocking ast
  281. * args from the conversion request */
  282. if (!test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
  283. li->li_lksb.sb_status == 0) {
  284. li->li_bastparam = li->li_pend_bastparam;
  285. li->li_bastaddr = li->li_pend_bastaddr;
  286. li->li_pend_bastaddr = NULL;
  287. }
  288. /* If it's an async request then post data to the user's AST queue. */
  289. if (li->li_castaddr) {
  290. int lvb_updated = 0;
  291. /* See if the lvb has been updated */
  292. if (dlm_lvb_operations[li->li_grmode+1][li->li_rqmode+1] == 1)
  293. lvb_updated = 1;
  294. if (li->li_lksb.sb_status == 0)
  295. li->li_grmode = li->li_rqmode;
  296. /* Only queue AST if the device is still open */
  297. if (test_bit(1, &li->li_file->fi_flags))
  298. add_to_astqueue(li, li->li_castaddr, li->li_castparam,
  299. lvb_updated);
  300. /* If it's a new lock operation that failed, then
  301. * remove it from the owner queue and free the
  302. * lock_info.
  303. */
  304. if (test_and_clear_bit(LI_FLAG_FIRSTLOCK, &li->li_flags) &&
  305. li->li_lksb.sb_status != 0) {
  306. /* Wait till dlm_lock() has finished */
  307. down(&li->li_firstlock);
  308. up(&li->li_firstlock);
  309. spin_lock(&li->li_file->fi_li_lock);
  310. list_del(&li->li_ownerqueue);
  311. clear_bit(LI_FLAG_ONLIST, &li->li_flags);
  312. spin_unlock(&li->li_file->fi_li_lock);
  313. release_lockinfo(li);
  314. return;
  315. }
  316. /* Free unlocks & queries */
  317. if (li->li_lksb.sb_status == -DLM_EUNLOCK ||
  318. li->li_cmd == DLM_USER_QUERY) {
  319. release_lockinfo(li);
  320. }
  321. } else {
  322. /* Synchronous request, just wake up the caller */
  323. set_bit(LI_FLAG_COMPLETE, &li->li_flags);
  324. wake_up_interruptible(&li->li_waitq);
  325. }
  326. }
  327. /*
  328. * Wait for the lock op to complete and return the status.
  329. */
  330. static int wait_for_ast(struct lock_info *li)
  331. {
  332. /* Wait for the AST routine to complete */
  333. set_task_state(current, TASK_INTERRUPTIBLE);
  334. while (!test_bit(LI_FLAG_COMPLETE, &li->li_flags))
  335. schedule();
  336. set_task_state(current, TASK_RUNNING);
  337. return li->li_lksb.sb_status;
  338. }
  339. /* Open on control device */
  340. static int dlm_ctl_open(struct inode *inode, struct file *file)
  341. {
  342. file->private_data = NULL;
  343. return 0;
  344. }
  345. /* Close on control device */
  346. static int dlm_ctl_close(struct inode *inode, struct file *file)
  347. {
  348. return 0;
  349. }
  350. /* Open on lockspace device */
  351. static int dlm_open(struct inode *inode, struct file *file)
  352. {
  353. struct file_info *f;
  354. struct user_ls *lsinfo;
  355. lsinfo = find_lockspace(iminor(inode));
  356. if (!lsinfo)
  357. return -ENOENT;
  358. f = kzalloc(sizeof(struct file_info), GFP_KERNEL);
  359. if (!f)
  360. return -ENOMEM;
  361. atomic_inc(&lsinfo->ls_refcnt);
  362. INIT_LIST_HEAD(&f->fi_li_list);
  363. INIT_LIST_HEAD(&f->fi_ast_list);
  364. spin_lock_init(&f->fi_li_lock);
  365. spin_lock_init(&f->fi_ast_lock);
  366. init_waitqueue_head(&f->fi_wait);
  367. f->fi_ls = lsinfo;
  368. f->fi_flags = 0;
  369. get_file_info(f);
  370. set_bit(1, &f->fi_flags);
  371. file->private_data = f;
  372. return 0;
  373. }
  374. /* Check the user's version matches ours */
  375. static int check_version(struct dlm_write_request *req)
  376. {
  377. if (req->version[0] != DLM_DEVICE_VERSION_MAJOR ||
  378. (req->version[0] == DLM_DEVICE_VERSION_MAJOR &&
  379. req->version[1] > DLM_DEVICE_VERSION_MINOR)) {
  380. printk(KERN_DEBUG "dlm: process %s (%d) version mismatch "
  381. "user (%d.%d.%d) kernel (%d.%d.%d)\n",
  382. current->comm,
  383. current->pid,
  384. req->version[0],
  385. req->version[1],
  386. req->version[2],
  387. DLM_DEVICE_VERSION_MAJOR,
  388. DLM_DEVICE_VERSION_MINOR,
  389. DLM_DEVICE_VERSION_PATCH);
  390. return -EINVAL;
  391. }
  392. return 0;
  393. }
  394. /* Close on lockspace device */
  395. static int dlm_close(struct inode *inode, struct file *file)
  396. {
  397. struct file_info *f = file->private_data;
  398. struct lock_info li;
  399. struct lock_info *old_li, *safe;
  400. sigset_t tmpsig;
  401. sigset_t allsigs;
  402. struct user_ls *lsinfo;
  403. DECLARE_WAITQUEUE(wq, current);
  404. lsinfo = find_lockspace(iminor(inode));
  405. if (!lsinfo)
  406. return -ENOENT;
  407. /* Mark this closed so that ASTs will not be delivered any more */
  408. clear_bit(1, &f->fi_flags);
  409. /* Block signals while we are doing this */
  410. sigfillset(&allsigs);
  411. sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
  412. /* We use our own lock_info struct here, so that any
  413. * outstanding "real" ASTs will be delivered with the
  414. * corresponding "real" params, thus freeing the lock_info
  415. * that belongs the lock. This catches the corner case where
  416. * a lock is BUSY when we try to unlock it here
  417. */
  418. memset(&li, 0, sizeof(li));
  419. clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
  420. init_waitqueue_head(&li.li_waitq);
  421. add_wait_queue(&li.li_waitq, &wq);
  422. /*
  423. * Free any outstanding locks, they are on the
  424. * list in LIFO order so there should be no problems
  425. * about unlocking parents before children.
  426. */
  427. list_for_each_entry_safe(old_li, safe, &f->fi_li_list, li_ownerqueue) {
  428. int status;
  429. int flags = 0;
  430. /* Don't unlock persistent locks, just mark them orphaned */
  431. if (test_bit(LI_FLAG_PERSISTENT, &old_li->li_flags)) {
  432. list_del(&old_li->li_ownerqueue);
  433. /* Update master copy */
  434. /* TODO: Check locking core updates the local and
  435. remote ORPHAN flags */
  436. li.li_lksb.sb_lkid = old_li->li_lksb.sb_lkid;
  437. status = dlm_lock(f->fi_ls->ls_lockspace,
  438. old_li->li_grmode, &li.li_lksb,
  439. DLM_LKF_CONVERT|DLM_LKF_ORPHAN,
  440. NULL, 0, 0, ast_routine, NULL,
  441. NULL, NULL);
  442. if (status != 0)
  443. printk("dlm: Error orphaning lock %x: %d\n",
  444. old_li->li_lksb.sb_lkid, status);
  445. /* But tidy our references in it */
  446. release_lockinfo(old_li);
  447. continue;
  448. }
  449. clear_bit(LI_FLAG_COMPLETE, &li.li_flags);
  450. flags = DLM_LKF_FORCEUNLOCK;
  451. if (old_li->li_grmode >= DLM_LOCK_PW)
  452. flags |= DLM_LKF_IVVALBLK;
  453. status = dlm_unlock(f->fi_ls->ls_lockspace,
  454. old_li->li_lksb.sb_lkid, flags,
  455. &li.li_lksb, &li);
  456. /* Must wait for it to complete as the next lock could be its
  457. * parent */
  458. if (status == 0)
  459. wait_for_ast(&li);
  460. /* Unlock suceeded, free the lock_info struct. */
  461. if (status == 0)
  462. release_lockinfo(old_li);
  463. }
  464. remove_wait_queue(&li.li_waitq, &wq);
  465. /*
  466. * If this is the last reference to the lockspace
  467. * then free the struct. If it's an AUTOFREE lockspace
  468. * then free the whole thing.
  469. */
  470. mutex_lock(&user_ls_lock);
  471. if (atomic_dec_and_test(&lsinfo->ls_refcnt)) {
  472. if (lsinfo->ls_lockspace) {
  473. if (test_bit(LS_FLAG_AUTOFREE, &lsinfo->ls_flags)) {
  474. unregister_lockspace(lsinfo, 1);
  475. }
  476. } else {
  477. kfree(lsinfo->ls_miscinfo.name);
  478. kfree(lsinfo);
  479. }
  480. }
  481. mutex_unlock(&user_ls_lock);
  482. put_file_info(f);
  483. /* Restore signals */
  484. sigprocmask(SIG_SETMASK, &tmpsig, NULL);
  485. recalc_sigpending();
  486. return 0;
  487. }
  488. static int do_user_create_lockspace(struct file_info *fi, uint8_t cmd,
  489. struct dlm_lspace_params *kparams)
  490. {
  491. int status;
  492. struct user_ls *lsinfo;
  493. if (!capable(CAP_SYS_ADMIN))
  494. return -EPERM;
  495. status = register_lockspace(kparams->name, &lsinfo, kparams->flags);
  496. /* If it succeeded then return the minor number */
  497. if (status == 0)
  498. status = lsinfo->ls_miscinfo.minor;
  499. return status;
  500. }
  501. static int do_user_remove_lockspace(struct file_info *fi, uint8_t cmd,
  502. struct dlm_lspace_params *kparams)
  503. {
  504. int status;
  505. int force = 1;
  506. struct user_ls *lsinfo;
  507. if (!capable(CAP_SYS_ADMIN))
  508. return -EPERM;
  509. mutex_lock(&user_ls_lock);
  510. lsinfo = __find_lockspace(kparams->minor);
  511. if (!lsinfo) {
  512. mutex_unlock(&user_ls_lock);
  513. return -EINVAL;
  514. }
  515. if (kparams->flags & DLM_USER_LSFLG_FORCEFREE)
  516. force = 2;
  517. status = unregister_lockspace(lsinfo, force);
  518. mutex_unlock(&user_ls_lock);
  519. return status;
  520. }
  521. /* Read call, might block if no ASTs are waiting.
  522. * It will only ever return one message at a time, regardless
  523. * of how many are pending.
  524. */
  525. static ssize_t dlm_read(struct file *file, char __user *buffer, size_t count,
  526. loff_t *ppos)
  527. {
  528. struct file_info *fi = file->private_data;
  529. struct ast_info *ast;
  530. int data_size;
  531. int offset;
  532. DECLARE_WAITQUEUE(wait, current);
  533. if (count < sizeof(struct dlm_lock_result))
  534. return -EINVAL;
  535. spin_lock(&fi->fi_ast_lock);
  536. if (list_empty(&fi->fi_ast_list)) {
  537. /* No waiting ASTs.
  538. * Return EOF if the lockspace been deleted.
  539. */
  540. if (test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
  541. return 0;
  542. if (file->f_flags & O_NONBLOCK) {
  543. spin_unlock(&fi->fi_ast_lock);
  544. return -EAGAIN;
  545. }
  546. add_wait_queue(&fi->fi_wait, &wait);
  547. repeat:
  548. set_current_state(TASK_INTERRUPTIBLE);
  549. if (list_empty(&fi->fi_ast_list) &&
  550. !signal_pending(current)) {
  551. spin_unlock(&fi->fi_ast_lock);
  552. schedule();
  553. spin_lock(&fi->fi_ast_lock);
  554. goto repeat;
  555. }
  556. current->state = TASK_RUNNING;
  557. remove_wait_queue(&fi->fi_wait, &wait);
  558. if (signal_pending(current)) {
  559. spin_unlock(&fi->fi_ast_lock);
  560. return -ERESTARTSYS;
  561. }
  562. }
  563. ast = list_entry(fi->fi_ast_list.next, struct ast_info, list);
  564. list_del(&ast->list);
  565. spin_unlock(&fi->fi_ast_lock);
  566. /* Work out the size of the returned data */
  567. data_size = sizeof(struct dlm_lock_result);
  568. if (ast->lvb_updated && ast->result.lksb.sb_lvbptr)
  569. data_size += DLM_USER_LVB_LEN;
  570. offset = sizeof(struct dlm_lock_result);
  571. /* Room for the extended data ? */
  572. if (count >= data_size) {
  573. if (ast->lvb_updated && ast->result.lksb.sb_lvbptr) {
  574. if (copy_to_user(buffer+offset,
  575. ast->result.lksb.sb_lvbptr,
  576. DLM_USER_LVB_LEN))
  577. return -EFAULT;
  578. ast->result.lvb_offset = offset;
  579. offset += DLM_USER_LVB_LEN;
  580. }
  581. }
  582. ast->result.length = data_size;
  583. /* Copy the header now it has all the offsets in it */
  584. if (copy_to_user(buffer, &ast->result, sizeof(struct dlm_lock_result)))
  585. offset = -EFAULT;
  586. /* If we only returned a header and there's more to come then put it
  587. back on the list */
  588. if (count < data_size) {
  589. spin_lock(&fi->fi_ast_lock);
  590. list_add(&ast->list, &fi->fi_ast_list);
  591. spin_unlock(&fi->fi_ast_lock);
  592. } else
  593. kfree(ast);
  594. return offset;
  595. }
  596. static unsigned int dlm_poll(struct file *file, poll_table *wait)
  597. {
  598. struct file_info *fi = file->private_data;
  599. poll_wait(file, &fi->fi_wait, wait);
  600. spin_lock(&fi->fi_ast_lock);
  601. if (!list_empty(&fi->fi_ast_list)) {
  602. spin_unlock(&fi->fi_ast_lock);
  603. return POLLIN | POLLRDNORM;
  604. }
  605. spin_unlock(&fi->fi_ast_lock);
  606. return 0;
  607. }
  608. static struct lock_info *allocate_lockinfo(struct file_info *fi, uint8_t cmd,
  609. struct dlm_lock_params *kparams)
  610. {
  611. struct lock_info *li;
  612. if (!try_module_get(THIS_MODULE))
  613. return NULL;
  614. li = kzalloc(sizeof(struct lock_info), GFP_KERNEL);
  615. if (li) {
  616. li->li_magic = LOCKINFO_MAGIC;
  617. li->li_file = fi;
  618. li->li_cmd = cmd;
  619. li->li_flags = 0;
  620. li->li_grmode = -1;
  621. li->li_rqmode = -1;
  622. li->li_pend_bastparam = NULL;
  623. li->li_pend_bastaddr = NULL;
  624. li->li_castaddr = NULL;
  625. li->li_castparam = NULL;
  626. li->li_lksb.sb_lvbptr = NULL;
  627. li->li_bastaddr = kparams->bastaddr;
  628. li->li_bastparam = kparams->bastparam;
  629. get_file_info(fi);
  630. }
  631. return li;
  632. }
  633. static int do_user_lock(struct file_info *fi, uint8_t cmd,
  634. struct dlm_lock_params *kparams)
  635. {
  636. struct lock_info *li;
  637. int status;
  638. /*
  639. * Validate things that we need to have correct.
  640. */
  641. if (!kparams->castaddr)
  642. return -EINVAL;
  643. if (!kparams->lksb)
  644. return -EINVAL;
  645. /* Persistent child locks are not available yet */
  646. if ((kparams->flags & DLM_LKF_PERSISTENT) && kparams->parent)
  647. return -EINVAL;
  648. /* For conversions, there should already be a lockinfo struct,
  649. unless we are adopting an orphaned persistent lock */
  650. if (kparams->flags & DLM_LKF_CONVERT) {
  651. li = get_lockinfo(kparams->lkid);
  652. /* If this is a persistent lock we will have to create a
  653. lockinfo again */
  654. if (!li && (kparams->flags & DLM_LKF_PERSISTENT)) {
  655. li = allocate_lockinfo(fi, cmd, kparams);
  656. if (!li)
  657. return -ENOMEM;
  658. li->li_lksb.sb_lkid = kparams->lkid;
  659. li->li_castaddr = kparams->castaddr;
  660. li->li_castparam = kparams->castparam;
  661. /* OK, this isn;t exactly a FIRSTLOCK but it is the
  662. first time we've used this lockinfo, and if things
  663. fail we want rid of it */
  664. init_MUTEX_LOCKED(&li->li_firstlock);
  665. set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
  666. add_lockinfo(li);
  667. /* TODO: do a query to get the current state ?? */
  668. }
  669. if (!li)
  670. return -EINVAL;
  671. if (li->li_magic != LOCKINFO_MAGIC)
  672. return -EINVAL;
  673. /* For conversions don't overwrite the current blocking AST
  674. info so that:
  675. a) if a blocking AST fires before the conversion is queued
  676. it runs the current handler
  677. b) if the conversion is cancelled, the original blocking AST
  678. declaration is active
  679. The pend_ info is made active when the conversion
  680. completes.
  681. */
  682. li->li_pend_bastaddr = kparams->bastaddr;
  683. li->li_pend_bastparam = kparams->bastparam;
  684. } else {
  685. li = allocate_lockinfo(fi, cmd, kparams);
  686. if (!li)
  687. return -ENOMEM;
  688. /* semaphore to allow us to complete our work before
  689. the AST routine runs. In fact we only need (and use) this
  690. when the initial lock fails */
  691. init_MUTEX_LOCKED(&li->li_firstlock);
  692. set_bit(LI_FLAG_FIRSTLOCK, &li->li_flags);
  693. }
  694. li->li_user_lksb = kparams->lksb;
  695. li->li_castaddr = kparams->castaddr;
  696. li->li_castparam = kparams->castparam;
  697. li->li_lksb.sb_lkid = kparams->lkid;
  698. li->li_rqmode = kparams->mode;
  699. if (kparams->flags & DLM_LKF_PERSISTENT)
  700. set_bit(LI_FLAG_PERSISTENT, &li->li_flags);
  701. /* Copy in the value block */
  702. if (kparams->flags & DLM_LKF_VALBLK) {
  703. if (!li->li_lksb.sb_lvbptr) {
  704. li->li_lksb.sb_lvbptr = kmalloc(DLM_USER_LVB_LEN,
  705. GFP_KERNEL);
  706. if (!li->li_lksb.sb_lvbptr) {
  707. status = -ENOMEM;
  708. goto out_err;
  709. }
  710. }
  711. memcpy(li->li_lksb.sb_lvbptr, kparams->lvb, DLM_USER_LVB_LEN);
  712. }
  713. /* Lock it ... */
  714. status = dlm_lock(fi->fi_ls->ls_lockspace,
  715. kparams->mode, &li->li_lksb,
  716. kparams->flags,
  717. kparams->name, kparams->namelen,
  718. kparams->parent,
  719. ast_routine,
  720. li,
  721. (li->li_pend_bastaddr || li->li_bastaddr) ?
  722. bast_routine : NULL,
  723. kparams->range.ra_end ? &kparams->range : NULL);
  724. if (status)
  725. goto out_err;
  726. /* If it succeeded (this far) with a new lock then keep track of
  727. it on the file's lockinfo list */
  728. if (!status && test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags)) {
  729. spin_lock(&fi->fi_li_lock);
  730. list_add(&li->li_ownerqueue, &fi->fi_li_list);
  731. set_bit(LI_FLAG_ONLIST, &li->li_flags);
  732. spin_unlock(&fi->fi_li_lock);
  733. if (add_lockinfo(li))
  734. printk(KERN_WARNING "Add lockinfo failed\n");
  735. up(&li->li_firstlock);
  736. }
  737. /* Return the lockid as the user needs it /now/ */
  738. return li->li_lksb.sb_lkid;
  739. out_err:
  740. if (test_bit(LI_FLAG_FIRSTLOCK, &li->li_flags))
  741. release_lockinfo(li);
  742. return status;
  743. }
  744. static int do_user_unlock(struct file_info *fi, uint8_t cmd,
  745. struct dlm_lock_params *kparams)
  746. {
  747. struct lock_info *li;
  748. int status;
  749. int convert_cancel = 0;
  750. li = get_lockinfo(kparams->lkid);
  751. if (!li) {
  752. li = allocate_lockinfo(fi, cmd, kparams);
  753. if (!li)
  754. return -ENOMEM;
  755. spin_lock(&fi->fi_li_lock);
  756. list_add(&li->li_ownerqueue, &fi->fi_li_list);
  757. set_bit(LI_FLAG_ONLIST, &li->li_flags);
  758. spin_unlock(&fi->fi_li_lock);
  759. }
  760. if (li->li_magic != LOCKINFO_MAGIC)
  761. return -EINVAL;
  762. li->li_user_lksb = kparams->lksb;
  763. li->li_castparam = kparams->castparam;
  764. li->li_cmd = cmd;
  765. /* Cancelling a conversion doesn't remove the lock...*/
  766. if (kparams->flags & DLM_LKF_CANCEL && li->li_grmode != -1)
  767. convert_cancel = 1;
  768. /* Wait until dlm_lock() has completed */
  769. if (!test_bit(LI_FLAG_ONLIST, &li->li_flags)) {
  770. down(&li->li_firstlock);
  771. up(&li->li_firstlock);
  772. }
  773. /* dlm_unlock() passes a 0 for castaddr which means don't overwrite
  774. the existing li_castaddr as that's the completion routine for
  775. unlocks. dlm_unlock_wait() specifies a new AST routine to be
  776. executed when the unlock completes. */
  777. if (kparams->castaddr)
  778. li->li_castaddr = kparams->castaddr;
  779. /* Use existing lksb & astparams */
  780. status = dlm_unlock(fi->fi_ls->ls_lockspace,
  781. kparams->lkid,
  782. kparams->flags, &li->li_lksb, li);
  783. if (!status && !convert_cancel) {
  784. spin_lock(&fi->fi_li_lock);
  785. list_del(&li->li_ownerqueue);
  786. clear_bit(LI_FLAG_ONLIST, &li->li_flags);
  787. spin_unlock(&fi->fi_li_lock);
  788. }
  789. return status;
  790. }
  791. /* Write call, submit a locking request */
  792. static ssize_t dlm_write(struct file *file, const char __user *buffer,
  793. size_t count, loff_t *ppos)
  794. {
  795. struct file_info *fi = file->private_data;
  796. struct dlm_write_request *kparams;
  797. sigset_t tmpsig;
  798. sigset_t allsigs;
  799. int status;
  800. /* -1 because lock name is optional */
  801. if (count < sizeof(struct dlm_write_request)-1)
  802. return -EINVAL;
  803. /* Has the lockspace been deleted */
  804. if (fi && test_bit(LS_FLAG_DELETED, &fi->fi_ls->ls_flags))
  805. return -ENOENT;
  806. kparams = kmalloc(count, GFP_KERNEL);
  807. if (!kparams)
  808. return -ENOMEM;
  809. status = -EFAULT;
  810. /* Get the command info */
  811. if (copy_from_user(kparams, buffer, count))
  812. goto out_free;
  813. status = -EBADE;
  814. if (check_version(kparams))
  815. goto out_free;
  816. /* Block signals while we are doing this */
  817. sigfillset(&allsigs);
  818. sigprocmask(SIG_BLOCK, &allsigs, &tmpsig);
  819. status = -EINVAL;
  820. switch (kparams->cmd)
  821. {
  822. case DLM_USER_LOCK:
  823. if (!fi) goto out_sig;
  824. status = do_user_lock(fi, kparams->cmd, &kparams->i.lock);
  825. break;
  826. case DLM_USER_UNLOCK:
  827. if (!fi) goto out_sig;
  828. status = do_user_unlock(fi, kparams->cmd, &kparams->i.lock);
  829. break;
  830. case DLM_USER_CREATE_LOCKSPACE:
  831. if (fi) goto out_sig;
  832. status = do_user_create_lockspace(fi, kparams->cmd,
  833. &kparams->i.lspace);
  834. break;
  835. case DLM_USER_REMOVE_LOCKSPACE:
  836. if (fi) goto out_sig;
  837. status = do_user_remove_lockspace(fi, kparams->cmd,
  838. &kparams->i.lspace);
  839. break;
  840. default:
  841. printk("Unknown command passed to DLM device : %d\n",
  842. kparams->cmd);
  843. break;
  844. }
  845. out_sig:
  846. /* Restore signals */
  847. sigprocmask(SIG_SETMASK, &tmpsig, NULL);
  848. recalc_sigpending();
  849. out_free:
  850. kfree(kparams);
  851. if (status == 0)
  852. return count;
  853. else
  854. return status;
  855. }
  856. static struct file_operations _dlm_fops = {
  857. .open = dlm_open,
  858. .release = dlm_close,
  859. .read = dlm_read,
  860. .write = dlm_write,
  861. .poll = dlm_poll,
  862. .owner = THIS_MODULE,
  863. };
  864. static struct file_operations _dlm_ctl_fops = {
  865. .open = dlm_ctl_open,
  866. .release = dlm_ctl_close,
  867. .write = dlm_write,
  868. .owner = THIS_MODULE,
  869. };
  870. /*
  871. * Create control device
  872. */
  873. static int __init dlm_device_init(void)
  874. {
  875. int r;
  876. INIT_LIST_HEAD(&user_ls_list);
  877. mutex_init(&user_ls_lock);
  878. rwlock_init(&lockinfo_lock);
  879. ctl_device.name = "dlm-control";
  880. ctl_device.fops = &_dlm_ctl_fops;
  881. ctl_device.minor = MISC_DYNAMIC_MINOR;
  882. r = misc_register(&ctl_device);
  883. if (r) {
  884. printk(KERN_ERR "dlm: misc_register failed for control dev\n");
  885. return r;
  886. }
  887. return 0;
  888. }
  889. static void __exit dlm_device_exit(void)
  890. {
  891. misc_deregister(&ctl_device);
  892. }
  893. MODULE_DESCRIPTION("Distributed Lock Manager device interface");
  894. MODULE_AUTHOR("Red Hat, Inc.");
  895. MODULE_LICENSE("GPL");
  896. module_init(dlm_device_init);
  897. module_exit(dlm_device_exit);