locks.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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
  97. * the kernel detects local
  98. * deadlock. */
  99. ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  100. CEPH_LOCK_UNLOCK, 0, fl);
  101. dout("got %d on posix_lock_file, undid lock",
  102. err);
  103. }
  104. }
  105. } else if (err == -ERESTARTSYS) {
  106. dout("undoing lock\n");
  107. ceph_lock_message(CEPH_LOCK_FCNTL, op, file,
  108. CEPH_LOCK_UNLOCK, 0, fl);
  109. }
  110. return err;
  111. }
  112. int ceph_flock(struct file *file, int cmd, struct file_lock *fl)
  113. {
  114. u8 lock_cmd;
  115. int err;
  116. u8 wait = 1;
  117. fl->fl_nspid = get_pid(task_tgid(current));
  118. dout("ceph_flock, fl_pid:%d", fl->fl_pid);
  119. /* set wait bit, then clear it out of cmd*/
  120. if (cmd & LOCK_NB)
  121. wait = 0;
  122. cmd = cmd & (LOCK_SH | LOCK_EX | LOCK_UN);
  123. /* set command sequence that Ceph wants to see:
  124. shared lock, exclusive lock, or unlock */
  125. if (LOCK_SH == cmd)
  126. lock_cmd = CEPH_LOCK_SHARED;
  127. else if (LOCK_EX == cmd)
  128. lock_cmd = CEPH_LOCK_EXCL;
  129. else
  130. lock_cmd = CEPH_LOCK_UNLOCK;
  131. err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK,
  132. file, lock_cmd, wait, fl);
  133. if (!err) {
  134. err = flock_lock_file_wait(file, fl);
  135. if (err) {
  136. ceph_lock_message(CEPH_LOCK_FLOCK,
  137. CEPH_MDS_OP_SETFILELOCK,
  138. file, CEPH_LOCK_UNLOCK, 0, fl);
  139. dout("got %d on flock_lock_file_wait, undid lock", err);
  140. }
  141. } else if (err == -ERESTARTSYS) {
  142. dout("undoing lock\n");
  143. ceph_lock_message(CEPH_LOCK_FLOCK,
  144. CEPH_MDS_OP_SETFILELOCK,
  145. file, CEPH_LOCK_UNLOCK, 0, fl);
  146. }
  147. return err;
  148. }
  149. /**
  150. * Must be called with BKL already held. Fills in the passed
  151. * counter variables, so you can prepare pagelist metadata before calling
  152. * ceph_encode_locks.
  153. */
  154. void ceph_count_locks(struct inode *inode, int *fcntl_count, int *flock_count)
  155. {
  156. struct file_lock *lock;
  157. *fcntl_count = 0;
  158. *flock_count = 0;
  159. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  160. if (lock->fl_flags & FL_POSIX)
  161. ++(*fcntl_count);
  162. else if (lock->fl_flags & FL_FLOCK)
  163. ++(*flock_count);
  164. }
  165. dout("counted %d flock locks and %d fcntl locks",
  166. *flock_count, *fcntl_count);
  167. }
  168. /**
  169. * Encode the flock and fcntl locks for the given inode into the ceph_filelock
  170. * array. Must be called with lock_flocks() already held.
  171. * If we encounter more of a specific lock type than expected, return -ENOSPC.
  172. */
  173. int ceph_encode_locks_to_buffer(struct inode *inode,
  174. struct ceph_filelock *flocks,
  175. int num_fcntl_locks, int num_flock_locks)
  176. {
  177. struct file_lock *lock;
  178. int err = 0;
  179. int seen_fcntl = 0;
  180. int seen_flock = 0;
  181. int l = 0;
  182. dout("encoding %d flock and %d fcntl locks", num_flock_locks,
  183. num_fcntl_locks);
  184. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  185. if (lock->fl_flags & FL_POSIX) {
  186. ++seen_fcntl;
  187. if (seen_fcntl > num_fcntl_locks) {
  188. err = -ENOSPC;
  189. goto fail;
  190. }
  191. err = lock_to_ceph_filelock(lock, &flocks[l]);
  192. if (err)
  193. goto fail;
  194. ++l;
  195. }
  196. }
  197. for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) {
  198. if (lock->fl_flags & FL_FLOCK) {
  199. ++seen_flock;
  200. if (seen_flock > num_flock_locks) {
  201. err = -ENOSPC;
  202. goto fail;
  203. }
  204. err = lock_to_ceph_filelock(lock, &flocks[l]);
  205. if (err)
  206. goto fail;
  207. ++l;
  208. }
  209. }
  210. fail:
  211. return err;
  212. }
  213. /**
  214. * Copy the encoded flock and fcntl locks into the pagelist.
  215. * Format is: #fcntl locks, sequential fcntl locks, #flock locks,
  216. * sequential flock locks.
  217. * Returns zero on success.
  218. */
  219. int ceph_locks_to_pagelist(struct ceph_filelock *flocks,
  220. struct ceph_pagelist *pagelist,
  221. int num_fcntl_locks, int num_flock_locks)
  222. {
  223. int err = 0;
  224. __le32 nlocks;
  225. nlocks = cpu_to_le32(num_fcntl_locks);
  226. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  227. if (err)
  228. goto out_fail;
  229. err = ceph_pagelist_append(pagelist, flocks,
  230. num_fcntl_locks * sizeof(*flocks));
  231. if (err)
  232. goto out_fail;
  233. nlocks = cpu_to_le32(num_flock_locks);
  234. err = ceph_pagelist_append(pagelist, &nlocks, sizeof(nlocks));
  235. if (err)
  236. goto out_fail;
  237. err = ceph_pagelist_append(pagelist,
  238. &flocks[num_fcntl_locks],
  239. num_flock_locks * sizeof(*flocks));
  240. out_fail:
  241. return err;
  242. }
  243. /*
  244. * Given a pointer to a lock, convert it to a ceph filelock
  245. */
  246. int lock_to_ceph_filelock(struct file_lock *lock,
  247. struct ceph_filelock *cephlock)
  248. {
  249. int err = 0;
  250. cephlock->start = cpu_to_le64(lock->fl_start);
  251. cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1);
  252. cephlock->client = cpu_to_le64(0);
  253. cephlock->pid = cpu_to_le64(lock->fl_pid);
  254. cephlock->pid_namespace =
  255. cpu_to_le64((u64)(unsigned long)lock->fl_nspid);
  256. switch (lock->fl_type) {
  257. case F_RDLCK:
  258. cephlock->type = CEPH_LOCK_SHARED;
  259. break;
  260. case F_WRLCK:
  261. cephlock->type = CEPH_LOCK_EXCL;
  262. break;
  263. case F_UNLCK:
  264. cephlock->type = CEPH_LOCK_UNLOCK;
  265. break;
  266. default:
  267. dout("Have unknown lock type %d", lock->fl_type);
  268. err = -EINVAL;
  269. }
  270. return err;
  271. }