flock.c 19 KB

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