flock.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 "internal.h"
  12. #define AFS_LOCK_GRANTED 0
  13. #define AFS_LOCK_PENDING 1
  14. #define AFS_LOCK_YOUR_TRY 2
  15. struct workqueue_struct *afs_lock_manager;
  16. static void afs_next_locker(struct afs_vnode *vnode, int error);
  17. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
  18. static void afs_fl_release_private(struct file_lock *fl);
  19. static const struct file_lock_operations afs_lock_ops = {
  20. .fl_copy_lock = afs_fl_copy_lock,
  21. .fl_release_private = afs_fl_release_private,
  22. };
  23. static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state)
  24. {
  25. _debug("STATE %u -> %u", vnode->lock_state, state);
  26. vnode->lock_state = state;
  27. }
  28. /*
  29. * if the callback is broken on this vnode, then the lock may now be available
  30. */
  31. void afs_lock_may_be_available(struct afs_vnode *vnode)
  32. {
  33. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  34. spin_lock(&vnode->lock);
  35. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  36. afs_next_locker(vnode, 0);
  37. spin_unlock(&vnode->lock);
  38. }
  39. /*
  40. * the lock will time out in 5 minutes unless we extend it, so schedule
  41. * extension in a bit less than that time
  42. */
  43. static void __maybe_unused afs_schedule_lock_extension(struct afs_vnode *vnode)
  44. {
  45. queue_delayed_work(afs_lock_manager, &vnode->lock_work,
  46. AFS_LOCKWAIT * HZ / 2);
  47. }
  48. /*
  49. * grant one or more locks (readlocks are allowed to jump the queue if the
  50. * first lock in the queue is itself a readlock)
  51. * - the caller must hold the vnode lock
  52. */
  53. static void afs_grant_locks(struct afs_vnode *vnode)
  54. {
  55. struct file_lock *p, *_p;
  56. bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
  57. list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
  58. if (!exclusive && p->fl_type == F_WRLCK)
  59. continue;
  60. list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
  61. p->fl_u.afs.state = AFS_LOCK_GRANTED;
  62. wake_up(&p->fl_wait);
  63. }
  64. }
  65. /*
  66. * If an error is specified, reject every pending lock that matches the
  67. * authentication and type of the lock we failed to get. If there are any
  68. * remaining lockers, try to wake up one of them to have a go.
  69. */
  70. static void afs_next_locker(struct afs_vnode *vnode, int error)
  71. {
  72. struct file_lock *p, *_p, *next = NULL;
  73. struct key *key = vnode->lock_key;
  74. unsigned int fl_type = F_RDLCK;
  75. _enter("");
  76. if (vnode->lock_type == AFS_LOCK_WRITE)
  77. fl_type = F_WRLCK;
  78. list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
  79. if (error &&
  80. p->fl_type == fl_type &&
  81. afs_file_key(p->fl_file) == key) {
  82. list_del_init(&p->fl_u.afs.link);
  83. p->fl_u.afs.state = error;
  84. wake_up(&p->fl_wait);
  85. }
  86. /* Select the next locker to hand off to. */
  87. if (next &&
  88. (next->fl_type == F_WRLCK || p->fl_type == F_RDLCK))
  89. continue;
  90. next = p;
  91. }
  92. vnode->lock_key = NULL;
  93. key_put(key);
  94. if (next) {
  95. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  96. next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
  97. wake_up(&next->fl_wait);
  98. } else {
  99. afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
  100. }
  101. _leave("");
  102. }
  103. /*
  104. * Get a lock on a file
  105. */
  106. static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
  107. afs_lock_type_t type)
  108. {
  109. struct afs_fs_cursor fc;
  110. int ret;
  111. _enter("%s{%x:%u.%u},%x,%u",
  112. vnode->volume->name,
  113. vnode->fid.vid,
  114. vnode->fid.vnode,
  115. vnode->fid.unique,
  116. key_serial(key), type);
  117. ret = -ERESTARTSYS;
  118. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  119. while (afs_select_fileserver(&fc)) {
  120. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  121. afs_fs_set_lock(&fc, type);
  122. }
  123. afs_check_for_remote_deletion(&fc, fc.vnode);
  124. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  125. ret = afs_end_vnode_operation(&fc);
  126. }
  127. _leave(" = %d", ret);
  128. return ret;
  129. }
  130. /*
  131. * Extend a lock on a file
  132. */
  133. static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
  134. {
  135. struct afs_fs_cursor fc;
  136. int ret;
  137. _enter("%s{%x:%u.%u},%x",
  138. vnode->volume->name,
  139. vnode->fid.vid,
  140. vnode->fid.vnode,
  141. vnode->fid.unique,
  142. key_serial(key));
  143. ret = -ERESTARTSYS;
  144. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  145. while (afs_select_current_fileserver(&fc)) {
  146. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  147. afs_fs_extend_lock(&fc);
  148. }
  149. afs_check_for_remote_deletion(&fc, fc.vnode);
  150. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  151. ret = afs_end_vnode_operation(&fc);
  152. }
  153. _leave(" = %d", ret);
  154. return ret;
  155. }
  156. /*
  157. * Release a lock on a file
  158. */
  159. static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
  160. {
  161. struct afs_fs_cursor fc;
  162. int ret;
  163. _enter("%s{%x:%u.%u},%x",
  164. vnode->volume->name,
  165. vnode->fid.vid,
  166. vnode->fid.vnode,
  167. vnode->fid.unique,
  168. key_serial(key));
  169. ret = -ERESTARTSYS;
  170. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  171. while (afs_select_current_fileserver(&fc)) {
  172. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  173. afs_fs_release_lock(&fc);
  174. }
  175. afs_check_for_remote_deletion(&fc, fc.vnode);
  176. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  177. ret = afs_end_vnode_operation(&fc);
  178. }
  179. _leave(" = %d", ret);
  180. return ret;
  181. }
  182. /*
  183. * do work for a lock, including:
  184. * - probing for a lock we're waiting on but didn't get immediately
  185. * - extending a lock that's close to timing out
  186. */
  187. void afs_lock_work(struct work_struct *work)
  188. {
  189. struct afs_vnode *vnode =
  190. container_of(work, struct afs_vnode, lock_work.work);
  191. struct key *key;
  192. int ret;
  193. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  194. spin_lock(&vnode->lock);
  195. again:
  196. _debug("wstate %u for %p", vnode->lock_state, vnode);
  197. switch (vnode->lock_state) {
  198. case AFS_VNODE_LOCK_NEED_UNLOCK:
  199. _debug("unlock");
  200. afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING);
  201. spin_unlock(&vnode->lock);
  202. /* attempt to release the server lock; if it fails, we just
  203. * wait 5 minutes and it'll expire anyway */
  204. ret = afs_release_lock(vnode, vnode->lock_key);
  205. if (ret < 0)
  206. printk(KERN_WARNING "AFS:"
  207. " Failed to release lock on {%x:%x} error %d\n",
  208. vnode->fid.vid, vnode->fid.vnode, ret);
  209. spin_lock(&vnode->lock);
  210. afs_next_locker(vnode, 0);
  211. spin_unlock(&vnode->lock);
  212. return;
  213. /* If we've already got a lock, then it must be time to extend that
  214. * lock as AFS locks time out after 5 minutes.
  215. */
  216. case AFS_VNODE_LOCK_GRANTED:
  217. _debug("extend");
  218. ASSERT(!list_empty(&vnode->granted_locks));
  219. key = key_get(vnode->lock_key);
  220. afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING);
  221. spin_unlock(&vnode->lock);
  222. ret = afs_extend_lock(vnode, key); /* RPC */
  223. key_put(key);
  224. if (ret < 0)
  225. pr_warning("AFS: Failed to extend lock on {%x:%x} error %d\n",
  226. vnode->fid.vid, vnode->fid.vnode, ret);
  227. spin_lock(&vnode->lock);
  228. if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
  229. goto again;
  230. afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
  231. if (ret != 0)
  232. queue_delayed_work(afs_lock_manager, &vnode->lock_work,
  233. HZ * 10);
  234. spin_unlock(&vnode->lock);
  235. _leave(" [ext]");
  236. return;
  237. /* If we're waiting for a callback to indicate lock release, we can't
  238. * actually rely on this, so need to recheck at regular intervals. The
  239. * problem is that the server might not notify us if the lock just
  240. * expires (say because a client died) rather than being explicitly
  241. * released.
  242. */
  243. case AFS_VNODE_LOCK_WAITING_FOR_CB:
  244. _debug("retry");
  245. afs_next_locker(vnode, 0);
  246. spin_unlock(&vnode->lock);
  247. return;
  248. default:
  249. /* Looks like a lock request was withdrawn. */
  250. spin_unlock(&vnode->lock);
  251. _leave(" [no]");
  252. return;
  253. }
  254. }
  255. /*
  256. * pass responsibility for the unlocking of a vnode on the server to the
  257. * manager thread, lest a pending signal in the calling thread interrupt
  258. * AF_RXRPC
  259. * - the caller must hold the vnode lock
  260. */
  261. static void afs_defer_unlock(struct afs_vnode *vnode)
  262. {
  263. _enter("%u", vnode->lock_state);
  264. if (list_empty(&vnode->granted_locks) &&
  265. (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
  266. vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) {
  267. cancel_delayed_work(&vnode->lock_work);
  268. afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK);
  269. queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
  270. }
  271. }
  272. /*
  273. * Check that our view of the file metadata is up to date and check to see
  274. * whether we think that we have a locking permit.
  275. */
  276. static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
  277. afs_lock_type_t type, bool can_sleep)
  278. {
  279. afs_access_t access;
  280. int ret;
  281. /* Make sure we've got a callback on this file and that our view of the
  282. * data version is up to date.
  283. */
  284. ret = afs_validate(vnode, key);
  285. if (ret < 0)
  286. return ret;
  287. /* Check the permission set to see if we're actually going to be
  288. * allowed to get a lock on this file.
  289. */
  290. ret = afs_check_permit(vnode, key, &access);
  291. if (ret < 0)
  292. return ret;
  293. /* At a rough estimation, you need LOCK, WRITE or INSERT perm to
  294. * read-lock a file and WRITE or INSERT perm to write-lock a file.
  295. *
  296. * We can't rely on the server to do this for us since if we want to
  297. * share a read lock that we already have, we won't go the server.
  298. */
  299. if (type == AFS_LOCK_READ) {
  300. if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
  301. return -EACCES;
  302. if (vnode->status.lock_count == -1 && !can_sleep)
  303. return -EAGAIN; /* Write locked */
  304. } else {
  305. if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
  306. return -EACCES;
  307. if (vnode->status.lock_count != 0 && !can_sleep)
  308. return -EAGAIN; /* Locked */
  309. }
  310. return 0;
  311. }
  312. /*
  313. * request a lock on a file on the server
  314. */
  315. static int afs_do_setlk(struct file *file, struct file_lock *fl)
  316. {
  317. struct inode *inode = locks_inode(file);
  318. struct afs_vnode *vnode = AFS_FS_I(inode);
  319. afs_lock_type_t type;
  320. struct key *key = afs_file_key(file);
  321. int ret;
  322. _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
  323. fl->fl_ops = &afs_lock_ops;
  324. INIT_LIST_HEAD(&fl->fl_u.afs.link);
  325. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  326. type = (fl->fl_type == F_RDLCK) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
  327. ret = afs_do_setlk_check(vnode, key, type, fl->fl_flags & FL_SLEEP);
  328. if (ret < 0)
  329. return ret;
  330. spin_lock(&vnode->lock);
  331. list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
  332. /* If we've already got a lock on the server then try to move to having
  333. * the VFS grant the requested lock. Note that this means that other
  334. * clients may get starved out.
  335. */
  336. _debug("try %u", vnode->lock_state);
  337. if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) {
  338. if (type == AFS_LOCK_READ) {
  339. _debug("instant readlock");
  340. list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
  341. fl->fl_u.afs.state = AFS_LOCK_GRANTED;
  342. goto vnode_is_locked_u;
  343. }
  344. if (vnode->lock_type == AFS_LOCK_WRITE) {
  345. _debug("instant writelock");
  346. list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
  347. fl->fl_u.afs.state = AFS_LOCK_GRANTED;
  348. goto vnode_is_locked_u;
  349. }
  350. }
  351. if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
  352. goto need_to_wait;
  353. try_to_lock:
  354. /* We don't have a lock on this vnode and we aren't currently waiting
  355. * for one either, so ask the server for a lock.
  356. *
  357. * Note that we need to be careful if we get interrupted by a signal
  358. * after dispatching the request as we may still get the lock, even
  359. * though we don't wait for the reply (it's not too bad a problem - the
  360. * lock will expire in 5 mins anyway).
  361. */
  362. _debug("not locked");
  363. vnode->lock_key = key_get(key);
  364. vnode->lock_type = type;
  365. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  366. spin_unlock(&vnode->lock);
  367. ret = afs_set_lock(vnode, key, type); /* RPC */
  368. spin_lock(&vnode->lock);
  369. switch (ret) {
  370. case -EKEYREJECTED:
  371. case -EKEYEXPIRED:
  372. case -EKEYREVOKED:
  373. case -EPERM:
  374. case -EACCES:
  375. fl->fl_u.afs.state = ret;
  376. list_del_init(&fl->fl_u.afs.link);
  377. afs_next_locker(vnode, ret);
  378. goto error_unlock;
  379. default:
  380. fl->fl_u.afs.state = ret;
  381. list_del_init(&fl->fl_u.afs.link);
  382. afs_next_locker(vnode, 0);
  383. goto error_unlock;
  384. case -EWOULDBLOCK:
  385. /* The server doesn't have a lock-waiting queue, so the client
  386. * will have to retry. The server will break the outstanding
  387. * callbacks on a file when a lock is released.
  388. */
  389. _debug("would block");
  390. ASSERT(list_empty(&vnode->granted_locks));
  391. ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
  392. goto lock_is_contended;
  393. case 0:
  394. _debug("acquired");
  395. afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
  396. afs_grant_locks(vnode);
  397. goto vnode_is_locked_u;
  398. }
  399. vnode_is_locked_u:
  400. spin_unlock(&vnode->lock);
  401. vnode_is_locked:
  402. /* the lock has been granted by the server... */
  403. ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED);
  404. /* ... but the VFS still needs to distribute access on this client. */
  405. ret = locks_lock_file_wait(file, fl);
  406. if (ret < 0)
  407. goto vfs_rejected_lock;
  408. /* Again, make sure we've got a callback on this file and, again, make
  409. * sure that our view of the data version is up to date (we ignore
  410. * errors incurred here and deal with the consequences elsewhere).
  411. */
  412. afs_validate(vnode, key);
  413. _leave(" = 0");
  414. return 0;
  415. lock_is_contended:
  416. if (!(fl->fl_flags & FL_SLEEP)) {
  417. list_del_init(&fl->fl_u.afs.link);
  418. afs_next_locker(vnode, 0);
  419. ret = -EAGAIN;
  420. goto error_unlock;
  421. }
  422. afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB);
  423. queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5);
  424. need_to_wait:
  425. /* We're going to have to wait. Either this client doesn't have a lock
  426. * on the server yet and we need to wait for a callback to occur, or
  427. * the client does have a lock on the server, but it's shared and we
  428. * need an exclusive lock.
  429. */
  430. spin_unlock(&vnode->lock);
  431. _debug("sleep");
  432. ret = wait_event_interruptible(fl->fl_wait,
  433. fl->fl_u.afs.state != AFS_LOCK_PENDING);
  434. _debug("wait = %d", ret);
  435. if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) {
  436. spin_lock(&vnode->lock);
  437. switch (fl->fl_u.afs.state) {
  438. case AFS_LOCK_YOUR_TRY:
  439. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  440. goto try_to_lock;
  441. case AFS_LOCK_PENDING:
  442. if (ret > 0) {
  443. /* We need to retry the lock. We may not be
  444. * notified by the server if it just expired
  445. * rather than being released.
  446. */
  447. ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB);
  448. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  449. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  450. goto try_to_lock;
  451. }
  452. goto error_unlock;
  453. case AFS_LOCK_GRANTED:
  454. default:
  455. break;
  456. }
  457. spin_unlock(&vnode->lock);
  458. }
  459. if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
  460. goto vnode_is_locked;
  461. ret = fl->fl_u.afs.state;
  462. goto error;
  463. vfs_rejected_lock:
  464. /* The VFS rejected the lock we just obtained, so we have to discard
  465. * what we just got. We defer this to the lock manager work item to
  466. * deal with.
  467. */
  468. _debug("vfs refused %d", ret);
  469. spin_lock(&vnode->lock);
  470. list_del_init(&fl->fl_u.afs.link);
  471. afs_defer_unlock(vnode);
  472. error_unlock:
  473. spin_unlock(&vnode->lock);
  474. error:
  475. _leave(" = %d", ret);
  476. return ret;
  477. }
  478. /*
  479. * unlock on a file on the server
  480. */
  481. static int afs_do_unlk(struct file *file, struct file_lock *fl)
  482. {
  483. struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
  484. int ret;
  485. _enter("{%x:%u},%u", vnode->fid.vid, vnode->fid.vnode, fl->fl_type);
  486. /* Flush all pending writes before doing anything with locks. */
  487. vfs_fsync(file, 0);
  488. ret = locks_lock_file_wait(file, fl);
  489. _leave(" = %d [%u]", ret, vnode->lock_state);
  490. return ret;
  491. }
  492. /*
  493. * return information about a lock we currently hold, if indeed we hold one
  494. */
  495. static int afs_do_getlk(struct file *file, struct file_lock *fl)
  496. {
  497. struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
  498. struct key *key = afs_file_key(file);
  499. int ret, lock_count;
  500. _enter("");
  501. fl->fl_type = F_UNLCK;
  502. /* check local lock records first */
  503. posix_test_lock(file, fl);
  504. if (fl->fl_type == F_UNLCK) {
  505. /* no local locks; consult the server */
  506. ret = afs_fetch_status(vnode, key, false);
  507. if (ret < 0)
  508. goto error;
  509. lock_count = READ_ONCE(vnode->status.lock_count);
  510. if (lock_count != 0) {
  511. if (lock_count > 0)
  512. fl->fl_type = F_RDLCK;
  513. else
  514. fl->fl_type = F_WRLCK;
  515. fl->fl_start = 0;
  516. fl->fl_end = OFFSET_MAX;
  517. fl->fl_pid = 0;
  518. }
  519. }
  520. ret = 0;
  521. error:
  522. _leave(" = %d [%hd]", ret, fl->fl_type);
  523. return ret;
  524. }
  525. /*
  526. * manage POSIX locks on a file
  527. */
  528. int afs_lock(struct file *file, int cmd, struct file_lock *fl)
  529. {
  530. struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
  531. _enter("{%x:%u},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
  532. vnode->fid.vid, vnode->fid.vnode, cmd,
  533. fl->fl_type, fl->fl_flags,
  534. (long long) fl->fl_start, (long long) fl->fl_end);
  535. /* AFS doesn't support mandatory locks */
  536. if (__mandatory_lock(&vnode->vfs_inode) && fl->fl_type != F_UNLCK)
  537. return -ENOLCK;
  538. if (IS_GETLK(cmd))
  539. return afs_do_getlk(file, fl);
  540. if (fl->fl_type == F_UNLCK)
  541. return afs_do_unlk(file, fl);
  542. return afs_do_setlk(file, fl);
  543. }
  544. /*
  545. * manage FLOCK locks on a file
  546. */
  547. int afs_flock(struct file *file, int cmd, struct file_lock *fl)
  548. {
  549. struct afs_vnode *vnode = AFS_FS_I(locks_inode(file));
  550. _enter("{%x:%u},%d,{t=%x,fl=%x}",
  551. vnode->fid.vid, vnode->fid.vnode, cmd,
  552. fl->fl_type, fl->fl_flags);
  553. /*
  554. * No BSD flocks over NFS allowed.
  555. * Note: we could try to fake a POSIX lock request here by
  556. * using ((u32) filp | 0x80000000) or some such as the pid.
  557. * Not sure whether that would be unique, though, or whether
  558. * that would break in other places.
  559. */
  560. if (!(fl->fl_flags & FL_FLOCK))
  561. return -ENOLCK;
  562. /* we're simulating flock() locks using posix locks on the server */
  563. if (fl->fl_type == F_UNLCK)
  564. return afs_do_unlk(file, fl);
  565. return afs_do_setlk(file, fl);
  566. }
  567. /*
  568. * the POSIX lock management core VFS code copies the lock record and adds the
  569. * copy into its own list, so we need to add that copy to the vnode's lock
  570. * queue in the same place as the original (which will be deleted shortly
  571. * after)
  572. */
  573. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
  574. {
  575. struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
  576. _enter("");
  577. spin_lock(&vnode->lock);
  578. list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
  579. spin_unlock(&vnode->lock);
  580. }
  581. /*
  582. * need to remove this lock from the vnode queue when it's removed from the
  583. * VFS's list
  584. */
  585. static void afs_fl_release_private(struct file_lock *fl)
  586. {
  587. struct afs_vnode *vnode = AFS_FS_I(locks_inode(fl->fl_file));
  588. _enter("");
  589. spin_lock(&vnode->lock);
  590. list_del_init(&fl->fl_u.afs.link);
  591. if (list_empty(&vnode->granted_locks))
  592. afs_defer_unlock(vnode);
  593. _debug("state %u for %p", vnode->lock_state, vnode);
  594. spin_unlock(&vnode->lock);
  595. }