locks.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 = flock_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 flock_lock_file_wait, undid lock", err);
  198. }
  199. }
  200. return err;
  201. }
  202. /**
  203. * Must be called with lock_flocks() already held. Fills in the passed
  204. * counter variables, so you can prepare pagelist metadata before calling
  205. * ceph_encode_locks.
  206. */
  207. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  208. {
  209. struct file_lock *lock;
  210. *fcntl_count = 0;
  211. *flock_count = 0;
  212. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  213. if (lock->fl_flags & FL_POSIX)
  214. ++(*fcntl_count);
  215. else if (lock->fl_flags & FL_FLOCK)
  216. ++(*flock_count);
  217. }
  218. dout("counted %d flock locks and %d fcntl locks",
  219. *flock_count, *fcntl_count);
  220. }
  221. /**
  222. * Encode the flock and fcntl locks for the given inode into the ceph_filelock
  223. * array. Must be called with inode->i_lock already held.
  224. * If we encounter more of a specific lock type than expected, return -ENOSPC.
  225. */
  226. int ceph_encode_locks_to_buffer(struct inode *inode,
  227. struct ceph_filelock *flocks,
  228. int num_fcntl_locks, int num_flock_locks)
  229. {
  230. struct file_lock *lock;
  231. int err = 0;
  232. int seen_fcntl = 0;
  233. int seen_flock = 0;
  234. int l = 0;
  235. dout("encoding %d flock and %d fcntl locks", num_flock_locks,
  236. num_fcntl_locks);
  237. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  238. if (lock->fl_flags & FL_POSIX) {
  239. ++seen_fcntl;
  240. if (seen_fcntl > num_fcntl_locks) {
  241. err = -ENOSPC;
  242. goto fail;
  243. }
  244. err = lock_to_ceph_filelock(lock, &flocks[l]);
  245. if (err)
  246. goto fail;
  247. ++l;
  248. }
  249. }
  250. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  251. if (lock->fl_flags & FL_FLOCK) {
  252. ++seen_flock;
  253. if (seen_flock > num_flock_locks) {
  254. err = -ENOSPC;
  255. goto fail;
  256. }
  257. err = lock_to_ceph_filelock(lock, &flocks[l]);
  258. if (err)
  259. goto fail;
  260. ++l;
  261. }
  262. }
  263. fail:
  264. return err;
  265. }
  266. /**
  267. * Copy the encoded flock and fcntl locks into the pagelist.
  268. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  269. * sequential flock locks.
  270. * Returns zero on success.
  271. */
  272. int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  273. struct ceph_pagelist *pagelist,
  274. int num_fcntl_locks, int num_flock_locks)
  275. {
  276. int err = 0;
  277. __le32 nlocks;
  278. nlocks = cpu_to_le32(num_fcntl_locks);
  279. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  280. if (err)
  281. goto out_fail;
  282. err = ceph_pagelist_append(pagelist, flocks,
  283. num_fcntl_locks * sizeof(*flocks));
  284. if (err)
  285. goto out_fail;
  286. nlocks = cpu_to_le32(num_flock_locks);
  287. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  288. if (err)
  289. goto out_fail;
  290. err = ceph_pagelist_append(pagelist,
  291. &flocks[num_fcntl_locks],
  292. num_flock_locks * sizeof(*flocks));
  293. out_fail:
  294. return err;
  295. }
  296. /*
  297. * Given a pointer to a lock, convert it to a ceph filelock
  298. */
  299. int lock_to_ceph_filelock(struct file_lock *lock,
  300. struct ceph_filelock *cephlock)
  301. {
  302. int err = 0;
  303. cephlock->start = cpu_to_le64(lock->fl_start);
  304. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  305. cephlock->client = cpu_to_le64(0);
  306. cephlock->pid = cpu_to_le64((u64)lock->fl_pid);
  307. cephlock->owner = cpu_to_le64(secure_addr(lock->fl_owner));
  308. switch (lock->fl_type) {
  309. case F_RDLCK:
  310. cephlock->type = CEPH_LOCK_SHARED;
  311. break;
  312. case F_WRLCK:
  313. cephlock->type = CEPH_LOCK_EXCL;
  314. break;
  315. case F_UNLCK:
  316. cephlock->type = CEPH_LOCK_UNLOCK;
  317. break;
  318. default:
  319. dout("Have unknown lock type %d", lock->fl_type);
  320. err = -EINVAL;
  321. }
  322. return err;
  323. }