locks.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. intr_req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETFILELOCK,
  109. USE_AUTH_MDS);
  110. if (IS_ERR(intr_req))
  111. return PTR_ERR(intr_req);
  112. intr_req->r_inode = inode;
  113. ihold(inode);
  114. intr_req->r_num_caps = 1;
  115. intr_req->r_args.filelock_change = req->r_args.filelock_change;
  116. intr_req->r_args.filelock_change.rule = lock_type;
  117. intr_req->r_args.filelock_change.type = CEPH_LOCK_UNLOCK;
  118. err = ceph_mdsc_do_request(mdsc, inode, intr_req);
  119. ceph_mdsc_put_request(intr_req);
  120. if (err && err != -ERESTARTSYS)
  121. return err;
  122. wait_for_completion(&req->r_completion);
  123. return 0;
  124. }
  125. /**
  126. * Attempt to set an fcntl lock.
  127. * For now, this just goes away to the server. Later it may be more awesome.
  128. */
  129. int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
  130. {
  131. u8 lock_cmd;
  132. int err;
  133. u8 wait = 0;
  134. u16 op = CEPH_MDS_OP_SETFILELOCK;
  135. if (!(fl->fl_flags & FL_POSIX))
  136. return -ENOLCK;
  137. /* No mandatory locks */
  138. if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
  139. return -ENOLCK;
  140. dout("ceph_lock, fl_owner: %p", fl->fl_owner);
  141. /* set wait bit as appropriate, then make command as Ceph expects it*/
  142. if (IS_GETLK(cmd))
  143. op = CEPH_MDS_OP_GETFILELOCK;
  144. else if (IS_SETLKW(cmd))
  145. wait = 1;
  146. if (F_RDLCK == fl->fl_type)
  147. lock_cmd = CEPH_LOCK_SHARED;
  148. else if (F_WRLCK == fl->fl_type)
  149. lock_cmd = CEPH_LOCK_EXCL;
  150. else
  151. lock_cmd = CEPH_LOCK_UNLOCK;
  152. err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl);
  153. if (!err) {
  154. if (op != CEPH_MDS_OP_GETFILELOCK) {
  155. dout("mds locked, locking locally");
  156. err = posix_lock_file(file, fl, NULL);
  157. if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
  158. /* undo! This should only happen if
  159. * the kernel detects local
  160. * deadlock. */
  161. ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  162. CEPH_LOCK_UNLOCK, 0, fl);
  163. dout("got %d on posix_lock_file, undid lock",
  164. err);
  165. }
  166. }
  167. }
  168. return err;
  169. }
  170. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  171. {
  172. u8 lock_cmd;
  173. int err;
  174. u8 wait = 0;
  175. if (!(fl->fl_flags & FL_FLOCK))
  176. return -ENOLCK;
  177. /* No mandatory locks */
  178. if (__mandatory_lock(file->f_mapping->host) && fl->fl_type != F_UNLCK)
  179. return -ENOLCK;
  180. dout("ceph_flock, fl_file: %p", fl->fl_file);
  181. if (IS_SETLKW(cmd))
  182. wait = 1;
  183. if (F_RDLCK == fl->fl_type)
  184. lock_cmd = CEPH_LOCK_SHARED;
  185. else if (F_WRLCK == fl->fl_type)
  186. lock_cmd = CEPH_LOCK_EXCL;
  187. else
  188. lock_cmd = CEPH_LOCK_UNLOCK;
  189. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  190. file, lock_cmd, wait, fl);
  191. if (!err) {
  192. err = locks_lock_file_wait(file, fl);
  193. if (err) {
  194. ceph_lock_message(CEPH_LOCK_FLOCK,
  195. CEPH_MDS_OP_SETFILELOCK,
  196. file, CEPH_LOCK_UNLOCK, 0, fl);
  197. dout("got %d on locks_lock_file_wait, undid lock", err);
  198. }
  199. }
  200. return err;
  201. }
  202. /*
  203. * Fills in the passed counter variables, so you can prepare pagelist metadata
  204. * before calling ceph_encode_locks.
  205. */
  206. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  207. {
  208. struct file_lock *lock;
  209. struct file_lock_context *ctx;
  210. *fcntl_count = 0;
  211. *flock_count = 0;
  212. ctx = inode->i_flctx;
  213. if (ctx) {
  214. spin_lock(&ctx->flc_lock);
  215. list_for_each_entry(lock, &ctx->flc_posix, fl_list)
  216. ++(*fcntl_count);
  217. list_for_each_entry(lock, &ctx->flc_flock, fl_list)
  218. ++(*flock_count);
  219. spin_unlock(&ctx->flc_lock);
  220. }
  221. dout("counted %d flock locks and %d fcntl locks",
  222. *flock_count, *fcntl_count);
  223. }
  224. /**
  225. * Encode the flock and fcntl locks for the given inode into the ceph_filelock
  226. * array. Must be called with inode->i_lock already held.
  227. * If we encounter more of a specific lock type than expected, return -ENOSPC.
  228. */
  229. int ceph_encode_locks_to_buffer(struct inode *inode,
  230. struct ceph_filelock *flocks,
  231. int num_fcntl_locks, int num_flock_locks)
  232. {
  233. struct file_lock *lock;
  234. struct file_lock_context *ctx = inode->i_flctx;
  235. int err = 0;
  236. int seen_fcntl = 0;
  237. int seen_flock = 0;
  238. int l = 0;
  239. dout("encoding %d flock and %d fcntl locks", num_flock_locks,
  240. num_fcntl_locks);
  241. if (!ctx)
  242. return 0;
  243. spin_lock(&ctx->flc_lock);
  244. list_for_each_entry(lock, &ctx->flc_posix, fl_list) {
  245. ++seen_fcntl;
  246. if (seen_fcntl > num_fcntl_locks) {
  247. err = -ENOSPC;
  248. goto fail;
  249. }
  250. err = lock_to_ceph_filelock(lock, &flocks[l]);
  251. if (err)
  252. goto fail;
  253. ++l;
  254. }
  255. list_for_each_entry(lock, &ctx->flc_flock, fl_list) {
  256. ++seen_flock;
  257. if (seen_flock > num_flock_locks) {
  258. err = -ENOSPC;
  259. goto fail;
  260. }
  261. err = lock_to_ceph_filelock(lock, &flocks[l]);
  262. if (err)
  263. goto fail;
  264. ++l;
  265. }
  266. fail:
  267. spin_unlock(&ctx->flc_lock);
  268. return err;
  269. }
  270. /**
  271. * Copy the encoded flock and fcntl locks into the pagelist.
  272. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  273. * sequential flock locks.
  274. * Returns zero on success.
  275. */
  276. int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  277. struct ceph_pagelist *pagelist,
  278. int num_fcntl_locks, int num_flock_locks)
  279. {
  280. int err = 0;
  281. __le32 nlocks;
  282. nlocks = cpu_to_le32(num_fcntl_locks);
  283. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  284. if (err)
  285. goto out_fail;
  286. err = ceph_pagelist_append(pagelist, flocks,
  287. num_fcntl_locks * sizeof(*flocks));
  288. if (err)
  289. goto out_fail;
  290. nlocks = cpu_to_le32(num_flock_locks);
  291. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  292. if (err)
  293. goto out_fail;
  294. err = ceph_pagelist_append(pagelist,
  295. &flocks[num_fcntl_locks],
  296. num_flock_locks * sizeof(*flocks));
  297. out_fail:
  298. return err;
  299. }
  300. /*
  301. * Given a pointer to a lock, convert it to a ceph filelock
  302. */
  303. int lock_to_ceph_filelock(struct file_lock *lock,
  304. struct ceph_filelock *cephlock)
  305. {
  306. int err = 0;
  307. cephlock->start = cpu_to_le64(lock->fl_start);
  308. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  309. cephlock->client = cpu_to_le64(0);
  310. cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
  311. cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
  312. switch (lock->fl_type) {
  313. case F_RDLCK:
  314. cephlock->type = CEPH_LOCK_SHARED;
  315. break;
  316. case F_WRLCK:
  317. cephlock->type = CEPH_LOCK_EXCL;
  318. break;
  319. case F_UNLCK:
  320. cephlock->type = CEPH_LOCK_UNLOCK;
  321. break;
  322. default:
  323. dout("Have unknown lock type %d", lock->fl_type);
  324. err = -EINVAL;
  325. }
  326. return err;
  327. }