locks.c 7.5 KB

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