locks.c 8.5 KB

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