flock.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. /* AFS file locking support
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/smp_lock.h>
  12. #include "internal.h"
  13. #define AFS_LOCK_GRANTED 0
  14. #define AFS_LOCK_PENDING 1
  15. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
  16. static void afs_fl_release_private(struct file_lock *fl);
  17. static struct workqueue_struct *afs_lock_manager;
  18. static struct file_lock_operations afs_lock_ops = {
  19. .fl_copy_lock = afs_fl_copy_lock,
  20. .fl_release_private = afs_fl_release_private,
  21. };
  22. /*
  23. * initialise the lock manager thread if it isn't already running
  24. */
  25. static int afs_init_lock_manager(void)
  26. {
  27. if (!afs_lock_manager) {
  28. afs_lock_manager = create_singlethread_workqueue("kafs_lockd");
  29. if (!afs_lock_manager)
  30. return -ENOMEM;
  31. }
  32. return 0;
  33. }
  34. /*
  35. * destroy the lock manager thread if it's running
  36. */
  37. void __exit afs_kill_lock_manager(void)
  38. {
  39. if (afs_lock_manager)
  40. destroy_workqueue(afs_lock_manager);
  41. }
  42. /*
  43. * if the callback is broken on this vnode, then the lock may now be available
  44. */
  45. void afs_lock_may_be_available(struct afs_vnode *vnode)
  46. {
  47. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  48. queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
  49. }
  50. /*
  51. * the lock will time out in 5 minutes unless we extend it, so schedule
  52. * extension in a bit less than that time
  53. */
  54. static void afs_schedule_lock_extension(struct afs_vnode *vnode)
  55. {
  56. queue_delayed_work(afs_lock_manager, &vnode->lock_work,
  57. AFS_LOCKWAIT * HZ / 2);
  58. }
  59. /*
  60. * do work for a lock, including:
  61. * - probing for a lock we're waiting on but didn't get immediately
  62. * - extending a lock that's close to timing out
  63. */
  64. void afs_lock_work(struct work_struct *work)
  65. {
  66. struct afs_vnode *vnode =
  67. container_of(work, struct afs_vnode, lock_work.work);
  68. struct file_lock *fl;
  69. afs_lock_type_t type;
  70. struct key *key;
  71. int ret;
  72. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  73. spin_lock(&vnode->lock);
  74. if (test_bit(AFS_VNODE_UNLOCKING, &vnode->flags)) {
  75. _debug("unlock");
  76. spin_unlock(&vnode->lock);
  77. /* attempt to release the server lock; if it fails, we just
  78. * wait 5 minutes and it'll time out anyway */
  79. ret = afs_vnode_release_lock(vnode, vnode->unlock_key);
  80. if (ret < 0)
  81. printk(KERN_WARNING "AFS:"
  82. " Failed to release lock on {%x:%x} error %d\n",
  83. vnode->fid.vid, vnode->fid.vnode, ret);
  84. spin_lock(&vnode->lock);
  85. key_put(vnode->unlock_key);
  86. vnode->unlock_key = NULL;
  87. clear_bit(AFS_VNODE_UNLOCKING, &vnode->flags);
  88. }
  89. /* if we've got a lock, then it must be time to extend that lock as AFS
  90. * locks time out after 5 minutes */
  91. if (!list_empty(&vnode->granted_locks)) {
  92. _debug("extend");
  93. if (test_and_set_bit(AFS_VNODE_LOCKING, &vnode->flags))
  94. BUG();
  95. fl = list_entry(vnode->granted_locks.next,
  96. struct file_lock, fl_u.afs.link);
  97. key = key_get(fl->fl_file->private_data);
  98. spin_unlock(&vnode->lock);
  99. ret = afs_vnode_extend_lock(vnode, key);
  100. clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
  101. key_put(key);
  102. switch (ret) {
  103. case 0:
  104. afs_schedule_lock_extension(vnode);
  105. break;
  106. default:
  107. /* ummm... we failed to extend the lock - retry
  108. * extension shortly */
  109. printk(KERN_WARNING "AFS:"
  110. " Failed to extend lock on {%x:%x} error %d\n",
  111. vnode->fid.vid, vnode->fid.vnode, ret);
  112. queue_delayed_work(afs_lock_manager, &vnode->lock_work,
  113. HZ * 10);
  114. break;
  115. }
  116. _leave(" [extend]");
  117. return;
  118. }
  119. /* if we don't have a granted lock, then we must've been called back by
  120. * the server, and so if might be possible to get a lock we're
  121. * currently waiting for */
  122. if (!list_empty(&vnode->pending_locks)) {
  123. _debug("get");
  124. if (test_and_set_bit(AFS_VNODE_LOCKING, &vnode->flags))
  125. BUG();
  126. fl = list_entry(vnode->pending_locks.next,
  127. struct file_lock, fl_u.afs.link);
  128. key = key_get(fl->fl_file->private_data);
  129. type = (fl->fl_type == F_RDLCK) ?
  130. AFS_LOCK_READ : AFS_LOCK_WRITE;
  131. spin_unlock(&vnode->lock);
  132. ret = afs_vnode_set_lock(vnode, key, type);
  133. clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
  134. switch (ret) {
  135. case -EWOULDBLOCK:
  136. _debug("blocked");
  137. break;
  138. case 0:
  139. _debug("acquired");
  140. if (type == AFS_LOCK_READ)
  141. set_bit(AFS_VNODE_READLOCKED, &vnode->flags);
  142. else
  143. set_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
  144. ret = AFS_LOCK_GRANTED;
  145. default:
  146. spin_lock(&vnode->lock);
  147. /* the pending lock may have been withdrawn due to a
  148. * signal */
  149. if (list_entry(vnode->pending_locks.next,
  150. struct file_lock, fl_u.afs.link) == fl) {
  151. fl->fl_u.afs.state = ret;
  152. if (ret == AFS_LOCK_GRANTED)
  153. list_move_tail(&fl->fl_u.afs.link,
  154. &vnode->granted_locks);
  155. else
  156. list_del_init(&fl->fl_u.afs.link);
  157. wake_up(&fl->fl_wait);
  158. spin_unlock(&vnode->lock);
  159. } else {
  160. _debug("withdrawn");
  161. clear_bit(AFS_VNODE_READLOCKED, &vnode->flags);
  162. clear_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
  163. spin_unlock(&vnode->lock);
  164. afs_vnode_release_lock(vnode, key);
  165. if (!list_empty(&vnode->pending_locks))
  166. afs_lock_may_be_available(vnode);
  167. }
  168. break;
  169. }
  170. key_put(key);
  171. _leave(" [pend]");
  172. return;
  173. }
  174. /* looks like the lock request was withdrawn on a signal */
  175. spin_unlock(&vnode->lock);
  176. _leave(" [no locks]");
  177. }
  178. /*
  179. * pass responsibility for the unlocking of a vnode on the server to the
  180. * manager thread, lest a pending signal in the calling thread interrupt
  181. * AF_RXRPC
  182. * - the caller must hold the vnode lock
  183. */
  184. static void afs_defer_unlock(struct afs_vnode *vnode, struct key *key)
  185. {
  186. cancel_delayed_work(&vnode->lock_work);
  187. if (!test_and_clear_bit(AFS_VNODE_READLOCKED, &vnode->flags) &&
  188. !test_and_clear_bit(AFS_VNODE_WRITELOCKED, &vnode->flags))
  189. BUG();
  190. if (test_and_set_bit(AFS_VNODE_UNLOCKING, &vnode->flags))
  191. BUG();
  192. vnode->unlock_key = key_get(key);
  193. afs_lock_may_be_available(vnode);
  194. }
  195. /*
  196. * request a lock on a file on the server
  197. */
  198. static int afs_do_setlk(struct file *file, struct file_lock *fl)
  199. {
  200. struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host);
  201. afs_lock_type_t type;
  202. struct key *key = file->private_data;
  203. int ret;
  204. _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
  205. /* only whole-file locks are supported */
  206. if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
  207. return -EINVAL;
  208. ret = afs_init_lock_manager();
  209. if (ret < 0)
  210. return ret;
  211. fl->fl_ops = &afs_lock_ops;
  212. INIT_LIST_HEAD(&fl->fl_u.afs.link);
  213. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  214. type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
  215. lock_kernel();
  216. /* make sure we've got a callback on this file and that our view of the
  217. * data version is up to date */
  218. ret = afs_vnode_fetch_status(vnode, NULL, key);
  219. if (ret < 0)
  220. goto error;
  221. if (vnode->status.lock_count != 0 && !(fl->fl_flags & FL_SLEEP)) {
  222. ret = -EAGAIN;
  223. goto error;
  224. }
  225. spin_lock(&vnode->lock);
  226. if (list_empty(&vnode->pending_locks)) {
  227. /* if there's no-one else with a lock on this vnode, then we
  228. * need to ask the server for a lock */
  229. if (list_empty(&vnode->granted_locks)) {
  230. _debug("not locked");
  231. ASSERTCMP(vnode->flags &
  232. ((1 << AFS_VNODE_LOCKING) |
  233. (1 << AFS_VNODE_READLOCKED) |
  234. (1 << AFS_VNODE_WRITELOCKED)), ==, 0);
  235. list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
  236. set_bit(AFS_VNODE_LOCKING, &vnode->flags);
  237. spin_unlock(&vnode->lock);
  238. ret = afs_vnode_set_lock(vnode, key, type);
  239. clear_bit(AFS_VNODE_LOCKING, &vnode->flags);
  240. switch (ret) {
  241. case 0:
  242. goto acquired_server_lock;
  243. case -EWOULDBLOCK:
  244. spin_lock(&vnode->lock);
  245. ASSERT(list_empty(&vnode->granted_locks));
  246. ASSERTCMP(vnode->pending_locks.next, ==,
  247. &fl->fl_u.afs.link);
  248. goto wait;
  249. default:
  250. spin_lock(&vnode->lock);
  251. list_del_init(&fl->fl_u.afs.link);
  252. spin_unlock(&vnode->lock);
  253. goto error;
  254. }
  255. }
  256. /* if we've already got a readlock on the server and no waiting
  257. * writelocks, then we might be able to instantly grant another
  258. * readlock */
  259. if (type == AFS_LOCK_READ &&
  260. vnode->flags & (1 << AFS_VNODE_READLOCKED)) {
  261. _debug("instant readlock");
  262. ASSERTCMP(vnode->flags &
  263. ((1 << AFS_VNODE_LOCKING) |
  264. (1 << AFS_VNODE_WRITELOCKED)), ==, 0);
  265. ASSERT(!list_empty(&vnode->granted_locks));
  266. goto sharing_existing_lock;
  267. }
  268. }
  269. /* otherwise, we need to wait for a local lock to become available */
  270. _debug("wait local");
  271. list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
  272. wait:
  273. if (!(fl->fl_flags & FL_SLEEP)) {
  274. _debug("noblock");
  275. ret = -EAGAIN;
  276. goto abort_attempt;
  277. }
  278. spin_unlock(&vnode->lock);
  279. /* now we need to sleep and wait for the lock manager thread to get the
  280. * lock from the server */
  281. _debug("sleep");
  282. ret = wait_event_interruptible(fl->fl_wait,
  283. fl->fl_u.afs.state <= AFS_LOCK_GRANTED);
  284. if (fl->fl_u.afs.state <= AFS_LOCK_GRANTED) {
  285. ret = fl->fl_u.afs.state;
  286. if (ret < 0)
  287. goto error;
  288. spin_lock(&vnode->lock);
  289. goto given_lock;
  290. }
  291. /* we were interrupted, but someone may still be in the throes of
  292. * giving us the lock */
  293. _debug("intr");
  294. ASSERTCMP(ret, ==, -ERESTARTSYS);
  295. spin_lock(&vnode->lock);
  296. if (fl->fl_u.afs.state <= AFS_LOCK_GRANTED) {
  297. ret = fl->fl_u.afs.state;
  298. if (ret < 0) {
  299. spin_unlock(&vnode->lock);
  300. goto error;
  301. }
  302. goto given_lock;
  303. }
  304. abort_attempt:
  305. /* we aren't going to get the lock, either because we're unwilling to
  306. * wait, or because some signal happened */
  307. _debug("abort");
  308. if (list_empty(&vnode->granted_locks) &&
  309. vnode->pending_locks.next == &fl->fl_u.afs.link) {
  310. if (vnode->pending_locks.prev != &fl->fl_u.afs.link) {
  311. /* kick the next pending lock into having a go */
  312. list_del_init(&fl->fl_u.afs.link);
  313. afs_lock_may_be_available(vnode);
  314. }
  315. } else {
  316. list_del_init(&fl->fl_u.afs.link);
  317. }
  318. spin_unlock(&vnode->lock);
  319. goto error;
  320. acquired_server_lock:
  321. /* we've acquired a server lock, but it needs to be renewed after 5
  322. * mins */
  323. spin_lock(&vnode->lock);
  324. afs_schedule_lock_extension(vnode);
  325. if (type == AFS_LOCK_READ)
  326. set_bit(AFS_VNODE_READLOCKED, &vnode->flags);
  327. else
  328. set_bit(AFS_VNODE_WRITELOCKED, &vnode->flags);
  329. sharing_existing_lock:
  330. /* the lock has been granted as far as we're concerned... */
  331. fl->fl_u.afs.state = AFS_LOCK_GRANTED;
  332. list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
  333. given_lock:
  334. /* ... but we do still need to get the VFS's blessing */
  335. ASSERT(!(vnode->flags & (1 << AFS_VNODE_LOCKING)));
  336. ASSERT((vnode->flags & ((1 << AFS_VNODE_READLOCKED) |
  337. (1 << AFS_VNODE_WRITELOCKED))) != 0);
  338. ret = posix_lock_file(file, fl, NULL);
  339. if (ret < 0)
  340. goto vfs_rejected_lock;
  341. spin_unlock(&vnode->lock);
  342. /* again, make sure we've got a callback on this file and, again, make
  343. * sure that our view of the data version is up to date (we ignore
  344. * errors incurred here and deal with the consequences elsewhere) */
  345. afs_vnode_fetch_status(vnode, NULL, key);
  346. error:
  347. unlock_kernel();
  348. _leave(" = %d", ret);
  349. return ret;
  350. vfs_rejected_lock:
  351. /* the VFS rejected the lock we just obtained, so we have to discard
  352. * what we just got */
  353. _debug("vfs refused %d", ret);
  354. list_del_init(&fl->fl_u.afs.link);
  355. if (list_empty(&vnode->granted_locks))
  356. afs_defer_unlock(vnode, key);
  357. spin_unlock(&vnode->lock);
  358. goto abort_attempt;
  359. }
  360. /*
  361. * unlock on a file on the server
  362. */
  363. static int afs_do_unlk(struct file *file, struct file_lock *fl)
  364. {
  365. struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host);
  366. struct key *key = file->private_data;
  367. int ret;
  368. _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
  369. /* only whole-file unlocks are supported */
  370. if (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX)
  371. return -EINVAL;
  372. fl->fl_ops = &afs_lock_ops;
  373. INIT_LIST_HEAD(&fl->fl_u.afs.link);
  374. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  375. spin_lock(&vnode->lock);
  376. ret = posix_lock_file(file, fl, NULL);
  377. if (ret < 0) {
  378. spin_unlock(&vnode->lock);
  379. _leave(" = %d [vfs]", ret);
  380. return ret;
  381. }
  382. /* discard the server lock only if all granted locks are gone */
  383. if (list_empty(&vnode->granted_locks))
  384. afs_defer_unlock(vnode, key);
  385. spin_unlock(&vnode->lock);
  386. _leave(" = 0");
  387. return 0;
  388. }
  389. /*
  390. * return information about a lock we currently hold, if indeed we hold one
  391. */
  392. static int afs_do_getlk(struct file *file, struct file_lock *fl)
  393. {
  394. struct afs_vnode *vnode = AFS_FS_I(file->f_mapping->host);
  395. struct key *key = file->private_data;
  396. int ret, lock_count;
  397. _enter("");
  398. fl->fl_type = F_UNLCK;
  399. mutex_lock(&vnode->vfs_inode.i_mutex);
  400. /* check local lock records first */
  401. ret = 0;
  402. posix_test_lock(file, fl);
  403. if (fl->fl_type == F_UNLCK) {
  404. /* no local locks; consult the server */
  405. ret = afs_vnode_fetch_status(vnode, NULL, key);
  406. if (ret < 0)
  407. goto error;
  408. lock_count = vnode->status.lock_count;
  409. if (lock_count) {
  410. if (lock_count > 0)
  411. fl->fl_type = F_RDLCK;
  412. else
  413. fl->fl_type = F_WRLCK;
  414. fl->fl_start = 0;
  415. fl->fl_end = OFFSET_MAX;
  416. }
  417. }
  418. error:
  419. mutex_unlock(&vnode->vfs_inode.i_mutex);
  420. _leave(" = %d [%hd]", ret, fl->fl_type);
  421. return ret;
  422. }
  423. /*
  424. * manage POSIX locks on a file
  425. */
  426. int afs_lock(struct file *file, int cmd, struct file_lock *fl)
  427. {
  428. struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
  429. _enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
  430. vnode->fid.vid, vnode->fid.vnode, cmd,
  431. fl->fl_type, fl->fl_flags,
  432. (long long) fl->fl_start, (long long) fl->fl_end);
  433. /* AFS doesn't support mandatory locks */
  434. if ((vnode->vfs_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  435. fl->fl_type != F_UNLCK)
  436. return -ENOLCK;
  437. if (IS_GETLK(cmd))
  438. return afs_do_getlk(file, fl);
  439. if (fl->fl_type == F_UNLCK)
  440. return afs_do_unlk(file, fl);
  441. return afs_do_setlk(file, fl);
  442. }
  443. /*
  444. * manage FLOCK locks on a file
  445. */
  446. int afs_flock(struct file *file, int cmd, struct file_lock *fl)
  447. {
  448. struct afs_vnode *vnode = AFS_FS_I(file->f_dentry->d_inode);
  449. _enter("{%x:%u},%d,{t=%x,fl=%x}",
  450. vnode->fid.vid, vnode->fid.vnode, cmd,
  451. fl->fl_type, fl->fl_flags);
  452. /*
  453. * No BSD flocks over NFS allowed.
  454. * Note: we could try to fake a POSIX lock request here by
  455. * using ((u32) filp | 0x80000000) or some such as the pid.
  456. * Not sure whether that would be unique, though, or whether
  457. * that would break in other places.
  458. */
  459. if (!(fl->fl_flags & FL_FLOCK))
  460. return -ENOLCK;
  461. /* we're simulating flock() locks using posix locks on the server */
  462. fl->fl_owner = (fl_owner_t) file;
  463. fl->fl_start = 0;
  464. fl->fl_end = OFFSET_MAX;
  465. if (fl->fl_type == F_UNLCK)
  466. return afs_do_unlk(file, fl);
  467. return afs_do_setlk(file, fl);
  468. }
  469. /*
  470. * the POSIX lock management core VFS code copies the lock record and adds the
  471. * copy into its own list, so we need to add that copy to the vnode's lock
  472. * queue in the same place as the original (which will be deleted shortly
  473. * after)
  474. */
  475. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
  476. {
  477. _enter("");
  478. list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
  479. }
  480. /*
  481. * need to remove this lock from the vnode queue when it's removed from the
  482. * VFS's list
  483. */
  484. static void afs_fl_release_private(struct file_lock *fl)
  485. {
  486. _enter("");
  487. list_del_init(&fl->fl_u.afs.link);
  488. }