locks.c 8.7 KB

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