locks.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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(src->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_client(inode->i_sb)->mdsc;
  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. atomic_inc(&ceph_inode(inode)->i_filelock_ref);
  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. if (wait)
  94. req->r_wait_for_completion = ceph_lock_wait_for_completion;
  95. err = ceph_mdsc_do_request(mdsc, inode, req);
  96. if (!err && operation == CEPH_MDS_OP_GETFILELOCK) {
  97. fl->fl_pid = -le64_to_cpu(req->r_reply_info.filelock_reply->pid);
  98. if (CEPH_LOCK_SHARED == req->r_reply_info.filelock_reply->type)
  99. fl->fl_type = F_RDLCK;
  100. else if (CEPH_LOCK_EXCL == req->r_reply_info.filelock_reply->type)
  101. fl->fl_type = F_WRLCK;
  102. else
  103. fl->fl_type = F_UNLCK;
  104. fl->fl_start = le64_to_cpu(req->r_reply_info.filelock_reply->start);
  105. length = le64_to_cpu(req->r_reply_info.filelock_reply->start) +
  106. le64_to_cpu(req->r_reply_info.filelock_reply->length);
  107. if (length >= 1)
  108. fl->fl_end = length -1;
  109. else
  110. fl->fl_end = 0;
  111. }
  112. ceph_mdsc_put_request(req);
  113. dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
  114. "length: %llu, wait: %d, type: %d, err code %d\n", (int)lock_type,
  115. (int)operation, (u64)fl->fl_pid, fl->fl_start,
  116. length, wait, fl->fl_type, err);
  117. return err;
  118. }
  119. static int ceph_lock_wait_for_completion(struct ceph_mds_client *mdsc,
  120. struct ceph_mds_request *req)
  121. {
  122. struct ceph_mds_request *intr_req;
  123. struct inode *inode = req->r_inode;
  124. int err, lock_type;
  125. BUG_ON(req->r_op != CEPH_MDS_OP_SETFILELOCK);
  126. if (req->r_args.filelock_change.rule == CEPH_LOCK_FCNTL)
  127. lock_type = CEPH_LOCK_FCNTL_INTR;
  128. else if (req->r_args.filelock_change.rule == CEPH_LOCK_FLOCK)
  129. lock_type = CEPH_LOCK_FLOCK_INTR;
  130. else
  131. BUG_ON(1);
  132. BUG_ON(req->r_args.filelock_change.type == CEPH_LOCK_UNLOCK);
  133. err = wait_for_completion_interruptible(&req->r_completion);
  134. if (!err)
  135. return 0;
  136. dout("ceph_lock_wait_for_completion: request %llu was interrupted\n",
  137. req->r_tid);
  138. mutex_lock(&mdsc->mutex);
  139. if (test_bit(CEPH_MDS_R_GOT_RESULT, &req->r_req_flags)) {
  140. err = 0;
  141. } else {
  142. /*
  143. * ensure we aren't running concurrently with
  144. * ceph_fill_trace or ceph_readdir_prepopulate, which
  145. * rely on locks (dir mutex) held by our caller.
  146. */
  147. mutex_lock(&req->r_fill_mutex);
  148. req->r_err = err;
  149. set_bit(CEPH_MDS_R_ABORTED, &req->r_req_flags);
  150. mutex_unlock(&req->r_fill_mutex);
  151. if (!req->r_session) {
  152. // haven't sent the request
  153. err = 0;
  154. }
  155. }
  156. mutex_unlock(&mdsc->mutex);
  157. if (!err)
  158. return 0;
  159. intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
  160. USE_AUTH_MDS);
  161. if (IS_ERR(intr_req))
  162. return PTR_ERR(intr_req);
  163. intr_req->r_inode = inode;
  164. ihold(inode);
  165. intr_req->r_num_caps = 1;
  166. intr_req->r_args.filelock_change = req->r_args.filelock_change;
  167. intr_req->r_args.filelock_change.rule = lock_type;
  168. intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
  169. err = ceph_mdsc_do_request(mdsc, inode, intr_req);
  170. ceph_mdsc_put_request(intr_req);
  171. if (err && err != -ERESTARTSYS)
  172. return err;
  173. wait_for_completion_killable(&req->r_safe_completion);
  174. return 0;
  175. }
  176. /**
  177. * Attempt to set an fcntl lock.
  178. * For now, this just goes away to the server. Later it may be more awesome.
  179. */
  180. int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
  181. {
  182. struct inode *inode = file_inode(file);
  183. struct ceph_inode_info *ci = ceph_inode(inode);
  184. int err = 0;
  185. u16 op = CEPH_MDS_OP_SETFILELOCK;
  186. u8 wait = 0;
  187. u8 lock_cmd;
  188. if (!(fl->fl_flags & FL_POSIX))
  189. return -ENOLCK;
  190. /* No mandatory locks */
  191. if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
  192. return -ENOLCK;
  193. dout("ceph_lock, fl_owner: %p\n", fl->fl_owner);
  194. /* set wait bit as appropriate, then make command as Ceph expects it*/
  195. if (IS_GETLK(cmd))
  196. op = CEPH_MDS_OP_GETFILELOCK;
  197. else if (IS_SETLKW(cmd))
  198. wait = 1;
  199. spin_lock(&ci->i_ceph_lock);
  200. if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
  201. err = -EIO;
  202. } else if (op == CEPH_MDS_OP_SETFILELOCK) {
  203. /*
  204. * increasing i_filelock_ref closes race window between
  205. * handling request reply and adding file_lock struct to
  206. * inode. Otherwise, i_auth_cap may get trimmed in the
  207. * window. Caller function will decrease the counter.
  208. */
  209. fl->fl_ops = &ceph_fl_lock_ops;
  210. atomic_inc(&ci->i_filelock_ref);
  211. }
  212. spin_unlock(&ci->i_ceph_lock);
  213. if (err < 0) {
  214. if (op == CEPH_MDS_OP_SETFILELOCK && F_UNLCK == fl->fl_type)
  215. posix_lock_file(file, fl, NULL);
  216. return err;
  217. }
  218. if (F_RDLCK == fl->fl_type)
  219. lock_cmd = CEPH_LOCK_SHARED;
  220. else if (F_WRLCK == fl->fl_type)
  221. lock_cmd = CEPH_LOCK_EXCL;
  222. else
  223. lock_cmd = CEPH_LOCK_UNLOCK;
  224. err = ceph_lock_message(CEPH_LOCK_FCNTL, op, inode, lock_cmd, wait, fl);
  225. if (!err) {
  226. if (op == CEPH_MDS_OP_SETFILELOCK) {
  227. dout("mds locked, locking locally\n");
  228. err = posix_lock_file(file, fl, NULL);
  229. if (err) {
  230. /* undo! This should only happen if
  231. * the kernel detects local
  232. * deadlock. */
  233. ceph_lock_message(CEPH_LOCK_FCNTL, op, inode,
  234. CEPH_LOCK_UNLOCK, 0, fl);
  235. dout("got %d on posix_lock_file, undid lock\n",
  236. err);
  237. }
  238. }
  239. }
  240. return err;
  241. }
  242. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  243. {
  244. struct inode *inode = file_inode(file);
  245. struct ceph_inode_info *ci = ceph_inode(inode);
  246. int err = 0;
  247. u8 wait = 0;
  248. u8 lock_cmd;
  249. if (!(fl->fl_flags & FL_FLOCK))
  250. return -ENOLCK;
  251. /* No mandatory locks */
  252. if (fl->fl_type & LOCK_MAND)
  253. return -EOPNOTSUPP;
  254. dout("ceph_flock, fl_file: %p\n", fl->fl_file);
  255. spin_lock(&ci->i_ceph_lock);
  256. if (ci->i_ceph_flags & CEPH_I_ERROR_FILELOCK) {
  257. err = -EIO;
  258. } else {
  259. /* see comment in ceph_lock */
  260. fl->fl_ops = &ceph_fl_lock_ops;
  261. atomic_inc(&ci->i_filelock_ref);
  262. }
  263. spin_unlock(&ci->i_ceph_lock);
  264. if (err < 0) {
  265. if (F_UNLCK == fl->fl_type)
  266. locks_lock_file_wait(file, fl);
  267. return err;
  268. }
  269. if (IS_SETLKW(cmd))
  270. wait = 1;
  271. if (F_RDLCK == fl->fl_type)
  272. lock_cmd = CEPH_LOCK_SHARED;
  273. else if (F_WRLCK == fl->fl_type)
  274. lock_cmd = CEPH_LOCK_EXCL;
  275. else
  276. lock_cmd = CEPH_LOCK_UNLOCK;
  277. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  278. inode, lock_cmd, wait, fl);
  279. if (!err) {
  280. err = locks_lock_file_wait(file, fl);
  281. if (err) {
  282. ceph_lock_message(CEPH_LOCK_FLOCK,
  283. CEPH_MDS_OP_SETFILELOCK,
  284. inode, CEPH_LOCK_UNLOCK, 0, fl);
  285. dout("got %d on locks_lock_file_wait, undid lock\n", err);
  286. }
  287. }
  288. return err;
  289. }
  290. /*
  291. * Fills in the passed counter variables, so you can prepare pagelist metadata
  292. * before calling ceph_encode_locks.
  293. */
  294. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  295. {
  296. struct file_lock *lock;
  297. struct file_lock_context *ctx;
  298. *fcntl_count = 0;
  299. *flock_count = 0;
  300. ctx = inode->i_flctx;
  301. if (ctx) {
  302. spin_lock(&ctx->flc_lock);
  303. list_for_each_entry(lock, &ctx->flc_posix, fl_list)
  304. ++(*fcntl_count);
  305. list_for_each_entry(lock, &ctx->flc_flock, fl_list)
  306. ++(*flock_count);
  307. spin_unlock(&ctx->flc_lock);
  308. }
  309. dout("counted %d flock locks and %d fcntl locks\n",
  310. *flock_count, *fcntl_count);
  311. }
  312. /*
  313. * Given a pointer to a lock, convert it to a ceph filelock
  314. */
  315. static int lock_to_ceph_filelock(struct file_lock *lock,
  316. struct ceph_filelock *cephlock)
  317. {
  318. int err = 0;
  319. cephlock->start = cpu_to_le64(lock->fl_start);
  320. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  321. cephlock->client = cpu_to_le64(0);
  322. cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
  323. cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
  324. switch (lock->fl_type) {
  325. case F_RDLCK:
  326. cephlock->type = CEPH_LOCK_SHARED;
  327. break;
  328. case F_WRLCK:
  329. cephlock->type = CEPH_LOCK_EXCL;
  330. break;
  331. case F_UNLCK:
  332. cephlock->type = CEPH_LOCK_UNLOCK;
  333. break;
  334. default:
  335. dout("Have unknown lock type %d\n", lock->fl_type);
  336. err = -EINVAL;
  337. }
  338. return err;
  339. }
  340. /**
  341. * Encode the flock and fcntl locks for the given inode into the ceph_filelock
  342. * array. Must be called with inode->i_lock already held.
  343. * If we encounter more of a specific lock type than expected, return -ENOSPC.
  344. */
  345. int ceph_encode_locks_to_buffer(struct inode *inode,
  346. struct ceph_filelock *flocks,
  347. int num_fcntl_locks, int num_flock_locks)
  348. {
  349. struct file_lock *lock;
  350. struct file_lock_context *ctx = inode->i_flctx;
  351. int err = 0;
  352. int seen_fcntl = 0;
  353. int seen_flock = 0;
  354. int l = 0;
  355. dout("encoding %d flock and %d fcntl locks\n", num_flock_locks,
  356. num_fcntl_locks);
  357. if (!ctx)
  358. return 0;
  359. spin_lock(&ctx->flc_lock);
  360. list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
  361. ++seen_fcntl;
  362. if (seen_fcntl > num_fcntl_locks) {
  363. err = -ENOSPC;
  364. goto fail;
  365. }
  366. err = lock_to_ceph_filelock(lock, &flocks[l]);
  367. if (err)
  368. goto fail;
  369. ++l;
  370. }
  371. list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
  372. ++seen_flock;
  373. if (seen_flock > num_flock_locks) {
  374. err = -ENOSPC;
  375. goto fail;
  376. }
  377. err = lock_to_ceph_filelock(lock, &flocks[l]);
  378. if (err)
  379. goto fail;
  380. ++l;
  381. }
  382. fail:
  383. spin_unlock(&ctx->flc_lock);
  384. return err;
  385. }
  386. /**
  387. * Copy the encoded flock and fcntl locks into the pagelist.
  388. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  389. * sequential flock locks.
  390. * Returns zero on success.
  391. */
  392. int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  393. struct ceph_pagelist *pagelist,
  394. int num_fcntl_locks, int num_flock_locks)
  395. {
  396. int err = 0;
  397. __le32 nlocks;
  398. nlocks = cpu_to_le32(num_fcntl_locks);
  399. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  400. if (err)
  401. goto out_fail;
  402. if (num_fcntl_locks > 0) {
  403. err = ceph_pagelist_append(pagelist, flocks,
  404. num_fcntl_locks * sizeof(*flocks));
  405. if (err)
  406. goto out_fail;
  407. }
  408. nlocks = cpu_to_le32(num_flock_locks);
  409. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  410. if (err)
  411. goto out_fail;
  412. if (num_flock_locks > 0) {
  413. err = ceph_pagelist_append(pagelist, &flocks[num_fcntl_locks],
  414. num_flock_locks * sizeof(*flocks));
  415. }
  416. out_fail:
  417. return err;
  418. }