locks.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ceph/ceph_debug.h>
  3. #include <linux/file.h>
  4. #include <linux/namei.h>
  5. #include <linux/random.h>
  6. #include "super.h"
  7. #include "mds_client.h"
  8. #include <linux/ceph/pagelist.h>
  9. static u64 lock_secret;
  10. static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
  11. struct ceph_mds_request *req);
  12. static inline u64 secure_addr(void *addr)
  13. {
  14. u64 v = lock_secret ^ (u64)(unsigned long)addr;
  15. /*
  16. * Set the most significant bit, so that MDS knows the 'owner'
  17. * is sufficient to identify the owner of lock. (old code uses
  18. * both 'owner' and 'pid')
  19. */
  20. v |= (1ULL << 63);
  21. return v;
  22. }
  23. void __init ceph_flock_init(void)
  24. {
  25. get_random_bytes(&lock_secret, sizeof(lock_secret));
  26. }
  27. static void ceph_fl_copy_lock(struct file_lock *dst, struct file_lock *src)
  28. {
  29. struct inode *inode = file_inode(dst->fl_file);
  30. atomic_inc(&ceph_inode(inode)->i_filelock_ref);
  31. }
  32. static void ceph_fl_release_lock(struct file_lock *fl)
  33. {
  34. struct inode *inode = file_inode(fl->fl_file);
  35. struct ceph_inode_info *ci = ceph_inode(inode);
  36. if (atomic_dec_and_test(&ci->i_filelock_ref)) {
  37. /* clear error when all locks are released */
  38. spin_lock(&ci->i_ceph_lock);
  39. ci->i_ceph_flags &= ~CEPH_I_ERROR_FILELOCK;
  40. spin_unlock(&ci->i_ceph_lock);
  41. }
  42. }
  43. static const struct file_lock_operations ceph_fl_lock_ops = {
  44. .fl_copy_lock = ceph_fl_copy_lock,
  45. .fl_release_private = ceph_fl_release_lock,
  46. };
  47. /*
  48. * Implement fcntl and flock locking functions.
  49. */
  50. static int ceph_lock_message(u8 lock_type, u16 operation, struct inode *inode,
  51. int cmd, u8 wait, struct file_lock *fl)
  52. {
  53. struct ceph_mds_client *mdsc = ceph_sb_to_mdsc(inode->i_sb);
  54. struct ceph_mds_request *req;
  55. int err;
  56. u64 length = 0;
  57. u64 owner;
  58. if (operation == CEPH_MDS_OP_SETFILELOCK) {
  59. /*
  60. * increasing i_filelock_ref closes race window between
  61. * handling request reply and adding file_lock struct to
  62. * inode. Otherwise, auth caps may get trimmed in the
  63. * window. Caller function will decrease the counter.
  64. */
  65. fl->fl_ops = &ceph_fl_lock_ops;
  66. fl->fl_ops->fl_copy_lock(fl, NULL);
  67. }
  68. if (operation != CEPH_MDS_OP_SETFILELOCK || cmd == CEPH_LOCK_UNLOCK)
  69. wait = 0;
  70. req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
  71. if (IS_ERR(req))
  72. return PTR_ERR(req);
  73. req->r_inode = inode;
  74. ihold(inode);
  75. req->r_num_caps = 1;
  76. /* mds requires start and length rather than start and end */
  77. if (LLONG_MAX == fl->fl_end)
  78. length = 0;
  79. else
  80. length = fl->fl_end - fl->fl_start + 1;
  81. owner = secure_addr(fl->fl_owner);
  82. dout("ceph_lock_message: rule: %d, op: %d, owner: %llx, pid: %llu, "
  83. "start: %llu, length: %llu, wait: %d, type: %d\n", (int)lock_type,
  84. (int)operation, owner, (u64)fl->fl_pid, fl->fl_start, length,
  85. wait, fl->fl_type);
  86. req->r_args.filelock_change.rule = lock_type;
  87. req->r_args.filelock_change.type = cmd;
  88. req->r_args.filelock_change.owner = cpu_to_le64(owner);
  89. req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid);
  90. req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start);
  91. req->r_args.filelock_change.length = cpu_to_le64(length);
  92. req->r_args.filelock_change.wait = wait;
  93. err = ceph_mdsc_submit_request(mdsc, inode, req);
  94. if (!err)
  95. err = ceph_mdsc_wait_request(mdsc, req, wait ?
  96. ceph_lock_wait_for_completion : NULL);
  97. if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
  98. fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
  99. if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
  100. fl->fl_type = F_RDLCK;
  101. else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
  102. fl->fl_type = F_WRLCK;
  103. else
  104. fl->fl_type = F_UNLCK;
  105. fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
  106. length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
  107. le64_to_cpu(req->r_reply_info.filelock_reply->length);
  108. if (length >= 1)
  109. fl->fl_end = length -1;
  110. else
  111. fl->fl_end = 0;
  112. }
  113. ceph_mdsc_put_request(req);
  114. dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
  115. "length: %llu, wait: %d, type: %d, err code %d\n", (int)lock_type,
  116. (int)operation, (u64)fl->fl_pid, fl->fl_start,
  117. length, wait, fl->fl_type, err);
  118. return err;
  119. }
  120. static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
  121. struct ceph_mds_request *req)
  122. {
  123. struct ceph_mds_request *intr_req;
  124. struct inode *inode = req->r_inode;
  125. int err, lock_type;
  126. BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
  127. if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
  128. lock_type = CEPH_LOCK_FCNTL_INTR;
  129. else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
  130. lock_type = CEPH_LOCK_FLOCK_INTR;
  131. else
  132. BUG_ON(1);
  133. BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
  134. err = wait_for_completion_interruptible(&req->r_completion);
  135. if (!err)
  136. return 0;
  137. dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
  138. req->r_tid);
  139. mutex_lock(&mdsc->mutex);
  140. if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  141. err = 0;
  142. } else {
  143. /*
  144. * ensure we aren't running concurrently with
  145. * ceph_fill_trace or ceph_readdir_prepopulate, which
  146. * rely on locks (dir mutex) held by our caller.
  147. */
  148. mutex_lock(&req->r_fill_mutex);
  149. req->r_err = err;
  150. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  151. mutex_unlock(&req->r_fill_mutex);
  152. if (!req->r_session) {
  153. // haven't sent the request
  154. err = 0;
  155. }
  156. }
  157. mutex_unlock(&mdsc->mutex);
  158. if (!err)
  159. return 0;
  160. intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
  161. USE_AUTH_MDS);
  162. if (IS_ERR(intr_req))
  163. return PTR_ERR(intr_req);
  164. intr_req->r_inode = inode;
  165. ihold(inode);
  166. intr_req->r_num_caps = 1;
  167. intr_req->r_args.filelock_change = req->r_args.filelock_change;
  168. intr_req->r_args.filelock_change.rule = lock_type;
  169. intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
  170. err = ceph_mdsc_do_request(mdsc, inode, intr_req);
  171. ceph_mdsc_put_request(intr_req);
  172. if (err && err != -ERESTARTSYS)
  173. return err;
  174. wait_for_completion_killable(&req->r_safe_completion);
  175. return 0;
  176. }
  177. static int try_unlock_file(struct file *file, struct file_lock *fl)
  178. {
  179. int err;
  180. unsigned int orig_flags = fl->fl_flags;
  181. fl->fl_flags |= FL_EXISTS;
  182. err = locks_lock_file_wait(file, fl);
  183. fl->fl_flags = orig_flags;
  184. if (err == -ENOENT) {
  185. if (!(orig_flags & FL_EXISTS))
  186. err = 0;
  187. return err;
  188. }
  189. return 1;
  190. }
  191. /*
  192. * Attempt to set an fcntl lock.
  193. * For now, this just goes away to the server. Later it may be more awesome.
  194. */
  195. int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
  196. {
  197. struct inode *inode = file_inode(file);
  198. struct ceph_inode_info *ci = ceph_inode(inode);
  199. int err = 0;
  200. u16 op = CEPH_MDS_OP_SETFILELOCK;
  201. u8 wait = 0;
  202. u8 lock_cmd;
  203. if (!(fl->fl_flags & FL_POSIX))
  204. return -ENOLCK;
  205. if (ceph_inode_is_shutdown(inode))
  206. return -ESTALE;
  207. dout("ceph_lock, fl_owner: %p\n", fl->fl_owner);
  208. /* set wait bit as appropriate, then make command as Ceph expects it*/
  209. if (IS_GETLK(cmd))
  210. op = CEPH_MDS_OP_GETFILELOCK;
  211. else if (IS_SETLKW(cmd))
  212. wait = 1;
  213. spin_lock(&ci->i_ceph_lock);
  214. if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
  215. err = -EIO;
  216. }
  217. spin_unlock(&ci->i_ceph_lock);
  218. if (err < 0) {
  219. if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type)
  220. posix_lock_file(file, fl, NULL);
  221. return err;
  222. }
  223. if (F_RDLCK == fl->fl_type)
  224. lock_cmd = CEPH_LOCK_SHARED;
  225. else if (F_WRLCK == fl->fl_type)
  226. lock_cmd = CEPH_LOCK_EXCL;
  227. else
  228. lock_cmd = CEPH_LOCK_UNLOCK;
  229. if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type) {
  230. err = try_unlock_file(file, fl);
  231. if (err <= 0)
  232. return err;
  233. }
  234. err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
  235. if (!err) {
  236. if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK != fl->fl_type) {
  237. dout("mds locked, locking locally\n");
  238. err = posix_lock_file(file, fl, NULL);
  239. if (err) {
  240. /* undo! This should only happen if
  241. * the kernel detects local
  242. * deadlock. */
  243. ceph_lock_message(CEPH_LOCK_FCNTL, op, inode,
  244. CEPH_LOCK_UNLOCK, 0, fl);
  245. dout("got %d on posix_lock_file, undid lock\n",
  246. err);
  247. }
  248. }
  249. }
  250. return err;
  251. }
  252. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  253. {
  254. struct inode *inode = file_inode(file);
  255. struct ceph_inode_info *ci = ceph_inode(inode);
  256. int err = 0;
  257. u8 wait = 0;
  258. u8 lock_cmd;
  259. if (!(fl->fl_flags & FL_FLOCK))
  260. return -ENOLCK;
  261. if (ceph_inode_is_shutdown(inode))
  262. return -ESTALE;
  263. dout("ceph_flock, fl_file: %p\n", fl->fl_file);
  264. spin_lock(&ci->i_ceph_lock);
  265. if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
  266. err = -EIO;
  267. }
  268. spin_unlock(&ci->i_ceph_lock);
  269. if (err < 0) {
  270. if (F_UNLCK == fl->fl_type)
  271. locks_lock_file_wait(file, fl);
  272. return err;
  273. }
  274. if (IS_SETLKW(cmd))
  275. wait = 1;
  276. if (F_RDLCK == fl->fl_type)
  277. lock_cmd = CEPH_LOCK_SHARED;
  278. else if (F_WRLCK == fl->fl_type)
  279. lock_cmd = CEPH_LOCK_EXCL;
  280. else
  281. lock_cmd = CEPH_LOCK_UNLOCK;
  282. if (F_UNLCK == fl->fl_type) {
  283. err = try_unlock_file(file, fl);
  284. if (err <= 0)
  285. return err;
  286. }
  287. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  288. inode, lock_cmd, wait, fl);
  289. if (!err && F_UNLCK != fl->fl_type) {
  290. err = locks_lock_file_wait(file, fl);
  291. if (err) {
  292. ceph_lock_message(CEPH_LOCK_FLOCK,
  293. CEPH_MDS_OP_SETFILELOCK,
  294. inode, CEPH_LOCK_UNLOCK, 0, fl);
  295. dout("got %d on locks_lock_file_wait, undid lock\n", err);
  296. }
  297. }
  298. return err;
  299. }
  300. /*
  301. * Fills in the passed counter variables, so you can prepare pagelist metadata
  302. * before calling ceph_encode_locks.
  303. */
  304. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  305. {
  306. struct file_lock *lock;
  307. struct file_lock_context *ctx;
  308. *fcntl_count = 0;
  309. *flock_count = 0;
  310. ctx = inode->i_flctx;
  311. if (ctx) {
  312. spin_lock(&ctx->flc_lock);
  313. list_for_each_entry(lock, &ctx->flc_posix, fl_list)
  314. ++(*fcntl_count);
  315. list_for_each_entry(lock, &ctx->flc_flock, fl_list)
  316. ++(*flock_count);
  317. spin_unlock(&ctx->flc_lock);
  318. }
  319. dout("counted %d flock locks and %d fcntl locks\n",
  320. *flock_count, *fcntl_count);
  321. }
  322. /*
  323. * Given a pointer to a lock, convert it to a ceph filelock
  324. */
  325. static int lock_to_ceph_filelock(struct file_lock *lock,
  326. struct ceph_filelock *cephlock)
  327. {
  328. int err = 0;
  329. cephlock->start = cpu_to_le64(lock->fl_start);
  330. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  331. cephlock->client = cpu_to_le64(0);
  332. cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
  333. cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
  334. switch (lock->fl_type) {
  335. case F_RDLCK:
  336. cephlock->type = CEPH_LOCK_SHARED;
  337. break;
  338. case F_WRLCK:
  339. cephlock->type = CEPH_LOCK_EXCL;
  340. break;
  341. case F_UNLCK:
  342. cephlock->type = CEPH_LOCK_UNLOCK;
  343. break;
  344. default:
  345. dout("Have unknown lock type %d\n", lock->fl_type);
  346. err = -EINVAL;
  347. }
  348. return err;
  349. }
  350. /*
  351. * Encode the flock and fcntl locks for the given inode into the ceph_filelock
  352. * array. Must be called with inode->i_lock already held.
  353. * If we encounter more of a specific lock type than expected, return -ENOSPC.
  354. */
  355. int ceph_encode_locks_to_buffer(struct inode *inode,
  356. struct ceph_filelock *flocks,
  357. int num_fcntl_locks, int num_flock_locks)
  358. {
  359. struct file_lock *lock;
  360. struct file_lock_context *ctx = inode->i_flctx;
  361. int err = 0;
  362. int seen_fcntl = 0;
  363. int seen_flock = 0;
  364. int l = 0;
  365. dout("encoding %d flock and %d fcntl locks\n", num_flock_locks,
  366. num_fcntl_locks);
  367. if (!ctx)
  368. return 0;
  369. spin_lock(&ctx->flc_lock);
  370. list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
  371. ++seen_fcntl;
  372. if (seen_fcntl > num_fcntl_locks) {
  373. err = -ENOSPC;
  374. goto fail;
  375. }
  376. err = lock_to_ceph_filelock(lock, &flocks[l]);
  377. if (err)
  378. goto fail;
  379. ++l;
  380. }
  381. list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
  382. ++seen_flock;
  383. if (seen_flock > num_flock_locks) {
  384. err = -ENOSPC;
  385. goto fail;
  386. }
  387. err = lock_to_ceph_filelock(lock, &flocks[l]);
  388. if (err)
  389. goto fail;
  390. ++l;
  391. }
  392. fail:
  393. spin_unlock(&ctx->flc_lock);
  394. return err;
  395. }
  396. /*
  397. * Copy the encoded flock and fcntl locks into the pagelist.
  398. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  399. * sequential flock locks.
  400. * Returns zero on success.
  401. */
  402. int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  403. struct ceph_pagelist *pagelist,
  404. int num_fcntl_locks, int num_flock_locks)
  405. {
  406. int err = 0;
  407. __le32 nlocks;
  408. nlocks = cpu_to_le32(num_fcntl_locks);
  409. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  410. if (err)
  411. goto out_fail;
  412. if (num_fcntl_locks > 0) {
  413. err = ceph_pagelist_append(pagelist, flocks,
  414. num_fcntl_locks * sizeof(*flocks));
  415. if (err)
  416. goto out_fail;
  417. }
  418. nlocks = cpu_to_le32(num_flock_locks);
  419. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  420. if (err)
  421. goto out_fail;
  422. if (num_flock_locks > 0) {
  423. err = ceph_pagelist_append(pagelist, &flocks[num_fcntl_locks],
  424. num_flock_locks * sizeof(*flocks));
  425. }
  426. out_fail:
  427. return err;
  428. }