flock.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS file locking support
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include "internal.h"
  8. #define AFS_LOCK_GRANTED 0
  9. #define AFS_LOCK_PENDING 1
  10. #define AFS_LOCK_YOUR_TRY 2
  11. struct workqueue_struct *afs_lock_manager;
  12. static void afs_next_locker(struct afs_vnode *vnode, int error);
  13. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl);
  14. static void afs_fl_release_private(struct file_lock *fl);
  15. static const struct file_lock_operations afs_lock_ops = {
  16. .fl_copy_lock = afs_fl_copy_lock,
  17. .fl_release_private = afs_fl_release_private,
  18. };
  19. static inline void afs_set_lock_state(struct afs_vnode *vnode, enum afs_lock_state state)
  20. {
  21. _debug("STATE %u -> %u", vnode->lock_state, state);
  22. vnode->lock_state = state;
  23. }
  24. static atomic_t afs_file_lock_debug_id;
  25. /*
  26. * if the callback is broken on this vnode, then the lock may now be available
  27. */
  28. void afs_lock_may_be_available(struct afs_vnode *vnode)
  29. {
  30. _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
  31. spin_lock(&vnode->lock);
  32. if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
  33. afs_next_locker(vnode, 0);
  34. trace_afs_flock_ev(vnode, NULL, afs_flock_callback_break, 0);
  35. spin_unlock(&vnode->lock);
  36. }
  37. /*
  38. * the lock will time out in 5 minutes unless we extend it, so schedule
  39. * extension in a bit less than that time
  40. */
  41. static void afs_schedule_lock_extension(struct afs_vnode *vnode)
  42. {
  43. ktime_t expires_at, now, duration;
  44. u64 duration_j;
  45. expires_at = ktime_add_ms(vnode->locked_at, AFS_LOCKWAIT * 1000 / 2);
  46. now = ktime_get_real();
  47. duration = ktime_sub(expires_at, now);
  48. if (duration <= 0)
  49. duration_j = 0;
  50. else
  51. duration_j = nsecs_to_jiffies(ktime_to_ns(duration));
  52. queue_delayed_work(afs_lock_manager, &vnode->lock_work, duration_j);
  53. }
  54. /*
  55. * In the case of successful completion of a lock operation, record the time
  56. * the reply appeared and start the lock extension timer.
  57. */
  58. void afs_lock_op_done(struct afs_call *call)
  59. {
  60. struct afs_operation *op = call->op;
  61. struct afs_vnode *vnode = op->file[0].vnode;
  62. if (call->error == 0) {
  63. spin_lock(&vnode->lock);
  64. trace_afs_flock_ev(vnode, NULL, afs_flock_timestamp, 0);
  65. vnode->locked_at = call->issue_time;
  66. afs_schedule_lock_extension(vnode);
  67. spin_unlock(&vnode->lock);
  68. }
  69. }
  70. /*
  71. * grant one or more locks (readlocks are allowed to jump the queue if the
  72. * first lock in the queue is itself a readlock)
  73. * - the caller must hold the vnode lock
  74. */
  75. static void afs_grant_locks(struct afs_vnode *vnode)
  76. {
  77. struct file_lock *p, *_p;
  78. bool exclusive = (vnode->lock_type == AFS_LOCK_WRITE);
  79. list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
  80. if (!exclusive && lock_is_write(p))
  81. continue;
  82. list_move_tail(&p->fl_u.afs.link, &vnode->granted_locks);
  83. p->fl_u.afs.state = AFS_LOCK_GRANTED;
  84. trace_afs_flock_op(vnode, p, afs_flock_op_grant);
  85. locks_wake_up(p);
  86. }
  87. }
  88. /*
  89. * If an error is specified, reject every pending lock that matches the
  90. * authentication and type of the lock we failed to get. If there are any
  91. * remaining lockers, try to wake up one of them to have a go.
  92. */
  93. static void afs_next_locker(struct afs_vnode *vnode, int error)
  94. {
  95. struct file_lock *p, *_p, *next = NULL;
  96. struct key *key = vnode->lock_key;
  97. unsigned int type = F_RDLCK;
  98. _enter("");
  99. if (vnode->lock_type == AFS_LOCK_WRITE)
  100. type = F_WRLCK;
  101. list_for_each_entry_safe(p, _p, &vnode->pending_locks, fl_u.afs.link) {
  102. if (error &&
  103. p->c.flc_type == type &&
  104. afs_file_key(p->c.flc_file) == key) {
  105. list_del_init(&p->fl_u.afs.link);
  106. p->fl_u.afs.state = error;
  107. locks_wake_up(p);
  108. }
  109. /* Select the next locker to hand off to. */
  110. if (next && (lock_is_write(next) || lock_is_read(p)))
  111. continue;
  112. next = p;
  113. }
  114. vnode->lock_key = NULL;
  115. key_put(key);
  116. if (next) {
  117. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  118. next->fl_u.afs.state = AFS_LOCK_YOUR_TRY;
  119. trace_afs_flock_op(vnode, next, afs_flock_op_wake);
  120. locks_wake_up(next);
  121. } else {
  122. afs_set_lock_state(vnode, AFS_VNODE_LOCK_NONE);
  123. trace_afs_flock_ev(vnode, NULL, afs_flock_no_lockers, 0);
  124. }
  125. _leave("");
  126. }
  127. /*
  128. * Kill off all waiters in the the pending lock queue due to the vnode being
  129. * deleted.
  130. */
  131. static void afs_kill_lockers_enoent(struct afs_vnode *vnode)
  132. {
  133. struct file_lock *p;
  134. afs_set_lock_state(vnode, AFS_VNODE_LOCK_DELETED);
  135. while (!list_empty(&vnode->pending_locks)) {
  136. p = list_entry(vnode->pending_locks.next,
  137. struct file_lock, fl_u.afs.link);
  138. list_del_init(&p->fl_u.afs.link);
  139. p->fl_u.afs.state = -ENOENT;
  140. locks_wake_up(p);
  141. }
  142. key_put(vnode->lock_key);
  143. vnode->lock_key = NULL;
  144. }
  145. static void afs_lock_success(struct afs_operation *op)
  146. {
  147. _enter("op=%08x", op->debug_id);
  148. afs_vnode_commit_status(op, &op->file[0]);
  149. }
  150. static const struct afs_operation_ops afs_set_lock_operation = {
  151. .issue_afs_rpc = afs_fs_set_lock,
  152. .issue_yfs_rpc = yfs_fs_set_lock,
  153. .success = afs_lock_success,
  154. .aborted = afs_check_for_remote_deletion,
  155. };
  156. /*
  157. * Get a lock on a file
  158. */
  159. static int afs_set_lock(struct afs_vnode *vnode, struct key *key,
  160. afs_lock_type_t type)
  161. {
  162. struct afs_operation *op;
  163. _enter("%s{%llx:%llu.%u},%x,%u",
  164. vnode->volume->name,
  165. vnode->fid.vid,
  166. vnode->fid.vnode,
  167. vnode->fid.unique,
  168. key_serial(key), type);
  169. op = afs_alloc_operation(key, vnode->volume);
  170. if (IS_ERR(op))
  171. return PTR_ERR(op);
  172. afs_op_set_vnode(op, 0, vnode);
  173. op->lock.type = type;
  174. op->ops = &afs_set_lock_operation;
  175. return afs_do_sync_operation(op);
  176. }
  177. static const struct afs_operation_ops afs_extend_lock_operation = {
  178. .issue_afs_rpc = afs_fs_extend_lock,
  179. .issue_yfs_rpc = yfs_fs_extend_lock,
  180. .success = afs_lock_success,
  181. };
  182. /*
  183. * Extend a lock on a file
  184. */
  185. static int afs_extend_lock(struct afs_vnode *vnode, struct key *key)
  186. {
  187. struct afs_operation *op;
  188. _enter("%s{%llx:%llu.%u},%x",
  189. vnode->volume->name,
  190. vnode->fid.vid,
  191. vnode->fid.vnode,
  192. vnode->fid.unique,
  193. key_serial(key));
  194. op = afs_alloc_operation(key, vnode->volume);
  195. if (IS_ERR(op))
  196. return PTR_ERR(op);
  197. afs_op_set_vnode(op, 0, vnode);
  198. op->flags |= AFS_OPERATION_UNINTR;
  199. op->ops = &afs_extend_lock_operation;
  200. return afs_do_sync_operation(op);
  201. }
  202. static const struct afs_operation_ops afs_release_lock_operation = {
  203. .issue_afs_rpc = afs_fs_release_lock,
  204. .issue_yfs_rpc = yfs_fs_release_lock,
  205. .success = afs_lock_success,
  206. };
  207. /*
  208. * Release a lock on a file
  209. */
  210. static int afs_release_lock(struct afs_vnode *vnode, struct key *key)
  211. {
  212. struct afs_operation *op;
  213. _enter("%s{%llx:%llu.%u},%x",
  214. vnode->volume->name,
  215. vnode->fid.vid,
  216. vnode->fid.vnode,
  217. vnode->fid.unique,
  218. key_serial(key));
  219. op = afs_alloc_operation(key, vnode->volume);
  220. if (IS_ERR(op))
  221. return PTR_ERR(op);
  222. afs_op_set_vnode(op, 0, vnode);
  223. op->flags |= AFS_OPERATION_UNINTR;
  224. op->ops = &afs_release_lock_operation;
  225. return afs_do_sync_operation(op);
  226. }
  227. /*
  228. * do work for a lock, including:
  229. * - probing for a lock we're waiting on but didn't get immediately
  230. * - extending a lock that's close to timing out
  231. */
  232. void afs_lock_work(struct work_struct *work)
  233. {
  234. struct afs_vnode *vnode =
  235. container_of(work, struct afs_vnode, lock_work.work);
  236. struct key *key;
  237. int ret;
  238. _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
  239. spin_lock(&vnode->lock);
  240. again:
  241. _debug("wstate %u for %p", vnode->lock_state, vnode);
  242. switch (vnode->lock_state) {
  243. case AFS_VNODE_LOCK_NEED_UNLOCK:
  244. afs_set_lock_state(vnode, AFS_VNODE_LOCK_UNLOCKING);
  245. trace_afs_flock_ev(vnode, NULL, afs_flock_work_unlocking, 0);
  246. spin_unlock(&vnode->lock);
  247. /* attempt to release the server lock; if it fails, we just
  248. * wait 5 minutes and it'll expire anyway */
  249. ret = afs_release_lock(vnode, vnode->lock_key);
  250. if (ret < 0 && vnode->lock_state != AFS_VNODE_LOCK_DELETED) {
  251. trace_afs_flock_ev(vnode, NULL, afs_flock_release_fail,
  252. ret);
  253. printk(KERN_WARNING "AFS:"
  254. " Failed to release lock on {%llx:%llx} error %d\n",
  255. vnode->fid.vid, vnode->fid.vnode, ret);
  256. }
  257. spin_lock(&vnode->lock);
  258. if (ret == -ENOENT)
  259. afs_kill_lockers_enoent(vnode);
  260. else
  261. afs_next_locker(vnode, 0);
  262. spin_unlock(&vnode->lock);
  263. return;
  264. /* If we've already got a lock, then it must be time to extend that
  265. * lock as AFS locks time out after 5 minutes.
  266. */
  267. case AFS_VNODE_LOCK_GRANTED:
  268. _debug("extend");
  269. ASSERT(!list_empty(&vnode->granted_locks));
  270. key = key_get(vnode->lock_key);
  271. afs_set_lock_state(vnode, AFS_VNODE_LOCK_EXTENDING);
  272. trace_afs_flock_ev(vnode, NULL, afs_flock_work_extending, 0);
  273. spin_unlock(&vnode->lock);
  274. ret = afs_extend_lock(vnode, key); /* RPC */
  275. key_put(key);
  276. if (ret < 0) {
  277. trace_afs_flock_ev(vnode, NULL, afs_flock_extend_fail,
  278. ret);
  279. pr_warn("AFS: Failed to extend lock on {%llx:%llx} error %d\n",
  280. vnode->fid.vid, vnode->fid.vnode, ret);
  281. }
  282. spin_lock(&vnode->lock);
  283. if (ret == -ENOENT) {
  284. afs_kill_lockers_enoent(vnode);
  285. spin_unlock(&vnode->lock);
  286. return;
  287. }
  288. if (vnode->lock_state != AFS_VNODE_LOCK_EXTENDING)
  289. goto again;
  290. afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
  291. if (ret != 0)
  292. queue_delayed_work(afs_lock_manager, &vnode->lock_work,
  293. HZ * 10);
  294. spin_unlock(&vnode->lock);
  295. _leave(" [ext]");
  296. return;
  297. /* If we're waiting for a callback to indicate lock release, we can't
  298. * actually rely on this, so need to recheck at regular intervals. The
  299. * problem is that the server might not notify us if the lock just
  300. * expires (say because a client died) rather than being explicitly
  301. * released.
  302. */
  303. case AFS_VNODE_LOCK_WAITING_FOR_CB:
  304. _debug("retry");
  305. afs_next_locker(vnode, 0);
  306. spin_unlock(&vnode->lock);
  307. return;
  308. case AFS_VNODE_LOCK_DELETED:
  309. afs_kill_lockers_enoent(vnode);
  310. spin_unlock(&vnode->lock);
  311. return;
  312. default:
  313. /* Looks like a lock request was withdrawn. */
  314. spin_unlock(&vnode->lock);
  315. _leave(" [no]");
  316. return;
  317. }
  318. }
  319. /*
  320. * pass responsibility for the unlocking of a vnode on the server to the
  321. * manager thread, lest a pending signal in the calling thread interrupt
  322. * AF_RXRPC
  323. * - the caller must hold the vnode lock
  324. */
  325. static void afs_defer_unlock(struct afs_vnode *vnode)
  326. {
  327. _enter("%u", vnode->lock_state);
  328. if (list_empty(&vnode->granted_locks) &&
  329. (vnode->lock_state == AFS_VNODE_LOCK_GRANTED ||
  330. vnode->lock_state == AFS_VNODE_LOCK_EXTENDING)) {
  331. cancel_delayed_work(&vnode->lock_work);
  332. afs_set_lock_state(vnode, AFS_VNODE_LOCK_NEED_UNLOCK);
  333. trace_afs_flock_ev(vnode, NULL, afs_flock_defer_unlock, 0);
  334. queue_delayed_work(afs_lock_manager, &vnode->lock_work, 0);
  335. }
  336. }
  337. /*
  338. * Check that our view of the file metadata is up to date and check to see
  339. * whether we think that we have a locking permit.
  340. */
  341. static int afs_do_setlk_check(struct afs_vnode *vnode, struct key *key,
  342. enum afs_flock_mode mode, afs_lock_type_t type)
  343. {
  344. afs_access_t access;
  345. int ret;
  346. /* Make sure we've got a callback on this file and that our view of the
  347. * data version is up to date.
  348. */
  349. ret = afs_validate(vnode, key);
  350. if (ret < 0)
  351. return ret;
  352. /* Check the permission set to see if we're actually going to be
  353. * allowed to get a lock on this file.
  354. */
  355. ret = afs_check_permit(vnode, key, &access);
  356. if (ret < 0)
  357. return ret;
  358. /* At a rough estimation, you need LOCK, WRITE or INSERT perm to
  359. * read-lock a file and WRITE or INSERT perm to write-lock a file.
  360. *
  361. * We can't rely on the server to do this for us since if we want to
  362. * share a read lock that we already have, we won't go the server.
  363. */
  364. if (type == AFS_LOCK_READ) {
  365. if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE | AFS_ACE_LOCK)))
  366. return -EACCES;
  367. } else {
  368. if (!(access & (AFS_ACE_INSERT | AFS_ACE_WRITE)))
  369. return -EACCES;
  370. }
  371. return 0;
  372. }
  373. /*
  374. * request a lock on a file on the server
  375. */
  376. static int afs_do_setlk(struct file *file, struct file_lock *fl)
  377. {
  378. struct inode *inode = file_inode(file);
  379. struct afs_vnode *vnode = AFS_FS_I(inode);
  380. enum afs_flock_mode mode = AFS_FS_S(inode->i_sb)->flock_mode;
  381. afs_lock_type_t type;
  382. struct key *key = afs_file_key(file);
  383. bool partial, no_server_lock = false;
  384. int ret;
  385. if (mode == afs_flock_mode_unset)
  386. mode = afs_flock_mode_openafs;
  387. _enter("{%llx:%llu},%llu-%llu,%u,%u",
  388. vnode->fid.vid, vnode->fid.vnode,
  389. fl->fl_start, fl->fl_end, fl->c.flc_type, mode);
  390. fl->fl_ops = &afs_lock_ops;
  391. INIT_LIST_HEAD(&fl->fl_u.afs.link);
  392. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  393. partial = (fl->fl_start != 0 || fl->fl_end != OFFSET_MAX);
  394. type = lock_is_read(fl) ? AFS_LOCK_READ : AFS_LOCK_WRITE;
  395. if (mode == afs_flock_mode_write && partial)
  396. type = AFS_LOCK_WRITE;
  397. ret = afs_do_setlk_check(vnode, key, mode, type);
  398. if (ret < 0)
  399. return ret;
  400. trace_afs_flock_op(vnode, fl, afs_flock_op_set_lock);
  401. /* AFS3 protocol only supports full-file locks and doesn't provide any
  402. * method of upgrade/downgrade, so we need to emulate for partial-file
  403. * locks.
  404. *
  405. * The OpenAFS client only gets a server lock for a full-file lock and
  406. * keeps partial-file locks local. Allow this behaviour to be emulated
  407. * (as the default).
  408. */
  409. if (mode == afs_flock_mode_local ||
  410. (partial && mode == afs_flock_mode_openafs)) {
  411. no_server_lock = true;
  412. goto skip_server_lock;
  413. }
  414. spin_lock(&vnode->lock);
  415. list_add_tail(&fl->fl_u.afs.link, &vnode->pending_locks);
  416. ret = -ENOENT;
  417. if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
  418. goto error_unlock;
  419. /* If we've already got a lock on the server then try to move to having
  420. * the VFS grant the requested lock. Note that this means that other
  421. * clients may get starved out.
  422. */
  423. _debug("try %u", vnode->lock_state);
  424. if (vnode->lock_state == AFS_VNODE_LOCK_GRANTED) {
  425. if (type == AFS_LOCK_READ) {
  426. _debug("instant readlock");
  427. list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
  428. fl->fl_u.afs.state = AFS_LOCK_GRANTED;
  429. goto vnode_is_locked_u;
  430. }
  431. if (vnode->lock_type == AFS_LOCK_WRITE) {
  432. _debug("instant writelock");
  433. list_move_tail(&fl->fl_u.afs.link, &vnode->granted_locks);
  434. fl->fl_u.afs.state = AFS_LOCK_GRANTED;
  435. goto vnode_is_locked_u;
  436. }
  437. }
  438. if (vnode->lock_state == AFS_VNODE_LOCK_NONE &&
  439. !(fl->c.flc_flags & FL_SLEEP)) {
  440. ret = -EAGAIN;
  441. if (type == AFS_LOCK_READ) {
  442. if (vnode->status.lock_count == -1)
  443. goto lock_is_contended; /* Write locked */
  444. } else {
  445. if (vnode->status.lock_count != 0)
  446. goto lock_is_contended; /* Locked */
  447. }
  448. }
  449. if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
  450. goto need_to_wait;
  451. try_to_lock:
  452. /* We don't have a lock on this vnode and we aren't currently waiting
  453. * for one either, so ask the server for a lock.
  454. *
  455. * Note that we need to be careful if we get interrupted by a signal
  456. * after dispatching the request as we may still get the lock, even
  457. * though we don't wait for the reply (it's not too bad a problem - the
  458. * lock will expire in 5 mins anyway).
  459. */
  460. trace_afs_flock_ev(vnode, fl, afs_flock_try_to_lock, 0);
  461. vnode->lock_key = key_get(key);
  462. vnode->lock_type = type;
  463. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  464. spin_unlock(&vnode->lock);
  465. ret = afs_set_lock(vnode, key, type); /* RPC */
  466. spin_lock(&vnode->lock);
  467. switch (ret) {
  468. case -EKEYREJECTED:
  469. case -EKEYEXPIRED:
  470. case -EKEYREVOKED:
  471. case -EPERM:
  472. case -EACCES:
  473. fl->fl_u.afs.state = ret;
  474. trace_afs_flock_ev(vnode, fl, afs_flock_fail_perm, ret);
  475. list_del_init(&fl->fl_u.afs.link);
  476. afs_next_locker(vnode, ret);
  477. goto error_unlock;
  478. case -ENOENT:
  479. fl->fl_u.afs.state = ret;
  480. trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret);
  481. list_del_init(&fl->fl_u.afs.link);
  482. afs_kill_lockers_enoent(vnode);
  483. goto error_unlock;
  484. default:
  485. fl->fl_u.afs.state = ret;
  486. trace_afs_flock_ev(vnode, fl, afs_flock_fail_other, ret);
  487. list_del_init(&fl->fl_u.afs.link);
  488. afs_next_locker(vnode, 0);
  489. goto error_unlock;
  490. case -EWOULDBLOCK:
  491. /* The server doesn't have a lock-waiting queue, so the client
  492. * will have to retry. The server will break the outstanding
  493. * callbacks on a file when a lock is released.
  494. */
  495. ASSERT(list_empty(&vnode->granted_locks));
  496. ASSERTCMP(vnode->pending_locks.next, ==, &fl->fl_u.afs.link);
  497. goto lock_is_contended;
  498. case 0:
  499. afs_set_lock_state(vnode, AFS_VNODE_LOCK_GRANTED);
  500. trace_afs_flock_ev(vnode, fl, afs_flock_acquired, type);
  501. afs_grant_locks(vnode);
  502. goto vnode_is_locked_u;
  503. }
  504. vnode_is_locked_u:
  505. spin_unlock(&vnode->lock);
  506. vnode_is_locked:
  507. /* the lock has been granted by the server... */
  508. ASSERTCMP(fl->fl_u.afs.state, ==, AFS_LOCK_GRANTED);
  509. skip_server_lock:
  510. /* ... but the VFS still needs to distribute access on this client. */
  511. trace_afs_flock_ev(vnode, fl, afs_flock_vfs_locking, 0);
  512. ret = locks_lock_file_wait(file, fl);
  513. trace_afs_flock_ev(vnode, fl, afs_flock_vfs_lock, ret);
  514. if (ret < 0)
  515. goto vfs_rejected_lock;
  516. /* Again, make sure we've got a callback on this file and, again, make
  517. * sure that our view of the data version is up to date (we ignore
  518. * errors incurred here and deal with the consequences elsewhere).
  519. */
  520. afs_validate(vnode, key);
  521. _leave(" = 0");
  522. return 0;
  523. lock_is_contended:
  524. if (!(fl->c.flc_flags & FL_SLEEP)) {
  525. list_del_init(&fl->fl_u.afs.link);
  526. afs_next_locker(vnode, 0);
  527. ret = -EAGAIN;
  528. goto error_unlock;
  529. }
  530. afs_set_lock_state(vnode, AFS_VNODE_LOCK_WAITING_FOR_CB);
  531. trace_afs_flock_ev(vnode, fl, afs_flock_would_block, ret);
  532. queue_delayed_work(afs_lock_manager, &vnode->lock_work, HZ * 5);
  533. need_to_wait:
  534. /* We're going to have to wait. Either this client doesn't have a lock
  535. * on the server yet and we need to wait for a callback to occur, or
  536. * the client does have a lock on the server, but it's shared and we
  537. * need an exclusive lock.
  538. */
  539. spin_unlock(&vnode->lock);
  540. trace_afs_flock_ev(vnode, fl, afs_flock_waiting, 0);
  541. ret = wait_event_interruptible(fl->c.flc_wait,
  542. fl->fl_u.afs.state != AFS_LOCK_PENDING);
  543. trace_afs_flock_ev(vnode, fl, afs_flock_waited, ret);
  544. if (fl->fl_u.afs.state >= 0 && fl->fl_u.afs.state != AFS_LOCK_GRANTED) {
  545. spin_lock(&vnode->lock);
  546. switch (fl->fl_u.afs.state) {
  547. case AFS_LOCK_YOUR_TRY:
  548. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  549. goto try_to_lock;
  550. case AFS_LOCK_PENDING:
  551. if (ret > 0) {
  552. /* We need to retry the lock. We may not be
  553. * notified by the server if it just expired
  554. * rather than being released.
  555. */
  556. ASSERTCMP(vnode->lock_state, ==, AFS_VNODE_LOCK_WAITING_FOR_CB);
  557. afs_set_lock_state(vnode, AFS_VNODE_LOCK_SETTING);
  558. fl->fl_u.afs.state = AFS_LOCK_PENDING;
  559. goto try_to_lock;
  560. }
  561. goto error_unlock;
  562. case AFS_LOCK_GRANTED:
  563. default:
  564. break;
  565. }
  566. spin_unlock(&vnode->lock);
  567. }
  568. if (fl->fl_u.afs.state == AFS_LOCK_GRANTED)
  569. goto vnode_is_locked;
  570. ret = fl->fl_u.afs.state;
  571. goto error;
  572. vfs_rejected_lock:
  573. /* The VFS rejected the lock we just obtained, so we have to discard
  574. * what we just got. We defer this to the lock manager work item to
  575. * deal with.
  576. */
  577. _debug("vfs refused %d", ret);
  578. if (no_server_lock)
  579. goto error;
  580. spin_lock(&vnode->lock);
  581. list_del_init(&fl->fl_u.afs.link);
  582. afs_defer_unlock(vnode);
  583. error_unlock:
  584. spin_unlock(&vnode->lock);
  585. error:
  586. _leave(" = %d", ret);
  587. return ret;
  588. }
  589. /*
  590. * unlock on a file on the server
  591. */
  592. static int afs_do_unlk(struct file *file, struct file_lock *fl)
  593. {
  594. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  595. int ret;
  596. _enter("{%llx:%llu},%u", vnode->fid.vid, vnode->fid.vnode,
  597. fl->c.flc_type);
  598. trace_afs_flock_op(vnode, fl, afs_flock_op_unlock);
  599. /* Flush all pending writes before doing anything with locks. */
  600. vfs_fsync(file, 0);
  601. ret = locks_lock_file_wait(file, fl);
  602. _leave(" = %d [%u]", ret, vnode->lock_state);
  603. return ret;
  604. }
  605. /*
  606. * return information about a lock we currently hold, if indeed we hold one
  607. */
  608. static int afs_do_getlk(struct file *file, struct file_lock *fl)
  609. {
  610. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  611. struct key *key = afs_file_key(file);
  612. int ret, lock_count;
  613. _enter("");
  614. if (vnode->lock_state == AFS_VNODE_LOCK_DELETED)
  615. return -ENOENT;
  616. fl->c.flc_type = F_UNLCK;
  617. /* check local lock records first */
  618. posix_test_lock(file, fl);
  619. if (lock_is_unlock(fl)) {
  620. /* no local locks; consult the server */
  621. ret = afs_fetch_status(vnode, key, false, NULL);
  622. if (ret < 0)
  623. goto error;
  624. lock_count = READ_ONCE(vnode->status.lock_count);
  625. if (lock_count != 0) {
  626. if (lock_count > 0)
  627. fl->c.flc_type = F_RDLCK;
  628. else
  629. fl->c.flc_type = F_WRLCK;
  630. fl->fl_start = 0;
  631. fl->fl_end = OFFSET_MAX;
  632. fl->c.flc_pid = 0;
  633. }
  634. }
  635. ret = 0;
  636. error:
  637. _leave(" = %d [%hd]", ret, fl->c.flc_type);
  638. return ret;
  639. }
  640. /*
  641. * manage POSIX locks on a file
  642. */
  643. int afs_lock(struct file *file, int cmd, struct file_lock *fl)
  644. {
  645. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  646. enum afs_flock_operation op;
  647. int ret;
  648. _enter("{%llx:%llu},%d,{t=%x,fl=%x,r=%Ld:%Ld}",
  649. vnode->fid.vid, vnode->fid.vnode, cmd,
  650. fl->c.flc_type, fl->c.flc_flags,
  651. (long long) fl->fl_start, (long long) fl->fl_end);
  652. if (IS_GETLK(cmd))
  653. return afs_do_getlk(file, fl);
  654. fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
  655. trace_afs_flock_op(vnode, fl, afs_flock_op_lock);
  656. if (lock_is_unlock(fl))
  657. ret = afs_do_unlk(file, fl);
  658. else
  659. ret = afs_do_setlk(file, fl);
  660. switch (ret) {
  661. case 0: op = afs_flock_op_return_ok; break;
  662. case -EAGAIN: op = afs_flock_op_return_eagain; break;
  663. case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
  664. default: op = afs_flock_op_return_error; break;
  665. }
  666. trace_afs_flock_op(vnode, fl, op);
  667. return ret;
  668. }
  669. /*
  670. * manage FLOCK locks on a file
  671. */
  672. int afs_flock(struct file *file, int cmd, struct file_lock *fl)
  673. {
  674. struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
  675. enum afs_flock_operation op;
  676. int ret;
  677. _enter("{%llx:%llu},%d,{t=%x,fl=%x}",
  678. vnode->fid.vid, vnode->fid.vnode, cmd,
  679. fl->c.flc_type, fl->c.flc_flags);
  680. /*
  681. * No BSD flocks over NFS allowed.
  682. * Note: we could try to fake a POSIX lock request here by
  683. * using ((u32) filp | 0x80000000) or some such as the pid.
  684. * Not sure whether that would be unique, though, or whether
  685. * that would break in other places.
  686. */
  687. if (!(fl->c.flc_flags & FL_FLOCK))
  688. return -ENOLCK;
  689. fl->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
  690. trace_afs_flock_op(vnode, fl, afs_flock_op_flock);
  691. /* we're simulating flock() locks using posix locks on the server */
  692. if (lock_is_unlock(fl))
  693. ret = afs_do_unlk(file, fl);
  694. else
  695. ret = afs_do_setlk(file, fl);
  696. switch (ret) {
  697. case 0: op = afs_flock_op_return_ok; break;
  698. case -EAGAIN: op = afs_flock_op_return_eagain; break;
  699. case -EDEADLK: op = afs_flock_op_return_edeadlk; break;
  700. default: op = afs_flock_op_return_error; break;
  701. }
  702. trace_afs_flock_op(vnode, fl, op);
  703. return ret;
  704. }
  705. /*
  706. * the POSIX lock management core VFS code copies the lock record and adds the
  707. * copy into its own list, so we need to add that copy to the vnode's lock
  708. * queue in the same place as the original (which will be deleted shortly
  709. * after)
  710. */
  711. static void afs_fl_copy_lock(struct file_lock *new, struct file_lock *fl)
  712. {
  713. struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->c.flc_file));
  714. _enter("");
  715. new->fl_u.afs.debug_id = atomic_inc_return(&afs_file_lock_debug_id);
  716. spin_lock(&vnode->lock);
  717. trace_afs_flock_op(vnode, new, afs_flock_op_copy_lock);
  718. list_add(&new->fl_u.afs.link, &fl->fl_u.afs.link);
  719. spin_unlock(&vnode->lock);
  720. }
  721. /*
  722. * need to remove this lock from the vnode queue when it's removed from the
  723. * VFS's list
  724. */
  725. static void afs_fl_release_private(struct file_lock *fl)
  726. {
  727. struct afs_vnode *vnode = AFS_FS_I(file_inode(fl->c.flc_file));
  728. _enter("");
  729. spin_lock(&vnode->lock);
  730. trace_afs_flock_op(vnode, fl, afs_flock_op_release_lock);
  731. list_del_init(&fl->fl_u.afs.link);
  732. if (list_empty(&vnode->granted_locks))
  733. afs_defer_unlock(vnode);
  734. _debug("state %u for %p", vnode->lock_state, vnode);
  735. spin_unlock(&vnode->lock);
  736. }