locks.c 8.7 KB

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