locks.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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. 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,
  73. (u64)(unsigned long)fl->fl_nspid,
  74. lock_cmd, fl->fl_start,
  75. length, wait);
  76. if (!err) {
  77. dout("mds locked, locking locally");
  78. err = posix_lock_file(file, fl, NULL);
  79. if (err && (CEPH_MDS_OP_SETFILELOCK == op)) {
  80. /* undo! This should only happen if the kernel detects
  81. * local deadlock. */
  82. ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  83. (u64)fl->fl_pid,
  84. (u64)(unsigned long)fl->fl_nspid,
  85. CEPH_LOCK_UNLOCK, fl->fl_start,
  86. length, 0);
  87. dout("got %d on posix_lock_file, undid lock", err);
  88. }
  89. } else {
  90. dout("mds returned error code %d", err);
  91. }
  92. return err;
  93. }
  94. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  95. {
  96. u64 length;
  97. u8 lock_cmd;
  98. int err;
  99. u8 wait = 1;
  100. fl->fl_nspid = get_pid(task_tgid(current));
  101. dout("ceph_flock, fl_pid:%d", fl->fl_pid);
  102. /* set wait bit, then clear it out of cmd*/
  103. if (cmd & LOCK_NB)
  104. wait = 0;
  105. cmd = cmd & (LOCK_SH | LOCK_EX | LOCK_UN);
  106. /* set command sequence that Ceph wants to see:
  107. shared lock, exclusive lock, or unlock */
  108. if (LOCK_SH == cmd)
  109. lock_cmd = CEPH_LOCK_SHARED;
  110. else if (LOCK_EX == cmd)
  111. lock_cmd = CEPH_LOCK_EXCL;
  112. else
  113. lock_cmd = CEPH_LOCK_UNLOCK;
  114. /* mds requires start and length rather than start and end */
  115. if (LLONG_MAX == fl->fl_end)
  116. length = 0;
  117. else
  118. length = fl->fl_end - fl->fl_start + 1;
  119. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  120. file, (u64)fl->fl_pid,
  121. (u64)(unsigned long)fl->fl_nspid,
  122. lock_cmd, fl->fl_start,
  123. length, wait);
  124. if (!err) {
  125. err = flock_lock_file_wait(file, fl);
  126. if (err) {
  127. ceph_lock_message(CEPH_LOCK_FLOCK,
  128. CEPH_MDS_OP_SETFILELOCK,
  129. file, (u64)fl->fl_pid,
  130. (u64)(unsigned long)fl->fl_nspid,
  131. CEPH_LOCK_UNLOCK, fl->fl_start,
  132. length, 0);
  133. dout("got %d on flock_lock_file_wait, undid lock", err);
  134. }
  135. } else {
  136. dout("mds error code %d", err);
  137. }
  138. return err;
  139. }
  140. /**
  141. * Must be called with BKL already held. Fills in the passed
  142. * counter variables, so you can prepare pagelist metadata before calling
  143. * ceph_encode_locks.
  144. */
  145. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  146. {
  147. struct file_lock *lock;
  148. *fcntl_count = 0;
  149. *flock_count = 0;
  150. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  151. if (lock->fl_flags & FL_POSIX)
  152. ++(*fcntl_count);
  153. else if (lock->fl_flags & FL_FLOCK)
  154. ++(*flock_count);
  155. }
  156. dout("counted %d flock locks and %d fcntl locks",
  157. *flock_count, *fcntl_count);
  158. }
  159. /**
  160. * Encode the flock and fcntl locks for the given inode into the pagelist.
  161. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  162. * sequential flock locks.
  163. * Must be called with lock_flocks() already held.
  164. * If we encounter more of a specific lock type than expected,
  165. * we return the value 1.
  166. */
  167. int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist,
  168. int num_fcntl_locks, int num_flock_locks)
  169. {
  170. struct file_lock *lock;
  171. struct ceph_filelock cephlock;
  172. int err = 0;
  173. int seen_fcntl = 0;
  174. int seen_flock = 0;
  175. dout("encoding %d flock and %d fcntl locks", num_flock_locks,
  176. num_fcntl_locks);
  177. err = ceph_pagelist_append(pagelist, &num_fcntl_locks, sizeof(u32));
  178. if (err)
  179. goto fail;
  180. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  181. if (lock->fl_flags & FL_POSIX) {
  182. ++seen_fcntl;
  183. if (seen_fcntl > num_fcntl_locks) {
  184. err = -ENOSPC;
  185. goto fail;
  186. }
  187. err = lock_to_ceph_filelock(lock, &cephlock);
  188. if (err)
  189. goto fail;
  190. err = ceph_pagelist_append(pagelist, &cephlock,
  191. sizeof(struct ceph_filelock));
  192. }
  193. if (err)
  194. goto fail;
  195. }
  196. err = ceph_pagelist_append(pagelist, &num_flock_locks, sizeof(u32));
  197. if (err)
  198. goto fail;
  199. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  200. if (lock->fl_flags & FL_FLOCK) {
  201. ++seen_flock;
  202. if (seen_flock > num_flock_locks) {
  203. err = -ENOSPC;
  204. goto fail;
  205. }
  206. err = lock_to_ceph_filelock(lock, &cephlock);
  207. if (err)
  208. goto fail;
  209. err = ceph_pagelist_append(pagelist, &cephlock,
  210. sizeof(struct ceph_filelock));
  211. }
  212. if (err)
  213. goto fail;
  214. }
  215. fail:
  216. return err;
  217. }
  218. /*
  219. * Given a pointer to a lock, convert it to a ceph filelock
  220. */
  221. int lock_to_ceph_filelock(struct file_lock *lock,
  222. struct ceph_filelock *cephlock)
  223. {
  224. int err = 0;
  225. cephlock->start = cpu_to_le64(lock->fl_start);
  226. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  227. cephlock->client = cpu_to_le64(0);
  228. cephlock->pid = cpu_to_le64(lock->fl_pid);
  229. cephlock->pid_namespace =
  230. cpu_to_le64((u64)(unsigned long)lock->fl_nspid);
  231. switch (lock->fl_type) {
  232. case F_RDLCK:
  233. cephlock->type = CEPH_LOCK_SHARED;
  234. break;
  235. case F_WRLCK:
  236. cephlock->type = CEPH_LOCK_EXCL;
  237. break;
  238. case F_UNLCK:
  239. cephlock->type = CEPH_LOCK_UNLOCK;
  240. break;
  241. default:
  242. dout("Have unknown lock type %d", lock->fl_type);
  243. err = -EINVAL;
  244. }
  245. return err;
  246. }