locks.c 10 KB

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