locks.c 7.5 KB

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