locks.c 6.8 KB

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