locks.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "ceph_debug.h"
  2. #include <linux/file.h>
  3. #include <linux/namei.h>
  4. #include "super.h"
  5. #include "mds_client.h"
  6. #include "pagelist.h"
  7. /**
  8. * Implement fcntl and flock locking functions.
  9. */
  10. static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file,
  11. u64 pid, u64 pid_ns,
  12. int cmd, u64 start, u64 length, u8 wait)
  13. {
  14. struct inode *inode = file->f_dentry->d_inode;
  15. struct ceph_mds_client *mdsc =
  16. &ceph_sb_to_client(inode->i_sb)->mdsc;
  17. struct ceph_mds_request *req;
  18. int err;
  19. req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS);
  20. if (IS_ERR(req))
  21. return PTR_ERR(req);
  22. req->r_inode = igrab(inode);
  23. dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
  24. "length: %llu, wait: %d, type`: %d", (int)lock_type,
  25. (int)operation, pid, start, length, wait, cmd);
  26. req->r_args.filelock_change.rule = lock_type;
  27. req->r_args.filelock_change.type = cmd;
  28. req->r_args.filelock_change.pid = cpu_to_le64(pid);
  29. /* This should be adjusted, but I'm not sure if
  30. namespaces actually get id numbers*/
  31. req->r_args.filelock_change.pid_namespace =
  32. cpu_to_le64((u64)pid_ns);
  33. req->r_args.filelock_change.start = cpu_to_le64(start);
  34. req->r_args.filelock_change.length = cpu_to_le64(length);
  35. req->r_args.filelock_change.wait = wait;
  36. err = ceph_mdsc_do_request(mdsc, inode, req);
  37. ceph_mdsc_put_request(req);
  38. dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, "
  39. "length: %llu, wait: %d, type`: %d err code %d", (int)lock_type,
  40. (int)operation, pid, start, length, wait, cmd, err);
  41. return err;
  42. }
  43. /**
  44. * Attempt to set an fcntl lock.
  45. * For now, this just goes away to the server. Later it may be more awesome.
  46. */
  47. int ceph_lock(struct file *file, int cmd, struct file_lock *fl)
  48. {
  49. u64 length;
  50. u8 lock_cmd;
  51. int err;
  52. u8 wait = 0;
  53. u16 op = CEPH_MDS_OP_SETFILELOCK;
  54. fl->fl_nspid = get_pid(task_tgid(current));
  55. dout("ceph_lock, fl_pid:%d", fl->fl_pid);
  56. /* set wait bit as appropriate, then make command as Ceph expects it*/
  57. if (F_SETLKW == cmd)
  58. wait = 1;
  59. if (F_GETLK == cmd)
  60. op = CEPH_MDS_OP_GETFILELOCK;
  61. if (F_RDLCK == fl->fl_type)
  62. lock_cmd = CEPH_LOCK_SHARED;
  63. else if (F_WRLCK == fl->fl_type)
  64. lock_cmd = CEPH_LOCK_EXCL;
  65. else
  66. lock_cmd = CEPH_LOCK_UNLOCK;
  67. if (LLONG_MAX == fl->fl_end)
  68. length = 0;
  69. else
  70. length = fl->fl_end - fl->fl_start + 1;
  71. err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  72. (u64)fl->fl_pid, (u64)fl->fl_nspid,
  73. lock_cmd, fl->fl_start,
  74. length, wait);
  75. if (!err) {
  76. dout("mds locked, locking locally");
  77. err = posix_lock_file(file, fl, NULL);
  78. if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
  79. /* undo! This should only happen if the kernel detects
  80. * local deadlock. */
  81. ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  82. (u64)fl->fl_pid, (u64)fl->fl_nspid,
  83. CEPH_LOCK_UNLOCK, fl->fl_start,
  84. length, 0);
  85. dout("got %d on posix_lock_file, undid lock", err);
  86. }
  87. } else {
  88. dout("mds returned error code %d", err);
  89. }
  90. return err;
  91. }
  92. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  93. {
  94. u64 length;
  95. u8 lock_cmd;
  96. int err;
  97. u8 wait = 1;
  98. fl->fl_nspid = get_pid(task_tgid(current));
  99. dout("ceph_flock, fl_pid:%d", fl->fl_pid);
  100. /* set wait bit, then clear it out of cmd*/
  101. if (cmd & LOCK_NB)
  102. wait = 0;
  103. cmd = cmd & (LOCK_SH | LOCK_EX | LOCK_UN);
  104. /* set command sequence that Ceph wants to see:
  105. shared lock, exclusive lock, or unlock */
  106. if (LOCK_SH == cmd)
  107. lock_cmd = CEPH_LOCK_SHARED;
  108. else if (LOCK_EX == cmd)
  109. lock_cmd = CEPH_LOCK_EXCL;
  110. else
  111. lock_cmd = CEPH_LOCK_UNLOCK;
  112. /* mds requires start and length rather than start and end */
  113. if (LLONG_MAX == fl->fl_end)
  114. length = 0;
  115. else
  116. length = fl->fl_end - fl->fl_start + 1;
  117. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  118. file, (u64)fl->fl_pid, (u64)fl->fl_nspid,
  119. lock_cmd, fl->fl_start,
  120. length, wait);
  121. if (!err) {
  122. err = flock_lock_file_wait(file, fl);
  123. if (err) {
  124. ceph_lock_message(CEPH_LOCK_FLOCK,
  125. CEPH_MDS_OP_SETFILELOCK,
  126. file, (u64)fl->fl_pid,
  127. (u64)fl->fl_nspid,
  128. CEPH_LOCK_UNLOCK, fl->fl_start,
  129. length, 0);
  130. dout("got %d on flock_lock_file_wait, undid lock", err);
  131. }
  132. } else {
  133. dout("mds error code %d", err);
  134. }
  135. return err;
  136. }
  137. /**
  138. * Must be called with BKL already held. Fills in the passed
  139. * counter variables, so you can prepare pagelist metadata before calling
  140. * ceph_encode_locks.
  141. */
  142. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  143. {
  144. struct file_lock *lock;
  145. *fcntl_count = 0;
  146. *flock_count = 0;
  147. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  148. if (lock->fl_flags & FL_POSIX)
  149. ++(*fcntl_count);
  150. else if (lock->fl_flags & FL_FLOCK)
  151. ++(*flock_count);
  152. }
  153. dout("counted %d flock locks and %d fcntl locks",
  154. *flock_count, *fcntl_count);
  155. }
  156. /**
  157. * Encode the flock and fcntl locks for the given inode into the pagelist.
  158. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  159. * sequential flock locks.
  160. * Must be called with BLK already held, and the lock numbers should have
  161. * been gathered under the same lock holding window.
  162. */
  163. int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
  164. int num_fcntl_locks, int num_flock_locks)
  165. {
  166. struct file_lock *lock;
  167. struct ceph_filelock cephlock;
  168. int err = 0;
  169. dout("encoding %d flock and %d fcntl locks", num_flock_locks,
  170. num_fcntl_locks);
  171. err = ceph_pagelist_append(pagelist, &num_fcntl_locks, sizeof(u32));
  172. if (err)
  173. goto fail;
  174. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  175. if (lock->fl_flags & FL_POSIX) {
  176. err = lock_to_ceph_filelock(lock, &cephlock);
  177. if (err)
  178. goto fail;
  179. err = ceph_pagelist_append(pagelist, &cephlock,
  180. sizeof(struct ceph_filelock));
  181. }
  182. if (err)
  183. goto fail;
  184. }
  185. err = ceph_pagelist_append(pagelist, &num_flock_locks, sizeof(u32));
  186. if (err)
  187. goto fail;
  188. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  189. if (lock->fl_flags & FL_FLOCK) {
  190. err = lock_to_ceph_filelock(lock, &cephlock);
  191. if (err)
  192. goto fail;
  193. err = ceph_pagelist_append(pagelist, &cephlock,
  194. sizeof(struct ceph_filelock));
  195. }
  196. if (err)
  197. goto fail;
  198. }
  199. fail:
  200. return err;
  201. }
  202. /*
  203. * Given a pointer to a lock, convert it to a ceph filelock
  204. */
  205. int lock_to_ceph_filelock(struct file_lock *lock,
  206. struct ceph_filelock *cephlock)
  207. {
  208. int err = 0;
  209. cephlock->start = cpu_to_le64(lock->fl_start);
  210. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  211. cephlock->client = cpu_to_le64(0);
  212. cephlock->pid = cpu_to_le64(lock->fl_pid);
  213. cephlock->pid_namespace = cpu_to_le64((u64)lock->fl_nspid);
  214. switch (lock->fl_type) {
  215. case F_RDLCK:
  216. cephlock->type = CEPH_LOCK_SHARED;
  217. break;
  218. case F_WRLCK:
  219. cephlock->type = CEPH_LOCK_EXCL;
  220. break;
  221. case F_UNLCK:
  222. cephlock->type = CEPH_LOCK_UNLOCK;
  223. break;
  224. default:
  225. dout("Have unknown lock type %d", lock->fl_type);
  226. err = -EINVAL;
  227. }
  228. return err;
  229. }