locks.c 13 KB

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