[745] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | Locking functions
|
---|
| 4 | Copyright (C) Jeremy Allison 1992-2006
|
---|
| 5 |
|
---|
| 6 | This program is free software; you can redistribute it and/or modify
|
---|
| 7 | it under the terms of the GNU General Public License as published by
|
---|
| 8 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 9 | (at your option) any later version.
|
---|
| 10 |
|
---|
| 11 | This program is distributed in the hope that it will be useful,
|
---|
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | GNU General Public License for more details.
|
---|
| 15 |
|
---|
| 16 | You should have received a copy of the GNU General Public License
|
---|
| 17 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
[454] | 18 |
|
---|
[745] | 19 | Revision History:
|
---|
| 20 |
|
---|
| 21 | POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "includes.h"
|
---|
| 25 | #include "system/filesys.h"
|
---|
| 26 | #include "locking/proto.h"
|
---|
| 27 | #include "dbwrap.h"
|
---|
| 28 | #include "util_tdb.h"
|
---|
| 29 |
|
---|
| 30 | #undef DBGC_CLASS
|
---|
| 31 | #define DBGC_CLASS DBGC_LOCKING
|
---|
| 32 |
|
---|
| 33 | /*
|
---|
| 34 | * The pending close database handle.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | static struct db_context *posix_pending_close_db;
|
---|
| 38 |
|
---|
| 39 | /****************************************************************************
|
---|
| 40 | First - the functions that deal with the underlying system locks - these
|
---|
| 41 | functions are used no matter if we're mapping CIFS Windows locks or CIFS
|
---|
| 42 | POSIX locks onto POSIX.
|
---|
| 43 | ****************************************************************************/
|
---|
| 44 |
|
---|
| 45 | /****************************************************************************
|
---|
| 46 | Utility function to map a lock type correctly depending on the open
|
---|
| 47 | mode of a file.
|
---|
| 48 | ****************************************************************************/
|
---|
| 49 |
|
---|
| 50 | static int map_posix_lock_type( files_struct *fsp, enum brl_type lock_type)
|
---|
| 51 | {
|
---|
| 52 | if((lock_type == WRITE_LOCK) && !fsp->can_write) {
|
---|
| 53 | /*
|
---|
| 54 | * Many UNIX's cannot get a write lock on a file opened read-only.
|
---|
| 55 | * Win32 locking semantics allow this.
|
---|
| 56 | * Do the best we can and attempt a read-only lock.
|
---|
| 57 | */
|
---|
| 58 | DEBUG(10,("map_posix_lock_type: Downgrading write lock to read due to read-only file.\n"));
|
---|
| 59 | return F_RDLCK;
|
---|
[454] | 60 | }
|
---|
[745] | 61 |
|
---|
| 62 | /*
|
---|
| 63 | * This return should be the most normal, as we attempt
|
---|
| 64 | * to always open files read/write.
|
---|
| 65 | */
|
---|
| 66 |
|
---|
| 67 | return (lock_type == READ_LOCK) ? F_RDLCK : F_WRLCK;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | /****************************************************************************
|
---|
| 71 | Debugging aid :-).
|
---|
| 72 | ****************************************************************************/
|
---|
| 73 |
|
---|
| 74 | static const char *posix_lock_type_name(int lock_type)
|
---|
| 75 | {
|
---|
| 76 | return (lock_type == F_RDLCK) ? "READ" : "WRITE";
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | /****************************************************************************
|
---|
| 80 | Check to see if the given unsigned lock range is within the possible POSIX
|
---|
| 81 | range. Modifies the given args to be in range if possible, just returns
|
---|
| 82 | False if not.
|
---|
| 83 | ****************************************************************************/
|
---|
| 84 |
|
---|
| 85 | static bool posix_lock_in_range(SMB_OFF_T *offset_out, SMB_OFF_T *count_out,
|
---|
| 86 | uint64_t u_offset, uint64_t u_count)
|
---|
| 87 | {
|
---|
| 88 | SMB_OFF_T offset = (SMB_OFF_T)u_offset;
|
---|
| 89 | SMB_OFF_T count = (SMB_OFF_T)u_count;
|
---|
| 90 |
|
---|
| 91 | /*
|
---|
| 92 | * For the type of system we are, attempt to
|
---|
| 93 | * find the maximum positive lock offset as an SMB_OFF_T.
|
---|
| 94 | */
|
---|
| 95 |
|
---|
| 96 | #if defined(MAX_POSITIVE_LOCK_OFFSET) /* Some systems have arbitrary limits. */
|
---|
| 97 |
|
---|
| 98 | SMB_OFF_T max_positive_lock_offset = (MAX_POSITIVE_LOCK_OFFSET);
|
---|
| 99 |
|
---|
| 100 | #elif defined(LARGE_SMB_OFF_T) && !defined(HAVE_BROKEN_FCNTL64_LOCKS)
|
---|
| 101 |
|
---|
| 102 | /*
|
---|
| 103 | * In this case SMB_OFF_T is 64 bits,
|
---|
| 104 | * and the underlying system can handle 64 bit signed locks.
|
---|
| 105 | */
|
---|
| 106 |
|
---|
| 107 | SMB_OFF_T mask2 = ((SMB_OFF_T)0x4) << (SMB_OFF_T_BITS-4);
|
---|
| 108 | SMB_OFF_T mask = (mask2<<1);
|
---|
| 109 | SMB_OFF_T max_positive_lock_offset = ~mask;
|
---|
| 110 |
|
---|
| 111 | #else /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
|
---|
| 112 |
|
---|
| 113 | /*
|
---|
| 114 | * In this case either SMB_OFF_T is 32 bits,
|
---|
| 115 | * or the underlying system cannot handle 64 bit signed locks.
|
---|
| 116 | * All offsets & counts must be 2^31 or less.
|
---|
| 117 | */
|
---|
| 118 |
|
---|
| 119 | SMB_OFF_T max_positive_lock_offset = 0x7FFFFFFF;
|
---|
| 120 |
|
---|
| 121 | #endif /* !LARGE_SMB_OFF_T || HAVE_BROKEN_FCNTL64_LOCKS */
|
---|
| 122 |
|
---|
| 123 | /*
|
---|
| 124 | * POSIX locks of length zero mean lock to end-of-file.
|
---|
| 125 | * Win32 locks of length zero are point probes. Ignore
|
---|
| 126 | * any Win32 locks of length zero. JRA.
|
---|
| 127 | */
|
---|
| 128 |
|
---|
| 129 | if (count == (SMB_OFF_T)0) {
|
---|
| 130 | DEBUG(10,("posix_lock_in_range: count = 0, ignoring.\n"));
|
---|
| 131 | return False;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
| 135 | * If the given offset was > max_positive_lock_offset then we cannot map this at all
|
---|
| 136 | * ignore this lock.
|
---|
| 137 | */
|
---|
| 138 |
|
---|
| 139 | if (u_offset & ~((uint64_t)max_positive_lock_offset)) {
|
---|
| 140 | DEBUG(10,("posix_lock_in_range: (offset = %.0f) offset > %.0f and we cannot handle this. Ignoring lock.\n",
|
---|
| 141 | (double)u_offset, (double)((uint64_t)max_positive_lock_offset) ));
|
---|
| 142 | return False;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /*
|
---|
| 146 | * We must truncate the count to less than max_positive_lock_offset.
|
---|
| 147 | */
|
---|
| 148 |
|
---|
| 149 | if (u_count & ~((uint64_t)max_positive_lock_offset)) {
|
---|
| 150 | count = max_positive_lock_offset;
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | /*
|
---|
| 154 | * Truncate count to end at max lock offset.
|
---|
| 155 | */
|
---|
| 156 |
|
---|
| 157 | if (offset + count < 0 || offset + count > max_positive_lock_offset) {
|
---|
| 158 | count = max_positive_lock_offset - offset;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | /*
|
---|
| 162 | * If we ate all the count, ignore this lock.
|
---|
| 163 | */
|
---|
| 164 |
|
---|
| 165 | if (count == 0) {
|
---|
| 166 | DEBUG(10,("posix_lock_in_range: Count = 0. Ignoring lock u_offset = %.0f, u_count = %.0f\n",
|
---|
| 167 | (double)u_offset, (double)u_count ));
|
---|
| 168 | return False;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | /*
|
---|
| 172 | * The mapping was successful.
|
---|
| 173 | */
|
---|
| 174 |
|
---|
| 175 | DEBUG(10,("posix_lock_in_range: offset_out = %.0f, count_out = %.0f\n",
|
---|
| 176 | (double)offset, (double)count ));
|
---|
| 177 |
|
---|
| 178 | *offset_out = offset;
|
---|
| 179 | *count_out = count;
|
---|
| 180 |
|
---|
| 181 | return True;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
| 184 | bool smb_vfs_call_lock(struct vfs_handle_struct *handle,
|
---|
| 185 | struct files_struct *fsp, int op, SMB_OFF_T offset,
|
---|
| 186 | SMB_OFF_T count, int type)
|
---|
| 187 | {
|
---|
| 188 | VFS_FIND(lock);
|
---|
| 189 | return handle->fns->lock(handle, fsp, op, offset, count, type);
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | /****************************************************************************
|
---|
| 193 | Actual function that does POSIX locks. Copes with 64 -> 32 bit cruft and
|
---|
| 194 | broken NFS implementations.
|
---|
| 195 | ****************************************************************************/
|
---|
| 196 |
|
---|
| 197 | static bool posix_fcntl_lock(files_struct *fsp, int op, SMB_OFF_T offset, SMB_OFF_T count, int type)
|
---|
| 198 | {
|
---|
| 199 | bool ret;
|
---|
| 200 |
|
---|
| 201 | DEBUG(8,("posix_fcntl_lock %d %d %.0f %.0f %d\n",fsp->fh->fd,op,(double)offset,(double)count,type));
|
---|
| 202 |
|
---|
| 203 | ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
|
---|
| 204 |
|
---|
| 205 | if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
|
---|
| 206 |
|
---|
| 207 | DEBUG(0,("posix_fcntl_lock: WARNING: lock request at offset %.0f, length %.0f returned\n",
|
---|
| 208 | (double)offset,(double)count));
|
---|
| 209 | DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
|
---|
| 210 | DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
|
---|
| 211 |
|
---|
| 212 | /*
|
---|
| 213 | * If the offset is > 0x7FFFFFFF then this will cause problems on
|
---|
| 214 | * 32 bit NFS mounted filesystems. Just ignore it.
|
---|
| 215 | */
|
---|
| 216 |
|
---|
| 217 | if (offset & ~((SMB_OFF_T)0x7fffffff)) {
|
---|
| 218 | DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
|
---|
| 219 | return True;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | if (count & ~((SMB_OFF_T)0x7fffffff)) {
|
---|
| 223 | /* 32 bit NFS file system, retry with smaller offset */
|
---|
| 224 | DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
|
---|
| 225 | errno = 0;
|
---|
| 226 | count &= 0x7fffffff;
|
---|
| 227 | ret = SMB_VFS_LOCK(fsp, op, offset, count, type);
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | DEBUG(8,("posix_fcntl_lock: Lock call %s\n", ret ? "successful" : "failed"));
|
---|
| 232 | return ret;
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | bool smb_vfs_call_getlock(struct vfs_handle_struct *handle,
|
---|
| 236 | struct files_struct *fsp, SMB_OFF_T *poffset,
|
---|
| 237 | SMB_OFF_T *pcount, int *ptype, pid_t *ppid)
|
---|
| 238 | {
|
---|
| 239 | VFS_FIND(getlock);
|
---|
| 240 | return handle->fns->getlock(handle, fsp, poffset, pcount, ptype, ppid);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | /****************************************************************************
|
---|
| 244 | Actual function that gets POSIX locks. Copes with 64 -> 32 bit cruft and
|
---|
| 245 | broken NFS implementations.
|
---|
| 246 | ****************************************************************************/
|
---|
| 247 |
|
---|
| 248 | static bool posix_fcntl_getlock(files_struct *fsp, SMB_OFF_T *poffset, SMB_OFF_T *pcount, int *ptype)
|
---|
| 249 | {
|
---|
| 250 | pid_t pid;
|
---|
| 251 | bool ret;
|
---|
| 252 |
|
---|
| 253 | DEBUG(8,("posix_fcntl_getlock %d %.0f %.0f %d\n",
|
---|
| 254 | fsp->fh->fd,(double)*poffset,(double)*pcount,*ptype));
|
---|
| 255 |
|
---|
| 256 | ret = SMB_VFS_GETLOCK(fsp, poffset, pcount, ptype, &pid);
|
---|
| 257 |
|
---|
| 258 | #ifndef __OS2__ /* file locks currently fail with OS/2's libc */
|
---|
| 259 | if (!ret && ((errno == EFBIG) || (errno == ENOLCK) || (errno == EINVAL))) {
|
---|
| 260 |
|
---|
| 261 | DEBUG(0,("posix_fcntl_getlock: WARNING: lock request at offset %.0f, length %.0f returned\n",
|
---|
| 262 | (double)*poffset,(double)*pcount));
|
---|
| 263 | DEBUGADD(0,("an %s error. This can happen when using 64 bit lock offsets\n", strerror(errno)));
|
---|
| 264 | DEBUGADD(0,("on 32 bit NFS mounted file systems.\n"));
|
---|
| 265 |
|
---|
| 266 | /*
|
---|
| 267 | * If the offset is > 0x7FFFFFFF then this will cause problems on
|
---|
| 268 | * 32 bit NFS mounted filesystems. Just ignore it.
|
---|
| 269 | */
|
---|
| 270 |
|
---|
| 271 | if (*poffset & ~((SMB_OFF_T)0x7fffffff)) {
|
---|
| 272 | DEBUG(0,("Offset greater than 31 bits. Returning success.\n"));
|
---|
| 273 | return True;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | if (*pcount & ~((SMB_OFF_T)0x7fffffff)) {
|
---|
| 277 | /* 32 bit NFS file system, retry with smaller offset */
|
---|
| 278 | DEBUG(0,("Count greater than 31 bits - retrying with 31 bit truncated length.\n"));
|
---|
| 279 | errno = 0;
|
---|
| 280 | *pcount &= 0x7fffffff;
|
---|
| 281 | ret = SMB_VFS_GETLOCK(fsp,poffset,pcount,ptype,&pid);
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 | #endif
|
---|
| 285 |
|
---|
| 286 | DEBUG(8,("posix_fcntl_getlock: Lock query call %s\n", ret ? "successful" : "failed"));
|
---|
| 287 | return ret;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /****************************************************************************
|
---|
| 291 | POSIX function to see if a file region is locked. Returns True if the
|
---|
| 292 | region is locked, False otherwise.
|
---|
| 293 | ****************************************************************************/
|
---|
| 294 |
|
---|
| 295 | bool is_posix_locked(files_struct *fsp,
|
---|
| 296 | uint64_t *pu_offset,
|
---|
| 297 | uint64_t *pu_count,
|
---|
| 298 | enum brl_type *plock_type,
|
---|
| 299 | enum brl_flavour lock_flav)
|
---|
| 300 | {
|
---|
| 301 | SMB_OFF_T offset;
|
---|
| 302 | SMB_OFF_T count;
|
---|
| 303 | int posix_lock_type = map_posix_lock_type(fsp,*plock_type);
|
---|
| 304 |
|
---|
| 305 | DEBUG(10,("is_posix_locked: File %s, offset = %.0f, count = %.0f, "
|
---|
| 306 | "type = %s\n", fsp_str_dbg(fsp), (double)*pu_offset,
|
---|
| 307 | (double)*pu_count, posix_lock_type_name(*plock_type)));
|
---|
| 308 |
|
---|
| 309 | /*
|
---|
| 310 | * If the requested lock won't fit in the POSIX range, we will
|
---|
| 311 | * never set it, so presume it is not locked.
|
---|
| 312 | */
|
---|
| 313 |
|
---|
| 314 | if(!posix_lock_in_range(&offset, &count, *pu_offset, *pu_count)) {
|
---|
| 315 | return False;
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | if (!posix_fcntl_getlock(fsp,&offset,&count,&posix_lock_type)) {
|
---|
| 319 | return False;
|
---|
| 320 | }
|
---|
| 321 |
|
---|
| 322 | if (posix_lock_type == F_UNLCK) {
|
---|
| 323 | return False;
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | if (lock_flav == POSIX_LOCK) {
|
---|
| 327 | /* Only POSIX lock queries need to know the details. */
|
---|
| 328 | *pu_offset = (uint64_t)offset;
|
---|
| 329 | *pu_count = (uint64_t)count;
|
---|
| 330 | *plock_type = (posix_lock_type == F_RDLCK) ? READ_LOCK : WRITE_LOCK;
|
---|
| 331 | }
|
---|
| 332 | return True;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | /****************************************************************************
|
---|
| 336 | Next - the functions that deal with in memory database storing representations
|
---|
| 337 | of either Windows CIFS locks or POSIX CIFS locks.
|
---|
| 338 | ****************************************************************************/
|
---|
| 339 |
|
---|
| 340 | /* The key used in the in-memory POSIX databases. */
|
---|
| 341 |
|
---|
| 342 | struct lock_ref_count_key {
|
---|
| 343 | struct file_id id;
|
---|
| 344 | char r;
|
---|
| 345 | };
|
---|
| 346 |
|
---|
| 347 | /*******************************************************************
|
---|
| 348 | Form a static locking key for a dev/inode pair for the lock ref count
|
---|
| 349 | ******************************************************************/
|
---|
| 350 |
|
---|
| 351 | static TDB_DATA locking_ref_count_key_fsp(files_struct *fsp,
|
---|
| 352 | struct lock_ref_count_key *tmp)
|
---|
| 353 | {
|
---|
| 354 | ZERO_STRUCTP(tmp);
|
---|
| 355 | tmp->id = fsp->file_id;
|
---|
| 356 | tmp->r = 'r';
|
---|
| 357 | return make_tdb_data((uint8_t *)tmp, sizeof(*tmp));
|
---|
| 358 | }
|
---|
| 359 |
|
---|
| 360 | /*******************************************************************
|
---|
| 361 | Convenience function to get an fd_array key from an fsp.
|
---|
| 362 | ******************************************************************/
|
---|
| 363 |
|
---|
| 364 | static TDB_DATA fd_array_key_fsp(files_struct *fsp)
|
---|
| 365 | {
|
---|
| 366 | return make_tdb_data((uint8 *)&fsp->file_id, sizeof(fsp->file_id));
|
---|
| 367 | }
|
---|
| 368 |
|
---|
| 369 | /*******************************************************************
|
---|
| 370 | Create the in-memory POSIX lock databases.
|
---|
| 371 | ********************************************************************/
|
---|
| 372 |
|
---|
| 373 | bool posix_locking_init(bool read_only)
|
---|
| 374 | {
|
---|
| 375 | if (posix_pending_close_db != NULL) {
|
---|
| 376 | return true;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | posix_pending_close_db = db_open_rbt(NULL);
|
---|
| 380 |
|
---|
| 381 | if (posix_pending_close_db == NULL) {
|
---|
| 382 | DEBUG(0,("Failed to open POSIX pending close database.\n"));
|
---|
| 383 | return false;
|
---|
| 384 | }
|
---|
| 385 |
|
---|
| 386 | return true;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | /*******************************************************************
|
---|
| 390 | Delete the in-memory POSIX lock databases.
|
---|
| 391 | ********************************************************************/
|
---|
| 392 |
|
---|
| 393 | bool posix_locking_end(void)
|
---|
| 394 | {
|
---|
| 395 | /*
|
---|
| 396 | * Shouldn't we close all fd's here?
|
---|
| 397 | */
|
---|
| 398 | TALLOC_FREE(posix_pending_close_db);
|
---|
| 399 | return true;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | /****************************************************************************
|
---|
| 403 | Next - the functions that deal with storing fd's that have outstanding
|
---|
| 404 | POSIX locks when closed.
|
---|
| 405 | ****************************************************************************/
|
---|
| 406 |
|
---|
| 407 | /****************************************************************************
|
---|
| 408 | The records in posix_pending_close_tdb are composed of an array of ints
|
---|
| 409 | keyed by dev/ino pair.
|
---|
| 410 | The first int is a reference count of the number of outstanding locks on
|
---|
| 411 | all open fd's on this dev/ino pair. Any subsequent ints are the fd's that
|
---|
| 412 | were open on this dev/ino pair that should have been closed, but can't as
|
---|
| 413 | the lock ref count is non zero.
|
---|
| 414 | ****************************************************************************/
|
---|
| 415 |
|
---|
| 416 | /****************************************************************************
|
---|
| 417 | Keep a reference count of the number of Windows locks open on this dev/ino
|
---|
| 418 | pair. Creates entry if it doesn't exist.
|
---|
| 419 | ****************************************************************************/
|
---|
| 420 |
|
---|
| 421 | static void increment_windows_lock_ref_count(files_struct *fsp)
|
---|
| 422 | {
|
---|
| 423 | struct lock_ref_count_key tmp;
|
---|
| 424 | struct db_record *rec;
|
---|
| 425 | int lock_ref_count = 0;
|
---|
| 426 | NTSTATUS status;
|
---|
| 427 |
|
---|
| 428 | rec = posix_pending_close_db->fetch_locked(
|
---|
| 429 | posix_pending_close_db, talloc_tos(),
|
---|
| 430 | locking_ref_count_key_fsp(fsp, &tmp));
|
---|
| 431 |
|
---|
| 432 | SMB_ASSERT(rec != NULL);
|
---|
| 433 |
|
---|
| 434 | if (rec->value.dptr != NULL) {
|
---|
| 435 | SMB_ASSERT(rec->value.dsize == sizeof(lock_ref_count));
|
---|
| 436 | memcpy(&lock_ref_count, rec->value.dptr,
|
---|
| 437 | sizeof(lock_ref_count));
|
---|
| 438 | }
|
---|
| 439 |
|
---|
| 440 | lock_ref_count++;
|
---|
| 441 |
|
---|
| 442 | status = rec->store(rec, make_tdb_data((uint8 *)&lock_ref_count,
|
---|
| 443 | sizeof(lock_ref_count)), 0);
|
---|
| 444 |
|
---|
| 445 | SMB_ASSERT(NT_STATUS_IS_OK(status));
|
---|
| 446 |
|
---|
| 447 | TALLOC_FREE(rec);
|
---|
| 448 |
|
---|
| 449 | DEBUG(10,("increment_windows_lock_ref_count for file now %s = %d\n",
|
---|
| 450 | fsp_str_dbg(fsp), lock_ref_count));
|
---|
| 451 | }
|
---|
| 452 |
|
---|
| 453 | /****************************************************************************
|
---|
| 454 | Bulk delete - subtract as many locks as we've just deleted.
|
---|
| 455 | ****************************************************************************/
|
---|
| 456 |
|
---|
| 457 | void reduce_windows_lock_ref_count(files_struct *fsp, unsigned int dcount)
|
---|
| 458 | {
|
---|
| 459 | struct lock_ref_count_key tmp;
|
---|
| 460 | struct db_record *rec;
|
---|
| 461 | int lock_ref_count = 0;
|
---|
| 462 | NTSTATUS status;
|
---|
| 463 |
|
---|
| 464 | rec = posix_pending_close_db->fetch_locked(
|
---|
| 465 | posix_pending_close_db, talloc_tos(),
|
---|
| 466 | locking_ref_count_key_fsp(fsp, &tmp));
|
---|
| 467 |
|
---|
| 468 | SMB_ASSERT((rec != NULL)
|
---|
| 469 | && (rec->value.dptr != NULL)
|
---|
| 470 | && (rec->value.dsize == sizeof(lock_ref_count)));
|
---|
| 471 |
|
---|
| 472 | memcpy(&lock_ref_count, rec->value.dptr, sizeof(lock_ref_count));
|
---|
| 473 |
|
---|
| 474 | SMB_ASSERT(lock_ref_count > 0);
|
---|
| 475 |
|
---|
| 476 | lock_ref_count -= dcount;
|
---|
| 477 |
|
---|
| 478 | status = rec->store(rec, make_tdb_data((uint8 *)&lock_ref_count,
|
---|
| 479 | sizeof(lock_ref_count)), 0);
|
---|
| 480 |
|
---|
| 481 | SMB_ASSERT(NT_STATUS_IS_OK(status));
|
---|
| 482 |
|
---|
| 483 | TALLOC_FREE(rec);
|
---|
| 484 |
|
---|
| 485 | DEBUG(10,("reduce_windows_lock_ref_count for file now %s = %d\n",
|
---|
| 486 | fsp_str_dbg(fsp), lock_ref_count));
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | static void decrement_windows_lock_ref_count(files_struct *fsp)
|
---|
| 490 | {
|
---|
| 491 | reduce_windows_lock_ref_count(fsp, 1);
|
---|
| 492 | }
|
---|
| 493 |
|
---|
| 494 | /****************************************************************************
|
---|
| 495 | Fetch the lock ref count.
|
---|
| 496 | ****************************************************************************/
|
---|
| 497 |
|
---|
| 498 | static int get_windows_lock_ref_count(files_struct *fsp)
|
---|
| 499 | {
|
---|
| 500 | struct lock_ref_count_key tmp;
|
---|
| 501 | TDB_DATA dbuf;
|
---|
| 502 | int res;
|
---|
| 503 | int lock_ref_count = 0;
|
---|
| 504 |
|
---|
| 505 | res = posix_pending_close_db->fetch(
|
---|
| 506 | posix_pending_close_db, talloc_tos(),
|
---|
| 507 | locking_ref_count_key_fsp(fsp, &tmp), &dbuf);
|
---|
| 508 |
|
---|
| 509 | SMB_ASSERT(res == 0);
|
---|
| 510 |
|
---|
| 511 | if (dbuf.dsize != 0) {
|
---|
| 512 | SMB_ASSERT(dbuf.dsize == sizeof(lock_ref_count));
|
---|
| 513 | memcpy(&lock_ref_count, dbuf.dptr, sizeof(lock_ref_count));
|
---|
| 514 | TALLOC_FREE(dbuf.dptr);
|
---|
| 515 | }
|
---|
| 516 |
|
---|
| 517 | DEBUG(10,("get_windows_lock_count for file %s = %d\n",
|
---|
| 518 | fsp_str_dbg(fsp), lock_ref_count));
|
---|
| 519 |
|
---|
| 520 | return lock_ref_count;
|
---|
| 521 | }
|
---|
| 522 |
|
---|
| 523 | /****************************************************************************
|
---|
| 524 | Delete a lock_ref_count entry.
|
---|
| 525 | ****************************************************************************/
|
---|
| 526 |
|
---|
| 527 | static void delete_windows_lock_ref_count(files_struct *fsp)
|
---|
| 528 | {
|
---|
| 529 | struct lock_ref_count_key tmp;
|
---|
| 530 | struct db_record *rec;
|
---|
| 531 |
|
---|
| 532 | rec = posix_pending_close_db->fetch_locked(
|
---|
| 533 | posix_pending_close_db, talloc_tos(),
|
---|
| 534 | locking_ref_count_key_fsp(fsp, &tmp));
|
---|
| 535 |
|
---|
| 536 | SMB_ASSERT(rec != NULL);
|
---|
| 537 |
|
---|
| 538 | /* Not a bug if it doesn't exist - no locks were ever granted. */
|
---|
| 539 |
|
---|
| 540 | rec->delete_rec(rec);
|
---|
| 541 | TALLOC_FREE(rec);
|
---|
| 542 |
|
---|
| 543 | DEBUG(10,("delete_windows_lock_ref_count for file %s\n",
|
---|
| 544 | fsp_str_dbg(fsp)));
|
---|
| 545 | }
|
---|
| 546 |
|
---|
| 547 | /****************************************************************************
|
---|
| 548 | Add an fd to the pending close tdb.
|
---|
| 549 | ****************************************************************************/
|
---|
| 550 |
|
---|
| 551 | static void add_fd_to_close_entry(files_struct *fsp)
|
---|
| 552 | {
|
---|
| 553 | struct db_record *rec;
|
---|
| 554 | uint8_t *new_data;
|
---|
| 555 | NTSTATUS status;
|
---|
| 556 |
|
---|
| 557 | rec = posix_pending_close_db->fetch_locked(
|
---|
| 558 | posix_pending_close_db, talloc_tos(),
|
---|
| 559 | fd_array_key_fsp(fsp));
|
---|
| 560 |
|
---|
| 561 | SMB_ASSERT(rec != NULL);
|
---|
| 562 |
|
---|
| 563 | new_data = TALLOC_ARRAY(
|
---|
| 564 | rec, uint8_t, rec->value.dsize + sizeof(fsp->fh->fd));
|
---|
| 565 |
|
---|
| 566 | SMB_ASSERT(new_data != NULL);
|
---|
| 567 |
|
---|
| 568 | memcpy(new_data, rec->value.dptr, rec->value.dsize);
|
---|
| 569 | memcpy(new_data + rec->value.dsize,
|
---|
| 570 | &fsp->fh->fd, sizeof(fsp->fh->fd));
|
---|
| 571 |
|
---|
| 572 | status = rec->store(
|
---|
| 573 | rec, make_tdb_data(new_data,
|
---|
| 574 | rec->value.dsize + sizeof(fsp->fh->fd)), 0);
|
---|
| 575 |
|
---|
| 576 | SMB_ASSERT(NT_STATUS_IS_OK(status));
|
---|
| 577 |
|
---|
| 578 | TALLOC_FREE(rec);
|
---|
| 579 |
|
---|
| 580 | DEBUG(10,("add_fd_to_close_entry: added fd %d file %s\n",
|
---|
| 581 | fsp->fh->fd, fsp_str_dbg(fsp)));
|
---|
| 582 | }
|
---|
| 583 |
|
---|
| 584 | /****************************************************************************
|
---|
| 585 | Remove all fd entries for a specific dev/inode pair from the tdb.
|
---|
| 586 | ****************************************************************************/
|
---|
| 587 |
|
---|
| 588 | static void delete_close_entries(files_struct *fsp)
|
---|
| 589 | {
|
---|
| 590 | struct db_record *rec;
|
---|
| 591 |
|
---|
| 592 | rec = posix_pending_close_db->fetch_locked(
|
---|
| 593 | posix_pending_close_db, talloc_tos(),
|
---|
| 594 | fd_array_key_fsp(fsp));
|
---|
| 595 |
|
---|
| 596 | SMB_ASSERT(rec != NULL);
|
---|
| 597 | rec->delete_rec(rec);
|
---|
| 598 | TALLOC_FREE(rec);
|
---|
| 599 | }
|
---|
| 600 |
|
---|
| 601 | /****************************************************************************
|
---|
| 602 | Get the array of POSIX pending close records for an open fsp. Returns number
|
---|
| 603 | of entries.
|
---|
| 604 | ****************************************************************************/
|
---|
| 605 |
|
---|
| 606 | static size_t get_posix_pending_close_entries(TALLOC_CTX *mem_ctx,
|
---|
| 607 | files_struct *fsp, int **entries)
|
---|
| 608 | {
|
---|
| 609 | TDB_DATA dbuf;
|
---|
| 610 | int res;
|
---|
| 611 |
|
---|
| 612 | res = posix_pending_close_db->fetch(
|
---|
| 613 | posix_pending_close_db, mem_ctx, fd_array_key_fsp(fsp),
|
---|
| 614 | &dbuf);
|
---|
| 615 |
|
---|
| 616 | SMB_ASSERT(res == 0);
|
---|
| 617 |
|
---|
| 618 | if (dbuf.dsize == 0) {
|
---|
| 619 | *entries = NULL;
|
---|
| 620 | return 0;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | *entries = (int *)dbuf.dptr;
|
---|
| 624 | return (size_t)(dbuf.dsize / sizeof(int));
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | /****************************************************************************
|
---|
| 628 | Deal with pending closes needed by POSIX locking support.
|
---|
| 629 | Note that posix_locking_close_file() is expected to have been called
|
---|
| 630 | to delete all locks on this fsp before this function is called.
|
---|
| 631 | ****************************************************************************/
|
---|
| 632 |
|
---|
| 633 | int fd_close_posix(struct files_struct *fsp)
|
---|
| 634 | {
|
---|
| 635 | int saved_errno = 0;
|
---|
| 636 | int ret;
|
---|
| 637 | int *fd_array = NULL;
|
---|
| 638 | size_t count, i;
|
---|
| 639 |
|
---|
| 640 | if (!lp_locking(fsp->conn->params) ||
|
---|
| 641 | !lp_posix_locking(fsp->conn->params))
|
---|
| 642 | {
|
---|
| 643 | /*
|
---|
| 644 | * No locking or POSIX to worry about or we want POSIX semantics
|
---|
| 645 | * which will lose all locks on all fd's open on this dev/inode,
|
---|
| 646 | * just close.
|
---|
| 647 | */
|
---|
| 648 | return close(fsp->fh->fd);
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | if (get_windows_lock_ref_count(fsp)) {
|
---|
| 652 |
|
---|
| 653 | /*
|
---|
| 654 | * There are outstanding locks on this dev/inode pair on
|
---|
| 655 | * other fds. Add our fd to the pending close tdb and set
|
---|
| 656 | * fsp->fh->fd to -1.
|
---|
| 657 | */
|
---|
| 658 |
|
---|
| 659 | add_fd_to_close_entry(fsp);
|
---|
| 660 | return 0;
|
---|
| 661 | }
|
---|
| 662 |
|
---|
| 663 | /*
|
---|
| 664 | * No outstanding locks. Get the pending close fd's
|
---|
| 665 | * from the tdb and close them all.
|
---|
| 666 | */
|
---|
| 667 |
|
---|
| 668 | count = get_posix_pending_close_entries(talloc_tos(), fsp, &fd_array);
|
---|
| 669 |
|
---|
| 670 | if (count) {
|
---|
| 671 | DEBUG(10,("fd_close_posix: doing close on %u fd's.\n",
|
---|
| 672 | (unsigned int)count));
|
---|
| 673 |
|
---|
| 674 | for(i = 0; i < count; i++) {
|
---|
| 675 | if (close(fd_array[i]) == -1) {
|
---|
| 676 | saved_errno = errno;
|
---|
| 677 | }
|
---|
| 678 | }
|
---|
| 679 |
|
---|
| 680 | /*
|
---|
| 681 | * Delete all fd's stored in the tdb
|
---|
| 682 | * for this dev/inode pair.
|
---|
| 683 | */
|
---|
| 684 |
|
---|
| 685 | delete_close_entries(fsp);
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | TALLOC_FREE(fd_array);
|
---|
| 689 |
|
---|
| 690 | /* Don't need a lock ref count on this dev/ino anymore. */
|
---|
| 691 | delete_windows_lock_ref_count(fsp);
|
---|
| 692 |
|
---|
| 693 | /*
|
---|
| 694 | * Finally close the fd associated with this fsp.
|
---|
| 695 | */
|
---|
| 696 |
|
---|
| 697 | ret = close(fsp->fh->fd);
|
---|
| 698 |
|
---|
| 699 | if (ret == 0 && saved_errno != 0) {
|
---|
| 700 | errno = saved_errno;
|
---|
| 701 | ret = -1;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | return ret;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | /****************************************************************************
|
---|
| 708 | Next - the functions that deal with the mapping CIFS Windows locks onto
|
---|
| 709 | the underlying system POSIX locks.
|
---|
| 710 | ****************************************************************************/
|
---|
| 711 |
|
---|
| 712 | /*
|
---|
| 713 | * Structure used when splitting a lock range
|
---|
| 714 | * into a POSIX lock range. Doubly linked list.
|
---|
| 715 | */
|
---|
| 716 |
|
---|
| 717 | struct lock_list {
|
---|
| 718 | struct lock_list *next;
|
---|
| 719 | struct lock_list *prev;
|
---|
| 720 | SMB_OFF_T start;
|
---|
| 721 | SMB_OFF_T size;
|
---|
| 722 | };
|
---|
| 723 |
|
---|
| 724 | /****************************************************************************
|
---|
| 725 | Create a list of lock ranges that don't overlap a given range. Used in calculating
|
---|
| 726 | POSIX locks and unlocks. This is a difficult function that requires ASCII art to
|
---|
| 727 | understand it :-).
|
---|
| 728 | ****************************************************************************/
|
---|
| 729 |
|
---|
| 730 | static struct lock_list *posix_lock_list(TALLOC_CTX *ctx,
|
---|
| 731 | struct lock_list *lhead,
|
---|
| 732 | const struct lock_context *lock_ctx, /* Lock context lhead belongs to. */
|
---|
| 733 | files_struct *fsp,
|
---|
| 734 | const struct lock_struct *plocks,
|
---|
| 735 | int num_locks)
|
---|
| 736 | {
|
---|
| 737 | int i;
|
---|
| 738 |
|
---|
| 739 | /*
|
---|
| 740 | * Check the current lock list on this dev/inode pair.
|
---|
| 741 | * Quit if the list is deleted.
|
---|
| 742 | */
|
---|
| 743 |
|
---|
| 744 | DEBUG(10,("posix_lock_list: curr: start=%.0f,size=%.0f\n",
|
---|
| 745 | (double)lhead->start, (double)lhead->size ));
|
---|
| 746 |
|
---|
| 747 | for (i=0; i<num_locks && lhead; i++) {
|
---|
| 748 | const struct lock_struct *lock = &plocks[i];
|
---|
| 749 | struct lock_list *l_curr;
|
---|
| 750 |
|
---|
| 751 | /* Ignore all but read/write locks. */
|
---|
| 752 | if (lock->lock_type != READ_LOCK && lock->lock_type != WRITE_LOCK) {
|
---|
| 753 | continue;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | /* Ignore locks not owned by this process. */
|
---|
| 757 | if (!procid_equal(&lock->context.pid, &lock_ctx->pid)) {
|
---|
| 758 | continue;
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | /*
|
---|
| 762 | * Walk the lock list, checking for overlaps. Note that
|
---|
| 763 | * the lock list can expand within this loop if the current
|
---|
| 764 | * range being examined needs to be split.
|
---|
| 765 | */
|
---|
| 766 |
|
---|
| 767 | for (l_curr = lhead; l_curr;) {
|
---|
| 768 |
|
---|
| 769 | DEBUG(10,("posix_lock_list: lock: fnum=%d: start=%.0f,size=%.0f:type=%s", lock->fnum,
|
---|
| 770 | (double)lock->start, (double)lock->size, posix_lock_type_name(lock->lock_type) ));
|
---|
| 771 |
|
---|
| 772 | if ( (l_curr->start >= (lock->start + lock->size)) ||
|
---|
| 773 | (lock->start >= (l_curr->start + l_curr->size))) {
|
---|
| 774 |
|
---|
| 775 | /* No overlap with existing lock - leave this range alone. */
|
---|
| 776 | /*********************************************
|
---|
| 777 | +---------+
|
---|
| 778 | | l_curr |
|
---|
| 779 | +---------+
|
---|
| 780 | +-------+
|
---|
| 781 | | lock |
|
---|
| 782 | +-------+
|
---|
| 783 | OR....
|
---|
| 784 | +---------+
|
---|
| 785 | | l_curr |
|
---|
| 786 | +---------+
|
---|
| 787 | **********************************************/
|
---|
| 788 |
|
---|
| 789 | DEBUG(10,(" no overlap case.\n" ));
|
---|
| 790 |
|
---|
| 791 | l_curr = l_curr->next;
|
---|
| 792 |
|
---|
| 793 | } else if ( (l_curr->start >= lock->start) &&
|
---|
| 794 | (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
|
---|
| 795 |
|
---|
| 796 | /*
|
---|
| 797 | * This range is completely overlapped by this existing lock range
|
---|
| 798 | * and thus should have no effect. Delete it from the list.
|
---|
| 799 | */
|
---|
| 800 | /*********************************************
|
---|
| 801 | +---------+
|
---|
| 802 | | l_curr |
|
---|
| 803 | +---------+
|
---|
| 804 | +---------------------------+
|
---|
| 805 | | lock |
|
---|
| 806 | +---------------------------+
|
---|
| 807 | **********************************************/
|
---|
| 808 | /* Save the next pointer */
|
---|
| 809 | struct lock_list *ul_next = l_curr->next;
|
---|
| 810 |
|
---|
| 811 | DEBUG(10,(" delete case.\n" ));
|
---|
| 812 |
|
---|
| 813 | DLIST_REMOVE(lhead, l_curr);
|
---|
| 814 | if(lhead == NULL) {
|
---|
| 815 | break; /* No more list... */
|
---|
| 816 | }
|
---|
| 817 |
|
---|
| 818 | l_curr = ul_next;
|
---|
| 819 |
|
---|
| 820 | } else if ( (l_curr->start >= lock->start) &&
|
---|
| 821 | (l_curr->start < lock->start + lock->size) &&
|
---|
| 822 | (l_curr->start + l_curr->size > lock->start + lock->size) ) {
|
---|
| 823 |
|
---|
| 824 | /*
|
---|
| 825 | * This range overlaps the existing lock range at the high end.
|
---|
| 826 | * Truncate by moving start to existing range end and reducing size.
|
---|
| 827 | */
|
---|
| 828 | /*********************************************
|
---|
| 829 | +---------------+
|
---|
| 830 | | l_curr |
|
---|
| 831 | +---------------+
|
---|
| 832 | +---------------+
|
---|
| 833 | | lock |
|
---|
| 834 | +---------------+
|
---|
| 835 | BECOMES....
|
---|
| 836 | +-------+
|
---|
| 837 | | l_curr|
|
---|
| 838 | +-------+
|
---|
| 839 | **********************************************/
|
---|
| 840 |
|
---|
| 841 | l_curr->size = (l_curr->start + l_curr->size) - (lock->start + lock->size);
|
---|
| 842 | l_curr->start = lock->start + lock->size;
|
---|
| 843 |
|
---|
| 844 | DEBUG(10,(" truncate high case: start=%.0f,size=%.0f\n",
|
---|
| 845 | (double)l_curr->start, (double)l_curr->size ));
|
---|
| 846 |
|
---|
| 847 | l_curr = l_curr->next;
|
---|
| 848 |
|
---|
| 849 | } else if ( (l_curr->start < lock->start) &&
|
---|
| 850 | (l_curr->start + l_curr->size > lock->start) &&
|
---|
| 851 | (l_curr->start + l_curr->size <= lock->start + lock->size) ) {
|
---|
| 852 |
|
---|
| 853 | /*
|
---|
| 854 | * This range overlaps the existing lock range at the low end.
|
---|
| 855 | * Truncate by reducing size.
|
---|
| 856 | */
|
---|
| 857 | /*********************************************
|
---|
| 858 | +---------------+
|
---|
| 859 | | l_curr |
|
---|
| 860 | +---------------+
|
---|
| 861 | +---------------+
|
---|
| 862 | | lock |
|
---|
| 863 | +---------------+
|
---|
| 864 | BECOMES....
|
---|
| 865 | +-------+
|
---|
| 866 | | l_curr|
|
---|
| 867 | +-------+
|
---|
| 868 | **********************************************/
|
---|
| 869 |
|
---|
| 870 | l_curr->size = lock->start - l_curr->start;
|
---|
| 871 |
|
---|
| 872 | DEBUG(10,(" truncate low case: start=%.0f,size=%.0f\n",
|
---|
| 873 | (double)l_curr->start, (double)l_curr->size ));
|
---|
| 874 |
|
---|
| 875 | l_curr = l_curr->next;
|
---|
| 876 |
|
---|
| 877 | } else if ( (l_curr->start < lock->start) &&
|
---|
| 878 | (l_curr->start + l_curr->size > lock->start + lock->size) ) {
|
---|
| 879 | /*
|
---|
| 880 | * Worst case scenario. Range completely overlaps an existing
|
---|
| 881 | * lock range. Split the request into two, push the new (upper) request
|
---|
| 882 | * into the dlink list, and continue with the entry after l_new (as we
|
---|
| 883 | * know that l_new will not overlap with this lock).
|
---|
| 884 | */
|
---|
| 885 | /*********************************************
|
---|
| 886 | +---------------------------+
|
---|
| 887 | | l_curr |
|
---|
| 888 | +---------------------------+
|
---|
| 889 | +---------+
|
---|
| 890 | | lock |
|
---|
| 891 | +---------+
|
---|
| 892 | BECOMES.....
|
---|
| 893 | +-------+ +---------+
|
---|
| 894 | | l_curr| | l_new |
|
---|
| 895 | +-------+ +---------+
|
---|
| 896 | **********************************************/
|
---|
| 897 | struct lock_list *l_new = TALLOC_P(ctx, struct lock_list);
|
---|
| 898 |
|
---|
| 899 | if(l_new == NULL) {
|
---|
| 900 | DEBUG(0,("posix_lock_list: talloc fail.\n"));
|
---|
| 901 | return NULL; /* The talloc_destroy takes care of cleanup. */
|
---|
| 902 | }
|
---|
| 903 |
|
---|
| 904 | ZERO_STRUCTP(l_new);
|
---|
| 905 | l_new->start = lock->start + lock->size;
|
---|
| 906 | l_new->size = l_curr->start + l_curr->size - l_new->start;
|
---|
| 907 |
|
---|
| 908 | /* Truncate the l_curr. */
|
---|
| 909 | l_curr->size = lock->start - l_curr->start;
|
---|
| 910 |
|
---|
| 911 | DEBUG(10,(" split case: curr: start=%.0f,size=%.0f \
|
---|
| 912 | new: start=%.0f,size=%.0f\n", (double)l_curr->start, (double)l_curr->size,
|
---|
| 913 | (double)l_new->start, (double)l_new->size ));
|
---|
| 914 |
|
---|
| 915 | /*
|
---|
| 916 | * Add into the dlink list after the l_curr point - NOT at lhead.
|
---|
| 917 | */
|
---|
| 918 | DLIST_ADD_AFTER(lhead, l_new, l_curr);
|
---|
| 919 |
|
---|
| 920 | /* And move after the link we added. */
|
---|
| 921 | l_curr = l_new->next;
|
---|
| 922 |
|
---|
| 923 | } else {
|
---|
| 924 |
|
---|
| 925 | /*
|
---|
| 926 | * This logic case should never happen. Ensure this is the
|
---|
| 927 | * case by forcing an abort.... Remove in production.
|
---|
| 928 | */
|
---|
| 929 | char *msg = NULL;
|
---|
| 930 |
|
---|
| 931 | if (asprintf(&msg, "logic flaw in cases: l_curr: start = %.0f, size = %.0f : \
|
---|
| 932 | lock: start = %.0f, size = %.0f", (double)l_curr->start, (double)l_curr->size, (double)lock->start, (double)lock->size ) != -1) {
|
---|
| 933 | smb_panic(msg);
|
---|
| 934 | } else {
|
---|
| 935 | smb_panic("posix_lock_list");
|
---|
| 936 | }
|
---|
| 937 | }
|
---|
| 938 | } /* end for ( l_curr = lhead; l_curr;) */
|
---|
| 939 | } /* end for (i=0; i<num_locks && ul_head; i++) */
|
---|
| 940 |
|
---|
| 941 | return lhead;
|
---|
| 942 | }
|
---|
| 943 |
|
---|
| 944 | /****************************************************************************
|
---|
| 945 | POSIX function to acquire a lock. Returns True if the
|
---|
| 946 | lock could be granted, False if not.
|
---|
| 947 | ****************************************************************************/
|
---|
| 948 |
|
---|
| 949 | bool set_posix_lock_windows_flavour(files_struct *fsp,
|
---|
| 950 | uint64_t u_offset,
|
---|
| 951 | uint64_t u_count,
|
---|
| 952 | enum brl_type lock_type,
|
---|
| 953 | const struct lock_context *lock_ctx,
|
---|
| 954 | const struct lock_struct *plocks,
|
---|
| 955 | int num_locks,
|
---|
| 956 | int *errno_ret)
|
---|
| 957 | {
|
---|
| 958 | SMB_OFF_T offset;
|
---|
| 959 | SMB_OFF_T count;
|
---|
| 960 | int posix_lock_type = map_posix_lock_type(fsp,lock_type);
|
---|
| 961 | bool ret = True;
|
---|
| 962 | size_t lock_count;
|
---|
| 963 | TALLOC_CTX *l_ctx = NULL;
|
---|
| 964 | struct lock_list *llist = NULL;
|
---|
| 965 | struct lock_list *ll = NULL;
|
---|
| 966 |
|
---|
| 967 | DEBUG(5,("set_posix_lock_windows_flavour: File %s, offset = %.0f, "
|
---|
| 968 | "count = %.0f, type = %s\n", fsp_str_dbg(fsp),
|
---|
| 969 | (double)u_offset, (double)u_count,
|
---|
| 970 | posix_lock_type_name(lock_type)));
|
---|
| 971 |
|
---|
| 972 | /*
|
---|
| 973 | * If the requested lock won't fit in the POSIX range, we will
|
---|
| 974 | * pretend it was successful.
|
---|
| 975 | */
|
---|
| 976 |
|
---|
| 977 | if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
|
---|
| 978 | increment_windows_lock_ref_count(fsp);
|
---|
| 979 | return True;
|
---|
| 980 | }
|
---|
| 981 |
|
---|
| 982 | /*
|
---|
| 983 | * Windows is very strange. It allows read locks to be overlayed
|
---|
| 984 | * (even over a write lock), but leaves the write lock in force until the first
|
---|
| 985 | * unlock. It also reference counts the locks. This means the following sequence :
|
---|
| 986 | *
|
---|
| 987 | * process1 process2
|
---|
| 988 | * ------------------------------------------------------------------------
|
---|
| 989 | * WRITE LOCK : start = 2, len = 10
|
---|
| 990 | * READ LOCK: start =0, len = 10 - FAIL
|
---|
| 991 | * READ LOCK : start = 0, len = 14
|
---|
| 992 | * READ LOCK: start =0, len = 10 - FAIL
|
---|
| 993 | * UNLOCK : start = 2, len = 10
|
---|
| 994 | * READ LOCK: start =0, len = 10 - OK
|
---|
| 995 | *
|
---|
| 996 | * Under POSIX, the same sequence in steps 1 and 2 would not be reference counted, but
|
---|
| 997 | * would leave a single read lock over the 0-14 region.
|
---|
| 998 | */
|
---|
| 999 |
|
---|
| 1000 | if ((l_ctx = talloc_init("set_posix_lock")) == NULL) {
|
---|
| 1001 | DEBUG(0,("set_posix_lock_windows_flavour: unable to init talloc context.\n"));
|
---|
| 1002 | return False;
|
---|
| 1003 | }
|
---|
| 1004 |
|
---|
| 1005 | if ((ll = TALLOC_P(l_ctx, struct lock_list)) == NULL) {
|
---|
| 1006 | DEBUG(0,("set_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
|
---|
| 1007 | talloc_destroy(l_ctx);
|
---|
| 1008 | return False;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | /*
|
---|
| 1012 | * Create the initial list entry containing the
|
---|
| 1013 | * lock we want to add.
|
---|
| 1014 | */
|
---|
| 1015 |
|
---|
| 1016 | ZERO_STRUCTP(ll);
|
---|
| 1017 | ll->start = offset;
|
---|
| 1018 | ll->size = count;
|
---|
| 1019 |
|
---|
| 1020 | DLIST_ADD(llist, ll);
|
---|
| 1021 |
|
---|
| 1022 | /*
|
---|
| 1023 | * The following call calculates if there are any
|
---|
| 1024 | * overlapping locks held by this process on
|
---|
| 1025 | * fd's open on the same file and splits this list
|
---|
| 1026 | * into a list of lock ranges that do not overlap with existing
|
---|
| 1027 | * POSIX locks.
|
---|
| 1028 | */
|
---|
| 1029 |
|
---|
| 1030 | llist = posix_lock_list(l_ctx,
|
---|
| 1031 | llist,
|
---|
| 1032 | lock_ctx, /* Lock context llist belongs to. */
|
---|
| 1033 | fsp,
|
---|
| 1034 | plocks,
|
---|
| 1035 | num_locks);
|
---|
| 1036 |
|
---|
| 1037 | /*
|
---|
| 1038 | * Add the POSIX locks on the list of ranges returned.
|
---|
| 1039 | * As the lock is supposed to be added atomically, we need to
|
---|
| 1040 | * back out all the locks if any one of these calls fail.
|
---|
| 1041 | */
|
---|
| 1042 |
|
---|
| 1043 | for (lock_count = 0, ll = llist; ll; ll = ll->next, lock_count++) {
|
---|
| 1044 | offset = ll->start;
|
---|
| 1045 | count = ll->size;
|
---|
| 1046 |
|
---|
| 1047 | DEBUG(5,("set_posix_lock_windows_flavour: Real lock: Type = %s: offset = %.0f, count = %.0f\n",
|
---|
| 1048 | posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
|
---|
| 1049 |
|
---|
| 1050 | if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
|
---|
| 1051 | *errno_ret = errno;
|
---|
| 1052 | DEBUG(5,("set_posix_lock_windows_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
|
---|
| 1053 | posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
|
---|
| 1054 | ret = False;
|
---|
| 1055 | break;
|
---|
| 1056 | }
|
---|
| 1057 | }
|
---|
| 1058 |
|
---|
| 1059 | if (!ret) {
|
---|
| 1060 |
|
---|
| 1061 | /*
|
---|
| 1062 | * Back out all the POSIX locks we have on fail.
|
---|
| 1063 | */
|
---|
| 1064 |
|
---|
| 1065 | for (ll = llist; lock_count; ll = ll->next, lock_count--) {
|
---|
| 1066 | offset = ll->start;
|
---|
| 1067 | count = ll->size;
|
---|
| 1068 |
|
---|
| 1069 | DEBUG(5,("set_posix_lock_windows_flavour: Backing out locks: Type = %s: offset = %.0f, count = %.0f\n",
|
---|
| 1070 | posix_lock_type_name(posix_lock_type), (double)offset, (double)count ));
|
---|
| 1071 |
|
---|
| 1072 | posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK);
|
---|
| 1073 | }
|
---|
| 1074 | } else {
|
---|
| 1075 | /* Remember the number of Windows locks we have on this dev/ino pair. */
|
---|
| 1076 | increment_windows_lock_ref_count(fsp);
|
---|
| 1077 | }
|
---|
| 1078 |
|
---|
| 1079 | talloc_destroy(l_ctx);
|
---|
| 1080 | return ret;
|
---|
| 1081 | }
|
---|
| 1082 |
|
---|
| 1083 | /****************************************************************************
|
---|
| 1084 | POSIX function to release a lock. Returns True if the
|
---|
| 1085 | lock could be released, False if not.
|
---|
| 1086 | ****************************************************************************/
|
---|
| 1087 |
|
---|
| 1088 | bool release_posix_lock_windows_flavour(files_struct *fsp,
|
---|
| 1089 | uint64_t u_offset,
|
---|
| 1090 | uint64_t u_count,
|
---|
| 1091 | enum brl_type deleted_lock_type,
|
---|
| 1092 | const struct lock_context *lock_ctx,
|
---|
| 1093 | const struct lock_struct *plocks,
|
---|
| 1094 | int num_locks)
|
---|
| 1095 | {
|
---|
| 1096 | SMB_OFF_T offset;
|
---|
| 1097 | SMB_OFF_T count;
|
---|
| 1098 | bool ret = True;
|
---|
| 1099 | TALLOC_CTX *ul_ctx = NULL;
|
---|
| 1100 | struct lock_list *ulist = NULL;
|
---|
| 1101 | struct lock_list *ul = NULL;
|
---|
| 1102 |
|
---|
| 1103 | DEBUG(5,("release_posix_lock_windows_flavour: File %s, offset = %.0f, "
|
---|
| 1104 | "count = %.0f\n", fsp_str_dbg(fsp),
|
---|
| 1105 | (double)u_offset, (double)u_count));
|
---|
| 1106 |
|
---|
| 1107 | /* Remember the number of Windows locks we have on this dev/ino pair. */
|
---|
| 1108 | decrement_windows_lock_ref_count(fsp);
|
---|
| 1109 |
|
---|
| 1110 | /*
|
---|
| 1111 | * If the requested lock won't fit in the POSIX range, we will
|
---|
| 1112 | * pretend it was successful.
|
---|
| 1113 | */
|
---|
| 1114 |
|
---|
| 1115 | if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
|
---|
| 1116 | return True;
|
---|
| 1117 | }
|
---|
| 1118 |
|
---|
| 1119 | if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
|
---|
| 1120 | DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
|
---|
| 1121 | return False;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
|
---|
| 1125 | DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
|
---|
| 1126 | talloc_destroy(ul_ctx);
|
---|
| 1127 | return False;
|
---|
| 1128 | }
|
---|
| 1129 |
|
---|
| 1130 | /*
|
---|
| 1131 | * Create the initial list entry containing the
|
---|
| 1132 | * lock we want to remove.
|
---|
| 1133 | */
|
---|
| 1134 |
|
---|
| 1135 | ZERO_STRUCTP(ul);
|
---|
| 1136 | ul->start = offset;
|
---|
| 1137 | ul->size = count;
|
---|
| 1138 |
|
---|
| 1139 | DLIST_ADD(ulist, ul);
|
---|
| 1140 |
|
---|
| 1141 | /*
|
---|
| 1142 | * The following call calculates if there are any
|
---|
| 1143 | * overlapping locks held by this process on
|
---|
| 1144 | * fd's open on the same file and creates a
|
---|
| 1145 | * list of unlock ranges that will allow
|
---|
| 1146 | * POSIX lock ranges to remain on the file whilst the
|
---|
| 1147 | * unlocks are performed.
|
---|
| 1148 | */
|
---|
| 1149 |
|
---|
| 1150 | ulist = posix_lock_list(ul_ctx,
|
---|
| 1151 | ulist,
|
---|
| 1152 | lock_ctx, /* Lock context ulist belongs to. */
|
---|
| 1153 | fsp,
|
---|
| 1154 | plocks,
|
---|
| 1155 | num_locks);
|
---|
| 1156 |
|
---|
| 1157 | /*
|
---|
| 1158 | * If there were any overlapped entries (list is > 1 or size or start have changed),
|
---|
| 1159 | * and the lock_type we just deleted from
|
---|
| 1160 | * the upper layer tdb was a write lock, then before doing the unlock we need to downgrade
|
---|
| 1161 | * the POSIX lock to a read lock. This allows any overlapping read locks
|
---|
| 1162 | * to be atomically maintained.
|
---|
| 1163 | */
|
---|
| 1164 |
|
---|
| 1165 | if (deleted_lock_type == WRITE_LOCK &&
|
---|
| 1166 | (!ulist || ulist->next != NULL || ulist->start != offset || ulist->size != count)) {
|
---|
| 1167 |
|
---|
| 1168 | DEBUG(5,("release_posix_lock_windows_flavour: downgrading lock to READ: offset = %.0f, count = %.0f\n",
|
---|
| 1169 | (double)offset, (double)count ));
|
---|
| 1170 |
|
---|
| 1171 | if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_RDLCK)) {
|
---|
| 1172 | DEBUG(0,("release_posix_lock_windows_flavour: downgrade of lock failed with error %s !\n", strerror(errno) ));
|
---|
| 1173 | talloc_destroy(ul_ctx);
|
---|
| 1174 | return False;
|
---|
| 1175 | }
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
| 1178 | /*
|
---|
| 1179 | * Release the POSIX locks on the list of ranges returned.
|
---|
| 1180 | */
|
---|
| 1181 |
|
---|
| 1182 | for(; ulist; ulist = ulist->next) {
|
---|
| 1183 | offset = ulist->start;
|
---|
| 1184 | count = ulist->size;
|
---|
| 1185 |
|
---|
| 1186 | DEBUG(5,("release_posix_lock_windows_flavour: Real unlock: offset = %.0f, count = %.0f\n",
|
---|
| 1187 | (double)offset, (double)count ));
|
---|
| 1188 |
|
---|
| 1189 | if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
|
---|
| 1190 | ret = False;
|
---|
| 1191 | }
|
---|
| 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | talloc_destroy(ul_ctx);
|
---|
| 1195 | return ret;
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 | /****************************************************************************
|
---|
| 1199 | Next - the functions that deal with mapping CIFS POSIX locks onto
|
---|
| 1200 | the underlying system POSIX locks.
|
---|
| 1201 | ****************************************************************************/
|
---|
| 1202 |
|
---|
| 1203 | /****************************************************************************
|
---|
| 1204 | POSIX function to acquire a lock. Returns True if the
|
---|
| 1205 | lock could be granted, False if not.
|
---|
| 1206 | As POSIX locks don't stack or conflict (they just overwrite)
|
---|
| 1207 | we can map the requested lock directly onto a system one. We
|
---|
| 1208 | know it doesn't conflict with locks on other contexts as the
|
---|
| 1209 | upper layer would have refused it.
|
---|
| 1210 | ****************************************************************************/
|
---|
| 1211 |
|
---|
| 1212 | bool set_posix_lock_posix_flavour(files_struct *fsp,
|
---|
| 1213 | uint64_t u_offset,
|
---|
| 1214 | uint64_t u_count,
|
---|
| 1215 | enum brl_type lock_type,
|
---|
| 1216 | int *errno_ret)
|
---|
| 1217 | {
|
---|
| 1218 | SMB_OFF_T offset;
|
---|
| 1219 | SMB_OFF_T count;
|
---|
| 1220 | int posix_lock_type = map_posix_lock_type(fsp,lock_type);
|
---|
| 1221 |
|
---|
| 1222 | DEBUG(5,("set_posix_lock_posix_flavour: File %s, offset = %.0f, count "
|
---|
| 1223 | "= %.0f, type = %s\n", fsp_str_dbg(fsp),
|
---|
| 1224 | (double)u_offset, (double)u_count,
|
---|
| 1225 | posix_lock_type_name(lock_type)));
|
---|
| 1226 |
|
---|
| 1227 | /*
|
---|
| 1228 | * If the requested lock won't fit in the POSIX range, we will
|
---|
| 1229 | * pretend it was successful.
|
---|
| 1230 | */
|
---|
| 1231 |
|
---|
| 1232 | if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
|
---|
| 1233 | return True;
|
---|
| 1234 | }
|
---|
| 1235 |
|
---|
| 1236 | if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,posix_lock_type)) {
|
---|
| 1237 | *errno_ret = errno;
|
---|
| 1238 | DEBUG(5,("set_posix_lock_posix_flavour: Lock fail !: Type = %s: offset = %.0f, count = %.0f. Errno = %s\n",
|
---|
| 1239 | posix_lock_type_name(posix_lock_type), (double)offset, (double)count, strerror(errno) ));
|
---|
| 1240 | return False;
|
---|
| 1241 | }
|
---|
| 1242 | return True;
|
---|
| 1243 | }
|
---|
| 1244 |
|
---|
| 1245 | /****************************************************************************
|
---|
| 1246 | POSIX function to release a lock. Returns True if the
|
---|
| 1247 | lock could be released, False if not.
|
---|
| 1248 | We are given a complete lock state from the upper layer which is what the lock
|
---|
| 1249 | state should be after the unlock has already been done, so what
|
---|
| 1250 | we do is punch out holes in the unlock range where locks owned by this process
|
---|
| 1251 | have a different lock context.
|
---|
| 1252 | ****************************************************************************/
|
---|
| 1253 |
|
---|
| 1254 | bool release_posix_lock_posix_flavour(files_struct *fsp,
|
---|
| 1255 | uint64_t u_offset,
|
---|
| 1256 | uint64_t u_count,
|
---|
| 1257 | const struct lock_context *lock_ctx,
|
---|
| 1258 | const struct lock_struct *plocks,
|
---|
| 1259 | int num_locks)
|
---|
| 1260 | {
|
---|
| 1261 | bool ret = True;
|
---|
| 1262 | SMB_OFF_T offset;
|
---|
| 1263 | SMB_OFF_T count;
|
---|
| 1264 | TALLOC_CTX *ul_ctx = NULL;
|
---|
| 1265 | struct lock_list *ulist = NULL;
|
---|
| 1266 | struct lock_list *ul = NULL;
|
---|
| 1267 |
|
---|
| 1268 | DEBUG(5,("release_posix_lock_posix_flavour: File %s, offset = %.0f, "
|
---|
| 1269 | "count = %.0f\n", fsp_str_dbg(fsp),
|
---|
| 1270 | (double)u_offset, (double)u_count));
|
---|
| 1271 |
|
---|
| 1272 | /*
|
---|
| 1273 | * If the requested lock won't fit in the POSIX range, we will
|
---|
| 1274 | * pretend it was successful.
|
---|
| 1275 | */
|
---|
| 1276 |
|
---|
| 1277 | if(!posix_lock_in_range(&offset, &count, u_offset, u_count)) {
|
---|
| 1278 | return True;
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
| 1281 | if ((ul_ctx = talloc_init("release_posix_lock")) == NULL) {
|
---|
| 1282 | DEBUG(0,("release_posix_lock_windows_flavour: unable to init talloc context.\n"));
|
---|
| 1283 | return False;
|
---|
| 1284 | }
|
---|
| 1285 |
|
---|
| 1286 | if ((ul = TALLOC_P(ul_ctx, struct lock_list)) == NULL) {
|
---|
| 1287 | DEBUG(0,("release_posix_lock_windows_flavour: unable to talloc unlock list.\n"));
|
---|
| 1288 | talloc_destroy(ul_ctx);
|
---|
| 1289 | return False;
|
---|
| 1290 | }
|
---|
| 1291 |
|
---|
| 1292 | /*
|
---|
| 1293 | * Create the initial list entry containing the
|
---|
| 1294 | * lock we want to remove.
|
---|
| 1295 | */
|
---|
| 1296 |
|
---|
| 1297 | ZERO_STRUCTP(ul);
|
---|
| 1298 | ul->start = offset;
|
---|
| 1299 | ul->size = count;
|
---|
| 1300 |
|
---|
| 1301 | DLIST_ADD(ulist, ul);
|
---|
| 1302 |
|
---|
| 1303 | /*
|
---|
| 1304 | * Walk the given array creating a linked list
|
---|
| 1305 | * of unlock requests.
|
---|
| 1306 | */
|
---|
| 1307 |
|
---|
| 1308 | ulist = posix_lock_list(ul_ctx,
|
---|
| 1309 | ulist,
|
---|
| 1310 | lock_ctx, /* Lock context ulist belongs to. */
|
---|
| 1311 | fsp,
|
---|
| 1312 | plocks,
|
---|
| 1313 | num_locks);
|
---|
| 1314 |
|
---|
| 1315 | /*
|
---|
| 1316 | * Release the POSIX locks on the list of ranges returned.
|
---|
| 1317 | */
|
---|
| 1318 |
|
---|
| 1319 | for(; ulist; ulist = ulist->next) {
|
---|
| 1320 | offset = ulist->start;
|
---|
| 1321 | count = ulist->size;
|
---|
| 1322 |
|
---|
| 1323 | DEBUG(5,("release_posix_lock_posix_flavour: Real unlock: offset = %.0f, count = %.0f\n",
|
---|
| 1324 | (double)offset, (double)count ));
|
---|
| 1325 |
|
---|
| 1326 | if (!posix_fcntl_lock(fsp,SMB_F_SETLK,offset,count,F_UNLCK)) {
|
---|
| 1327 | ret = False;
|
---|
| 1328 | }
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 | talloc_destroy(ul_ctx);
|
---|
| 1332 | return ret;
|
---|
| 1333 | }
|
---|