Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 1 | #include <linux/ceph/ceph_debug.h> |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 2 | |
| 3 | #include <linux/file.h> |
| 4 | #include <linux/namei.h> |
| 5 | |
| 6 | #include "super.h" |
| 7 | #include "mds_client.h" |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 8 | #include <linux/ceph/pagelist.h> |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * Implement fcntl and flock locking functions. |
| 12 | */ |
| 13 | static int ceph_lock_message(u8 lock_type, u16 operation, struct file *file, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 14 | int cmd, u8 wait, struct file_lock *fl) |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 15 | { |
| 16 | struct inode *inode = file->f_dentry->d_inode; |
| 17 | struct ceph_mds_client *mdsc = |
Yehuda Sadeh | 3d14c5d | 2010-04-06 15:14:15 -0700 | [diff] [blame] | 18 | ceph_sb_to_client(inode->i_sb)->mdsc; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 19 | struct ceph_mds_request *req; |
| 20 | int err; |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 21 | u64 length = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 22 | |
| 23 | req = ceph_mdsc_create_request(mdsc, operation, USE_AUTH_MDS); |
| 24 | if (IS_ERR(req)) |
| 25 | return PTR_ERR(req); |
| 26 | req->r_inode = igrab(inode); |
| 27 | |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 28 | /* mds requires start and length rather than start and end */ |
| 29 | if (LLONG_MAX == fl->fl_end) |
| 30 | length = 0; |
| 31 | else |
| 32 | length = fl->fl_end - fl->fl_start + 1; |
| 33 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 34 | dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, " |
| 35 | "length: %llu, wait: %d, type`: %d", (int)lock_type, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 36 | (int)operation, (u64)fl->fl_pid, fl->fl_start, |
| 37 | length, wait, fl->fl_type); |
| 38 | |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 39 | |
| 40 | req->r_args.filelock_change.rule = lock_type; |
| 41 | req->r_args.filelock_change.type = cmd; |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 42 | req->r_args.filelock_change.pid = cpu_to_le64((u64)fl->fl_pid); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 43 | /* This should be adjusted, but I'm not sure if |
| 44 | namespaces actually get id numbers*/ |
| 45 | req->r_args.filelock_change.pid_namespace = |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 46 | cpu_to_le64((u64)(unsigned long)fl->fl_nspid); |
| 47 | req->r_args.filelock_change.start = cpu_to_le64(fl->fl_start); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 48 | req->r_args.filelock_change.length = cpu_to_le64(length); |
| 49 | req->r_args.filelock_change.wait = wait; |
| 50 | |
| 51 | err = ceph_mdsc_do_request(mdsc, inode, req); |
| 52 | ceph_mdsc_put_request(req); |
| 53 | dout("ceph_lock_message: rule: %d, op: %d, pid: %llu, start: %llu, " |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 54 | "length: %llu, wait: %d, type`: %d, err code %d", (int)lock_type, |
| 55 | (int)operation, (u64)fl->fl_pid, fl->fl_start, |
| 56 | length, wait, fl->fl_type, err); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 57 | return err; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Attempt to set an fcntl lock. |
| 62 | * For now, this just goes away to the server. Later it may be more awesome. |
| 63 | */ |
| 64 | int ceph_lock(struct file *file, int cmd, struct file_lock *fl) |
| 65 | { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 66 | u8 lock_cmd; |
| 67 | int err; |
| 68 | u8 wait = 0; |
| 69 | u16 op = CEPH_MDS_OP_SETFILELOCK; |
| 70 | |
| 71 | fl->fl_nspid = get_pid(task_tgid(current)); |
| 72 | dout("ceph_lock, fl_pid:%d", fl->fl_pid); |
| 73 | |
| 74 | /* set wait bit as appropriate, then make command as Ceph expects it*/ |
| 75 | if (F_SETLKW == cmd) |
| 76 | wait = 1; |
| 77 | if (F_GETLK == cmd) |
| 78 | op = CEPH_MDS_OP_GETFILELOCK; |
| 79 | |
| 80 | if (F_RDLCK == fl->fl_type) |
| 81 | lock_cmd = CEPH_LOCK_SHARED; |
| 82 | else if (F_WRLCK == fl->fl_type) |
| 83 | lock_cmd = CEPH_LOCK_EXCL; |
| 84 | else |
| 85 | lock_cmd = CEPH_LOCK_UNLOCK; |
| 86 | |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 87 | err = ceph_lock_message(CEPH_LOCK_FCNTL, op, file, lock_cmd, wait, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 88 | if (!err) { |
| 89 | dout("mds locked, locking locally"); |
| 90 | err = posix_lock_file(file, fl, NULL); |
| 91 | if (err && (CEPH_MDS_OP_SETFILELOCK == op)) { |
| 92 | /* undo! This should only happen if the kernel detects |
| 93 | * local deadlock. */ |
| 94 | ceph_lock_message(CEPH_LOCK_FCNTL, op, file, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 95 | CEPH_LOCK_UNLOCK, 0, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 96 | dout("got %d on posix_lock_file, undid lock", err); |
| 97 | } |
| 98 | } else { |
| 99 | dout("mds returned error code %d", err); |
| 100 | } |
| 101 | return err; |
| 102 | } |
| 103 | |
| 104 | int ceph_flock(struct file *file, int cmd, struct file_lock *fl) |
| 105 | { |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 106 | u8 lock_cmd; |
| 107 | int err; |
| 108 | u8 wait = 1; |
| 109 | |
| 110 | fl->fl_nspid = get_pid(task_tgid(current)); |
| 111 | dout("ceph_flock, fl_pid:%d", fl->fl_pid); |
| 112 | |
| 113 | /* set wait bit, then clear it out of cmd*/ |
| 114 | if (cmd & LOCK_NB) |
| 115 | wait = 0; |
| 116 | cmd = cmd & (LOCK_SH | LOCK_EX | LOCK_UN); |
| 117 | /* set command sequence that Ceph wants to see: |
| 118 | shared lock, exclusive lock, or unlock */ |
| 119 | if (LOCK_SH == cmd) |
| 120 | lock_cmd = CEPH_LOCK_SHARED; |
| 121 | else if (LOCK_EX == cmd) |
| 122 | lock_cmd = CEPH_LOCK_EXCL; |
| 123 | else |
| 124 | lock_cmd = CEPH_LOCK_UNLOCK; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 125 | |
| 126 | err = ceph_lock_message(CEPH_LOCK_FLOCK, CEPH_MDS_OP_SETFILELOCK, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 127 | file, lock_cmd, wait, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 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, |
Herb Shiu | 637ae8d | 2010-11-23 13:42:23 -0800 | [diff] [blame^] | 133 | file, CEPH_LOCK_UNLOCK, 0, fl); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 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 | /** |
| 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 | |
| 151 | *fcntl_count = 0; |
| 152 | *flock_count = 0; |
| 153 | |
| 154 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 155 | if (lock->fl_flags & FL_POSIX) |
| 156 | ++(*fcntl_count); |
| 157 | else if (lock->fl_flags & FL_FLOCK) |
| 158 | ++(*flock_count); |
| 159 | } |
| 160 | dout("counted %d flock locks and %d fcntl locks", |
| 161 | *flock_count, *fcntl_count); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Encode the flock and fcntl locks for the given inode into the pagelist. |
| 166 | * Format is: #fcntl locks, sequential fcntl locks, #flock locks, |
| 167 | * sequential flock locks. |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 168 | * Must be called with lock_flocks() already held. |
| 169 | * If we encounter more of a specific lock type than expected, |
| 170 | * we return the value 1. |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 171 | */ |
| 172 | int ceph_encode_locks(struct inode *inode, struct ceph_pagelist *pagelist, |
| 173 | int num_fcntl_locks, int num_flock_locks) |
| 174 | { |
| 175 | struct file_lock *lock; |
| 176 | struct ceph_filelock cephlock; |
| 177 | int err = 0; |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 178 | int seen_fcntl = 0; |
| 179 | int seen_flock = 0; |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 180 | |
| 181 | dout("encoding %d flock and %d fcntl locks", num_flock_locks, |
| 182 | num_fcntl_locks); |
| 183 | err = ceph_pagelist_append(pagelist, &num_fcntl_locks, sizeof(u32)); |
| 184 | if (err) |
| 185 | goto fail; |
| 186 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 187 | if (lock->fl_flags & FL_POSIX) { |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 188 | ++seen_fcntl; |
| 189 | if (seen_fcntl > num_fcntl_locks) { |
| 190 | err = -ENOSPC; |
| 191 | goto fail; |
| 192 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 193 | err = lock_to_ceph_filelock(lock, &cephlock); |
| 194 | if (err) |
| 195 | goto fail; |
| 196 | err = ceph_pagelist_append(pagelist, &cephlock, |
| 197 | sizeof(struct ceph_filelock)); |
| 198 | } |
| 199 | if (err) |
| 200 | goto fail; |
| 201 | } |
| 202 | |
| 203 | err = ceph_pagelist_append(pagelist, &num_flock_locks, sizeof(u32)); |
| 204 | if (err) |
| 205 | goto fail; |
| 206 | for (lock = inode->i_flock; lock != NULL; lock = lock->fl_next) { |
| 207 | if (lock->fl_flags & FL_FLOCK) { |
Greg Farnum | fca4451 | 2010-09-17 10:24:02 -0700 | [diff] [blame] | 208 | ++seen_flock; |
| 209 | if (seen_flock > num_flock_locks) { |
| 210 | err = -ENOSPC; |
| 211 | goto fail; |
| 212 | } |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 213 | err = lock_to_ceph_filelock(lock, &cephlock); |
| 214 | if (err) |
| 215 | goto fail; |
| 216 | err = ceph_pagelist_append(pagelist, &cephlock, |
| 217 | sizeof(struct ceph_filelock)); |
| 218 | } |
| 219 | if (err) |
| 220 | goto fail; |
| 221 | } |
| 222 | fail: |
| 223 | return err; |
| 224 | } |
| 225 | |
| 226 | /* |
| 227 | * Given a pointer to a lock, convert it to a ceph filelock |
| 228 | */ |
| 229 | int lock_to_ceph_filelock(struct file_lock *lock, |
| 230 | struct ceph_filelock *cephlock) |
| 231 | { |
| 232 | int err = 0; |
| 233 | |
| 234 | cephlock->start = cpu_to_le64(lock->fl_start); |
| 235 | cephlock->length = cpu_to_le64(lock->fl_end - lock->fl_start + 1); |
| 236 | cephlock->client = cpu_to_le64(0); |
| 237 | cephlock->pid = cpu_to_le64(lock->fl_pid); |
Alan Cox | ad8453a | 2010-08-25 13:26:32 +0100 | [diff] [blame] | 238 | cephlock->pid_namespace = |
| 239 | cpu_to_le64((u64)(unsigned long)lock->fl_nspid); |
Greg Farnum | 40819f6 | 2010-08-02 15:34:23 -0700 | [diff] [blame] | 240 | |
| 241 | switch (lock->fl_type) { |
| 242 | case F_RDLCK: |
| 243 | cephlock->type = CEPH_LOCK_SHARED; |
| 244 | break; |
| 245 | case F_WRLCK: |
| 246 | cephlock->type = CEPH_LOCK_EXCL; |
| 247 | break; |
| 248 | case F_UNLCK: |
| 249 | cephlock->type = CEPH_LOCK_UNLOCK; |
| 250 | break; |
| 251 | default: |
| 252 | dout("Have unknown lock type %d", lock->fl_type); |
| 253 | err = -EINVAL; |
| 254 | } |
| 255 | |
| 256 | return err; |
| 257 | } |