[137] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 | Locking functions
|
---|
| 4 | Copyright (C) Andrew Tridgell 1992-2000
|
---|
| 5 | Copyright (C) Jeremy Allison 1992-2006
|
---|
| 6 | Copyright (C) Volker Lendecke 2005
|
---|
| 7 |
|
---|
| 8 | This program is free software; you can redistribute it and/or modify
|
---|
| 9 | it under the terms of the GNU General Public License as published by
|
---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
---|
| 11 | (at your option) any later version.
|
---|
| 12 |
|
---|
| 13 | This program is distributed in the hope that it will be useful,
|
---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 16 | GNU General Public License for more details.
|
---|
| 17 |
|
---|
| 18 | You should have received a copy of the GNU General Public License
|
---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 20 |
|
---|
| 21 | Revision History:
|
---|
| 22 |
|
---|
| 23 | 12 aug 96: Erik.Devriendt@te6.siemens.be
|
---|
| 24 | added support for shared memory implementation of share mode locking
|
---|
| 25 |
|
---|
| 26 | May 1997. Jeremy Allison (jallison@whistle.com). Modified share mode
|
---|
| 27 | locking to deal with multiple share modes per open file.
|
---|
| 28 |
|
---|
| 29 | September 1997. Jeremy Allison (jallison@whistle.com). Added oplock
|
---|
| 30 | support.
|
---|
| 31 |
|
---|
| 32 | rewrtten completely to use new tdb code. Tridge, Dec '99
|
---|
| 33 |
|
---|
| 34 | Added POSIX locking support. Jeremy Allison (jeremy@valinux.com), Apr. 2000.
|
---|
| 35 | Added Unix Extensions POSIX locking support. Jeremy Allison Mar 2006.
|
---|
| 36 | */
|
---|
| 37 |
|
---|
| 38 | #include "includes.h"
|
---|
| 39 |
|
---|
| 40 | #undef DBGC_CLASS
|
---|
| 41 | #define DBGC_CLASS DBGC_LOCKING
|
---|
| 42 |
|
---|
| 43 | #define NO_LOCKING_COUNT (-1)
|
---|
| 44 |
|
---|
| 45 | /* the locking database handle */
|
---|
| 46 | static struct db_context *lock_db;
|
---|
| 47 |
|
---|
| 48 | /****************************************************************************
|
---|
| 49 | Debugging aids :-).
|
---|
| 50 | ****************************************************************************/
|
---|
| 51 |
|
---|
| 52 | const char *lock_type_name(enum brl_type lock_type)
|
---|
| 53 | {
|
---|
| 54 | switch (lock_type) {
|
---|
| 55 | case READ_LOCK:
|
---|
| 56 | return "READ";
|
---|
| 57 | case WRITE_LOCK:
|
---|
| 58 | return "WRITE";
|
---|
| 59 | case PENDING_READ_LOCK:
|
---|
| 60 | return "PENDING_READ";
|
---|
| 61 | case PENDING_WRITE_LOCK:
|
---|
| 62 | return "PENDING_WRITE";
|
---|
| 63 | default:
|
---|
| 64 | return "other";
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | const char *lock_flav_name(enum brl_flavour lock_flav)
|
---|
| 69 | {
|
---|
| 70 | return (lock_flav == WINDOWS_LOCK) ? "WINDOWS_LOCK" : "POSIX_LOCK";
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | /****************************************************************************
|
---|
| 74 | Utility function called to see if a file region is locked.
|
---|
| 75 | Called in the read/write codepath.
|
---|
| 76 | ****************************************************************************/
|
---|
| 77 |
|
---|
| 78 | bool is_locked(files_struct *fsp,
|
---|
| 79 | uint32 smbpid,
|
---|
| 80 | SMB_BIG_UINT count,
|
---|
| 81 | SMB_BIG_UINT offset,
|
---|
| 82 | enum brl_type lock_type)
|
---|
| 83 | {
|
---|
| 84 | int strict_locking = lp_strict_locking(fsp->conn->params);
|
---|
| 85 | enum brl_flavour lock_flav = lp_posix_cifsu_locktype(fsp);
|
---|
| 86 | bool ret = True;
|
---|
| 87 |
|
---|
| 88 | if (count == 0) {
|
---|
| 89 | return False;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | if (!lp_locking(fsp->conn->params) || !strict_locking) {
|
---|
| 93 | return False;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | if (strict_locking == Auto) {
|
---|
| 97 | if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type) && (lock_type == READ_LOCK || lock_type == WRITE_LOCK)) {
|
---|
| 98 | DEBUG(10,("is_locked: optimisation - exclusive oplock on file %s\n", fsp->fsp_name ));
|
---|
| 99 | ret = False;
|
---|
| 100 | } else if ((fsp->oplock_type == LEVEL_II_OPLOCK) &&
|
---|
| 101 | (lock_type == READ_LOCK)) {
|
---|
| 102 | DEBUG(10,("is_locked: optimisation - level II oplock on file %s\n", fsp->fsp_name ));
|
---|
| 103 | ret = False;
|
---|
| 104 | } else {
|
---|
| 105 | struct byte_range_lock *br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
|
---|
| 106 | if (!br_lck) {
|
---|
| 107 | return False;
|
---|
| 108 | }
|
---|
| 109 | ret = !brl_locktest(br_lck,
|
---|
| 110 | smbpid,
|
---|
| 111 | procid_self(),
|
---|
| 112 | offset,
|
---|
| 113 | count,
|
---|
| 114 | lock_type,
|
---|
| 115 | lock_flav);
|
---|
| 116 | TALLOC_FREE(br_lck);
|
---|
| 117 | }
|
---|
| 118 | } else {
|
---|
| 119 | struct byte_range_lock *br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
|
---|
| 120 | if (!br_lck) {
|
---|
| 121 | return False;
|
---|
| 122 | }
|
---|
| 123 | ret = !brl_locktest(br_lck,
|
---|
| 124 | smbpid,
|
---|
| 125 | procid_self(),
|
---|
| 126 | offset,
|
---|
| 127 | count,
|
---|
| 128 | lock_type,
|
---|
| 129 | lock_flav);
|
---|
| 130 | TALLOC_FREE(br_lck);
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | DEBUG(10,("is_locked: flavour = %s brl start=%.0f len=%.0f %s for fnum %d file %s\n",
|
---|
| 134 | lock_flav_name(lock_flav),
|
---|
| 135 | (double)offset, (double)count, ret ? "locked" : "unlocked",
|
---|
| 136 | fsp->fnum, fsp->fsp_name ));
|
---|
| 137 |
|
---|
| 138 | return ret;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | /****************************************************************************
|
---|
| 142 | Find out if a lock could be granted - return who is blocking us if we can't.
|
---|
| 143 | ****************************************************************************/
|
---|
| 144 |
|
---|
| 145 | NTSTATUS query_lock(files_struct *fsp,
|
---|
| 146 | uint32 *psmbpid,
|
---|
| 147 | SMB_BIG_UINT *pcount,
|
---|
| 148 | SMB_BIG_UINT *poffset,
|
---|
| 149 | enum brl_type *plock_type,
|
---|
| 150 | enum brl_flavour lock_flav)
|
---|
| 151 | {
|
---|
| 152 | struct byte_range_lock *br_lck = NULL;
|
---|
| 153 | NTSTATUS status = NT_STATUS_LOCK_NOT_GRANTED;
|
---|
| 154 |
|
---|
| 155 | if (!fsp->can_lock) {
|
---|
| 156 | return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if (!lp_locking(fsp->conn->params)) {
|
---|
| 160 | return NT_STATUS_OK;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | br_lck = brl_get_locks_readonly(talloc_tos(), fsp);
|
---|
| 164 | if (!br_lck) {
|
---|
| 165 | return NT_STATUS_NO_MEMORY;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | status = brl_lockquery(br_lck,
|
---|
| 169 | psmbpid,
|
---|
| 170 | procid_self(),
|
---|
| 171 | poffset,
|
---|
| 172 | pcount,
|
---|
| 173 | plock_type,
|
---|
| 174 | lock_flav);
|
---|
| 175 |
|
---|
| 176 | TALLOC_FREE(br_lck);
|
---|
| 177 | return status;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /****************************************************************************
|
---|
| 181 | Utility function called by locking requests.
|
---|
| 182 | ****************************************************************************/
|
---|
| 183 |
|
---|
| 184 | struct byte_range_lock *do_lock(struct messaging_context *msg_ctx,
|
---|
| 185 | files_struct *fsp,
|
---|
| 186 | uint32 lock_pid,
|
---|
| 187 | SMB_BIG_UINT count,
|
---|
| 188 | SMB_BIG_UINT offset,
|
---|
| 189 | enum brl_type lock_type,
|
---|
| 190 | enum brl_flavour lock_flav,
|
---|
| 191 | bool blocking_lock,
|
---|
| 192 | NTSTATUS *perr,
|
---|
| 193 | uint32 *plock_pid)
|
---|
| 194 | {
|
---|
| 195 | struct byte_range_lock *br_lck = NULL;
|
---|
| 196 |
|
---|
| 197 | if (!fsp->can_lock) {
|
---|
| 198 | *perr = fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
|
---|
| 199 | return NULL;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | if (!lp_locking(fsp->conn->params)) {
|
---|
| 203 | *perr = NT_STATUS_OK;
|
---|
| 204 | return NULL;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /* NOTE! 0 byte long ranges ARE allowed and should be stored */
|
---|
| 208 |
|
---|
| 209 | DEBUG(10,("do_lock: lock flavour %s lock type %s start=%.0f len=%.0f requested for fnum %d file %s\n",
|
---|
| 210 | lock_flav_name(lock_flav), lock_type_name(lock_type),
|
---|
| 211 | (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
|
---|
| 212 |
|
---|
| 213 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
| 214 | if (!br_lck) {
|
---|
| 215 | *perr = NT_STATUS_NO_MEMORY;
|
---|
| 216 | return NULL;
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | *perr = brl_lock(msg_ctx,
|
---|
| 220 | br_lck,
|
---|
| 221 | lock_pid,
|
---|
| 222 | procid_self(),
|
---|
| 223 | offset,
|
---|
| 224 | count,
|
---|
| 225 | lock_type,
|
---|
| 226 | lock_flav,
|
---|
| 227 | blocking_lock,
|
---|
| 228 | plock_pid);
|
---|
| 229 |
|
---|
| 230 | if (lock_flav == WINDOWS_LOCK &&
|
---|
| 231 | fsp->current_lock_count != NO_LOCKING_COUNT) {
|
---|
| 232 | /* blocking ie. pending, locks also count here,
|
---|
| 233 | * as this is an efficiency counter to avoid checking
|
---|
| 234 | * the lock db. on close. JRA. */
|
---|
| 235 |
|
---|
| 236 | fsp->current_lock_count++;
|
---|
| 237 | } else {
|
---|
| 238 | /* Notice that this has had a POSIX lock request.
|
---|
| 239 | * We can't count locks after this so forget them.
|
---|
| 240 | */
|
---|
| 241 | fsp->current_lock_count = NO_LOCKING_COUNT;
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | return br_lck;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /****************************************************************************
|
---|
| 248 | Utility function called by unlocking requests.
|
---|
| 249 | ****************************************************************************/
|
---|
| 250 |
|
---|
| 251 | NTSTATUS do_unlock(struct messaging_context *msg_ctx,
|
---|
| 252 | files_struct *fsp,
|
---|
| 253 | uint32 lock_pid,
|
---|
| 254 | SMB_BIG_UINT count,
|
---|
| 255 | SMB_BIG_UINT offset,
|
---|
| 256 | enum brl_flavour lock_flav)
|
---|
| 257 | {
|
---|
| 258 | bool ok = False;
|
---|
| 259 | struct byte_range_lock *br_lck = NULL;
|
---|
| 260 |
|
---|
| 261 | if (!fsp->can_lock) {
|
---|
| 262 | return fsp->is_directory ? NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | if (!lp_locking(fsp->conn->params)) {
|
---|
| 266 | return NT_STATUS_OK;
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | DEBUG(10,("do_unlock: unlock start=%.0f len=%.0f requested for fnum %d file %s\n",
|
---|
| 270 | (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
|
---|
| 271 |
|
---|
| 272 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
| 273 | if (!br_lck) {
|
---|
| 274 | return NT_STATUS_NO_MEMORY;
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | ok = brl_unlock(msg_ctx,
|
---|
| 278 | br_lck,
|
---|
| 279 | lock_pid,
|
---|
| 280 | procid_self(),
|
---|
| 281 | offset,
|
---|
| 282 | count,
|
---|
| 283 | lock_flav);
|
---|
| 284 |
|
---|
| 285 | TALLOC_FREE(br_lck);
|
---|
| 286 |
|
---|
| 287 | if (!ok) {
|
---|
| 288 | DEBUG(10,("do_unlock: returning ERRlock.\n" ));
|
---|
| 289 | return NT_STATUS_RANGE_NOT_LOCKED;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | if (lock_flav == WINDOWS_LOCK &&
|
---|
| 293 | fsp->current_lock_count != NO_LOCKING_COUNT) {
|
---|
| 294 | SMB_ASSERT(fsp->current_lock_count > 0);
|
---|
| 295 | fsp->current_lock_count--;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | return NT_STATUS_OK;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
| 301 | /****************************************************************************
|
---|
| 302 | Cancel any pending blocked locks.
|
---|
| 303 | ****************************************************************************/
|
---|
| 304 |
|
---|
| 305 | NTSTATUS do_lock_cancel(files_struct *fsp,
|
---|
| 306 | uint32 lock_pid,
|
---|
| 307 | SMB_BIG_UINT count,
|
---|
| 308 | SMB_BIG_UINT offset,
|
---|
| 309 | enum brl_flavour lock_flav)
|
---|
| 310 | {
|
---|
| 311 | bool ok = False;
|
---|
| 312 | struct byte_range_lock *br_lck = NULL;
|
---|
| 313 |
|
---|
| 314 | if (!fsp->can_lock) {
|
---|
| 315 | return fsp->is_directory ?
|
---|
| 316 | NT_STATUS_INVALID_DEVICE_REQUEST : NT_STATUS_INVALID_HANDLE;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | if (!lp_locking(fsp->conn->params)) {
|
---|
| 320 | return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | DEBUG(10,("do_lock_cancel: cancel start=%.0f len=%.0f requested for fnum %d file %s\n",
|
---|
| 324 | (double)offset, (double)count, fsp->fnum, fsp->fsp_name ));
|
---|
| 325 |
|
---|
| 326 | br_lck = brl_get_locks(talloc_tos(), fsp);
|
---|
| 327 | if (!br_lck) {
|
---|
| 328 | return NT_STATUS_NO_MEMORY;
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | ok = brl_lock_cancel(br_lck,
|
---|
| 332 | lock_pid,
|
---|
| 333 | procid_self(),
|
---|
| 334 | offset,
|
---|
| 335 | count,
|
---|
| 336 | lock_flav);
|
---|
| 337 |
|
---|
| 338 | TALLOC_FREE(br_lck);
|
---|
| 339 |
|
---|
| 340 | if (!ok) {
|
---|
| 341 | DEBUG(10,("do_lock_cancel: returning ERRcancelviolation.\n" ));
|
---|
| 342 | return NT_STATUS_DOS(ERRDOS, ERRcancelviolation);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | if (lock_flav == WINDOWS_LOCK &&
|
---|
| 346 | fsp->current_lock_count != NO_LOCKING_COUNT) {
|
---|
| 347 | SMB_ASSERT(fsp->current_lock_count > 0);
|
---|
| 348 | fsp->current_lock_count--;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | return NT_STATUS_OK;
|
---|
| 352 | }
|
---|
| 353 |
|
---|
| 354 | /****************************************************************************
|
---|
| 355 | Remove any locks on this fd. Called from file_close().
|
---|
| 356 | ****************************************************************************/
|
---|
| 357 |
|
---|
| 358 | void locking_close_file(struct messaging_context *msg_ctx,
|
---|
| 359 | files_struct *fsp)
|
---|
| 360 | {
|
---|
| 361 | struct byte_range_lock *br_lck;
|
---|
| 362 |
|
---|
| 363 | if (!lp_locking(fsp->conn->params)) {
|
---|
| 364 | return;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | /* If we have not outstanding locks or pending
|
---|
| 368 | * locks then we don't need to look in the lock db.
|
---|
| 369 | */
|
---|
| 370 |
|
---|
| 371 | if (fsp->current_lock_count == 0) {
|
---|
| 372 | return;
|
---|
| 373 | }
|
---|
| 374 |
|
---|
| 375 | br_lck = brl_get_locks(talloc_tos(),fsp);
|
---|
| 376 |
|
---|
| 377 | if (br_lck) {
|
---|
| 378 | cancel_pending_lock_requests_by_fid(fsp, br_lck);
|
---|
| 379 | brl_close_fnum(msg_ctx, br_lck);
|
---|
| 380 | TALLOC_FREE(br_lck);
|
---|
| 381 | }
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | /****************************************************************************
|
---|
| 385 | Initialise the locking functions.
|
---|
| 386 | ****************************************************************************/
|
---|
| 387 |
|
---|
| 388 | static bool locking_init_internal(bool read_only)
|
---|
| 389 | {
|
---|
| 390 | brl_init(read_only);
|
---|
| 391 |
|
---|
| 392 | if (lock_db)
|
---|
| 393 | return True;
|
---|
| 394 |
|
---|
| 395 | lock_db = db_open(NULL, lock_path("locking.tdb"),
|
---|
| 396 | lp_open_files_db_hash_size(),
|
---|
| 397 | TDB_DEFAULT|TDB_VOLATILE|TDB_CLEAR_IF_FIRST,
|
---|
| 398 | read_only?O_RDONLY:O_RDWR|O_CREAT, 0644);
|
---|
| 399 |
|
---|
| 400 | if (!lock_db) {
|
---|
| 401 | DEBUG(0,("ERROR: Failed to initialise locking database\n"));
|
---|
| 402 | return False;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | if (!posix_locking_init(read_only))
|
---|
| 406 | return False;
|
---|
| 407 |
|
---|
| 408 | return True;
|
---|
| 409 | }
|
---|
| 410 |
|
---|
| 411 | bool locking_init(void)
|
---|
| 412 | {
|
---|
| 413 | return locking_init_internal(false);
|
---|
| 414 | }
|
---|
| 415 |
|
---|
| 416 | bool locking_init_readonly(void)
|
---|
| 417 | {
|
---|
| 418 | return locking_init_internal(true);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | /*******************************************************************
|
---|
| 422 | Deinitialize the share_mode management.
|
---|
| 423 | ******************************************************************/
|
---|
| 424 |
|
---|
| 425 | bool locking_end(void)
|
---|
| 426 | {
|
---|
| 427 | brl_shutdown();
|
---|
| 428 | TALLOC_FREE(lock_db);
|
---|
| 429 | return true;
|
---|
| 430 | }
|
---|
| 431 |
|
---|
| 432 | /*******************************************************************
|
---|
| 433 | Form a static locking key for a dev/inode pair.
|
---|
| 434 | ******************************************************************/
|
---|
| 435 |
|
---|
| 436 | static TDB_DATA locking_key(const struct file_id *id, struct file_id *tmp)
|
---|
| 437 | {
|
---|
| 438 | *tmp = *id;
|
---|
| 439 | return make_tdb_data((const uint8_t *)tmp, sizeof(*tmp));
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /*******************************************************************
|
---|
| 443 | Print out a share mode.
|
---|
| 444 | ********************************************************************/
|
---|
| 445 |
|
---|
| 446 | char *share_mode_str(TALLOC_CTX *ctx, int num, const struct share_mode_entry *e)
|
---|
| 447 | {
|
---|
| 448 | return talloc_asprintf(ctx, "share_mode_entry[%d]: %s "
|
---|
| 449 | "pid = %s, share_access = 0x%x, private_options = 0x%x, "
|
---|
| 450 | "access_mask = 0x%x, mid = 0x%x, type= 0x%x, gen_id = %lu, "
|
---|
| 451 | "uid = %u, flags = %u, file_id %s",
|
---|
| 452 | num,
|
---|
| 453 | e->op_type == UNUSED_SHARE_MODE_ENTRY ? "UNUSED" : "",
|
---|
| 454 | procid_str_static(&e->pid),
|
---|
| 455 | e->share_access, e->private_options,
|
---|
| 456 | e->access_mask, e->op_mid, e->op_type, e->share_file_id,
|
---|
| 457 | (unsigned int)e->uid, (unsigned int)e->flags,
|
---|
| 458 | file_id_string_tos(&e->id));
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | /*******************************************************************
|
---|
| 462 | Print out a share mode table.
|
---|
| 463 | ********************************************************************/
|
---|
| 464 |
|
---|
| 465 | static void print_share_mode_table(struct locking_data *data)
|
---|
| 466 | {
|
---|
| 467 | int num_share_modes = data->u.s.num_share_mode_entries;
|
---|
| 468 | struct share_mode_entry *shares =
|
---|
| 469 | (struct share_mode_entry *)(data + 1);
|
---|
| 470 | int i;
|
---|
| 471 |
|
---|
| 472 | for (i = 0; i < num_share_modes; i++) {
|
---|
| 473 | struct share_mode_entry entry;
|
---|
| 474 | char *str;
|
---|
| 475 |
|
---|
| 476 | /*
|
---|
| 477 | * We need to memcpy the entry here due to alignment
|
---|
| 478 | * restrictions that are not met when directly accessing
|
---|
| 479 | * shares[i]
|
---|
| 480 | */
|
---|
| 481 |
|
---|
| 482 | memcpy(&entry, &shares[i], sizeof(struct share_mode_entry));
|
---|
| 483 | str = share_mode_str(talloc_tos(), i, &entry);
|
---|
| 484 |
|
---|
| 485 | DEBUG(10,("print_share_mode_table: %s\n", str ? str : ""));
|
---|
| 486 | TALLOC_FREE(str);
|
---|
| 487 | }
|
---|
| 488 | }
|
---|
| 489 |
|
---|
| 490 | /*******************************************************************
|
---|
| 491 | Get all share mode entries for a dev/inode pair.
|
---|
| 492 | ********************************************************************/
|
---|
| 493 |
|
---|
| 494 | static bool parse_share_modes(TDB_DATA dbuf, struct share_mode_lock *lck)
|
---|
| 495 | {
|
---|
[141] | 496 | struct locking_data data;
|
---|
[137] | 497 | int i;
|
---|
| 498 |
|
---|
| 499 | if (dbuf.dsize < sizeof(struct locking_data)) {
|
---|
| 500 | smb_panic("parse_share_modes: buffer too short");
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[141] | 503 | memcpy(&data, dbuf.dptr, sizeof(data));
|
---|
[137] | 504 |
|
---|
[141] | 505 | lck->delete_on_close = data.u.s.delete_on_close;
|
---|
| 506 | lck->old_write_time = data.u.s.old_write_time;
|
---|
| 507 | lck->changed_write_time = data.u.s.changed_write_time;
|
---|
| 508 | lck->num_share_modes = data.u.s.num_share_mode_entries;
|
---|
[137] | 509 |
|
---|
| 510 | DEBUG(10, ("parse_share_modes: delete_on_close: %d, owrt: %s, "
|
---|
| 511 | "cwrt: %s, tok: %u, num_share_modes: %d\n",
|
---|
| 512 | lck->delete_on_close,
|
---|
| 513 | timestring(debug_ctx(),
|
---|
| 514 | convert_timespec_to_time_t(lck->old_write_time)),
|
---|
| 515 | timestring(debug_ctx(),
|
---|
| 516 | convert_timespec_to_time_t(
|
---|
| 517 | lck->changed_write_time)),
|
---|
[141] | 518 | (unsigned int)data.u.s.delete_token_size,
|
---|
[137] | 519 | lck->num_share_modes));
|
---|
| 520 |
|
---|
| 521 | if ((lck->num_share_modes < 0) || (lck->num_share_modes > 1000000)) {
|
---|
| 522 | DEBUG(0, ("invalid number of share modes: %d\n",
|
---|
| 523 | lck->num_share_modes));
|
---|
| 524 | smb_panic("parse_share_modes: invalid number of share modes");
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | lck->share_modes = NULL;
|
---|
| 528 |
|
---|
| 529 | if (lck->num_share_modes != 0) {
|
---|
| 530 |
|
---|
| 531 | if (dbuf.dsize < (sizeof(struct locking_data) +
|
---|
| 532 | (lck->num_share_modes *
|
---|
| 533 | sizeof(struct share_mode_entry)))) {
|
---|
| 534 | smb_panic("parse_share_modes: buffer too short");
|
---|
| 535 | }
|
---|
| 536 |
|
---|
| 537 | lck->share_modes = (struct share_mode_entry *)
|
---|
[141] | 538 | TALLOC_MEMDUP(lck,
|
---|
| 539 | dbuf.dptr+sizeof(struct locking_data),
|
---|
[137] | 540 | lck->num_share_modes *
|
---|
| 541 | sizeof(struct share_mode_entry));
|
---|
| 542 |
|
---|
| 543 | if (lck->share_modes == NULL) {
|
---|
| 544 | smb_panic("parse_share_modes: talloc failed");
|
---|
| 545 | }
|
---|
| 546 | }
|
---|
| 547 |
|
---|
| 548 | /* Get any delete token. */
|
---|
[141] | 549 | if (data.u.s.delete_token_size) {
|
---|
| 550 | uint8 *p = dbuf.dptr + sizeof(struct locking_data) +
|
---|
[137] | 551 | (lck->num_share_modes *
|
---|
| 552 | sizeof(struct share_mode_entry));
|
---|
| 553 |
|
---|
[141] | 554 | if ((data.u.s.delete_token_size < sizeof(uid_t) + sizeof(gid_t)) ||
|
---|
| 555 | ((data.u.s.delete_token_size - sizeof(uid_t)) % sizeof(gid_t)) != 0) {
|
---|
[137] | 556 | DEBUG(0, ("parse_share_modes: invalid token size %d\n",
|
---|
[141] | 557 | data.u.s.delete_token_size));
|
---|
[137] | 558 | smb_panic("parse_share_modes: invalid token size");
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | lck->delete_token = TALLOC_P(lck, UNIX_USER_TOKEN);
|
---|
| 562 | if (!lck->delete_token) {
|
---|
| 563 | smb_panic("parse_share_modes: talloc failed");
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | /* Copy out the uid and gid. */
|
---|
| 567 | memcpy(&lck->delete_token->uid, p, sizeof(uid_t));
|
---|
| 568 | p += sizeof(uid_t);
|
---|
| 569 | memcpy(&lck->delete_token->gid, p, sizeof(gid_t));
|
---|
| 570 | p += sizeof(gid_t);
|
---|
| 571 |
|
---|
| 572 | /* Any supplementary groups ? */
|
---|
[141] | 573 | lck->delete_token->ngroups = (data.u.s.delete_token_size > (sizeof(uid_t) + sizeof(gid_t))) ?
|
---|
| 574 | ((data.u.s.delete_token_size -
|
---|
[137] | 575 | (sizeof(uid_t) + sizeof(gid_t)))/sizeof(gid_t)) : 0;
|
---|
| 576 |
|
---|
| 577 | if (lck->delete_token->ngroups) {
|
---|
| 578 | /* Make this a talloc child of lck->delete_token. */
|
---|
| 579 | lck->delete_token->groups = TALLOC_ARRAY(lck->delete_token, gid_t,
|
---|
| 580 | lck->delete_token->ngroups);
|
---|
| 581 | if (!lck->delete_token) {
|
---|
| 582 | smb_panic("parse_share_modes: talloc failed");
|
---|
| 583 | }
|
---|
| 584 |
|
---|
| 585 | for (i = 0; i < lck->delete_token->ngroups; i++) {
|
---|
| 586 | memcpy(&lck->delete_token->groups[i], p, sizeof(gid_t));
|
---|
| 587 | p += sizeof(gid_t);
|
---|
| 588 | }
|
---|
| 589 | }
|
---|
| 590 |
|
---|
| 591 | } else {
|
---|
| 592 | lck->delete_token = NULL;
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | /* Save off the associated service path and filename. */
|
---|
[141] | 596 | lck->servicepath = (const char *)dbuf.dptr + sizeof(struct locking_data) +
|
---|
[137] | 597 | (lck->num_share_modes * sizeof(struct share_mode_entry)) +
|
---|
[141] | 598 | data.u.s.delete_token_size;
|
---|
[137] | 599 |
|
---|
[141] | 600 | lck->filename = (const char *)dbuf.dptr + sizeof(struct locking_data) +
|
---|
[137] | 601 | (lck->num_share_modes * sizeof(struct share_mode_entry)) +
|
---|
[141] | 602 | data.u.s.delete_token_size +
|
---|
[137] | 603 | strlen(lck->servicepath) + 1;
|
---|
| 604 |
|
---|
| 605 | /*
|
---|
| 606 | * Ensure that each entry has a real process attached.
|
---|
| 607 | */
|
---|
| 608 |
|
---|
| 609 | for (i = 0; i < lck->num_share_modes; i++) {
|
---|
| 610 | struct share_mode_entry *entry_p = &lck->share_modes[i];
|
---|
| 611 | char *str = NULL;
|
---|
| 612 | if (DEBUGLEVEL >= 10) {
|
---|
| 613 | str = share_mode_str(NULL, i, entry_p);
|
---|
| 614 | }
|
---|
| 615 | DEBUG(10,("parse_share_modes: %s\n",
|
---|
| 616 | str ? str : ""));
|
---|
| 617 | if (!process_exists(entry_p->pid)) {
|
---|
| 618 | DEBUG(10,("parse_share_modes: deleted %s\n",
|
---|
| 619 | str ? str : ""));
|
---|
| 620 | entry_p->op_type = UNUSED_SHARE_MODE_ENTRY;
|
---|
| 621 | lck->modified = True;
|
---|
| 622 | }
|
---|
| 623 | TALLOC_FREE(str);
|
---|
| 624 | }
|
---|
| 625 |
|
---|
| 626 | return True;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | static TDB_DATA unparse_share_modes(struct share_mode_lock *lck)
|
---|
| 630 | {
|
---|
| 631 | TDB_DATA result;
|
---|
| 632 | int num_valid = 0;
|
---|
| 633 | int i;
|
---|
| 634 | struct locking_data *data;
|
---|
| 635 | ssize_t offset;
|
---|
| 636 | ssize_t sp_len;
|
---|
| 637 | uint32 delete_token_size;
|
---|
| 638 |
|
---|
| 639 | result.dptr = NULL;
|
---|
| 640 | result.dsize = 0;
|
---|
| 641 |
|
---|
| 642 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
| 643 | if (!is_unused_share_mode_entry(&lck->share_modes[i])) {
|
---|
| 644 | num_valid += 1;
|
---|
| 645 | }
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 | if (num_valid == 0) {
|
---|
| 649 | return result;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 | sp_len = strlen(lck->servicepath);
|
---|
| 653 | delete_token_size = (lck->delete_token ?
|
---|
| 654 | (sizeof(uid_t) + sizeof(gid_t) + (lck->delete_token->ngroups*sizeof(gid_t))) : 0);
|
---|
| 655 |
|
---|
| 656 | result.dsize = sizeof(*data) +
|
---|
| 657 | lck->num_share_modes * sizeof(struct share_mode_entry) +
|
---|
| 658 | delete_token_size +
|
---|
| 659 | sp_len + 1 +
|
---|
| 660 | strlen(lck->filename) + 1;
|
---|
| 661 | result.dptr = TALLOC_ARRAY(lck, uint8, result.dsize);
|
---|
| 662 |
|
---|
| 663 | if (result.dptr == NULL) {
|
---|
| 664 | smb_panic("talloc failed");
|
---|
| 665 | }
|
---|
| 666 |
|
---|
| 667 | data = (struct locking_data *)result.dptr;
|
---|
| 668 | ZERO_STRUCTP(data);
|
---|
| 669 | data->u.s.num_share_mode_entries = lck->num_share_modes;
|
---|
| 670 | data->u.s.delete_on_close = lck->delete_on_close;
|
---|
| 671 | data->u.s.old_write_time = lck->old_write_time;
|
---|
| 672 | data->u.s.changed_write_time = lck->changed_write_time;
|
---|
| 673 | data->u.s.delete_token_size = delete_token_size;
|
---|
| 674 |
|
---|
| 675 | DEBUG(10,("unparse_share_modes: del: %d, owrt: %s cwrt: %s, tok: %u, "
|
---|
| 676 | "num: %d\n", data->u.s.delete_on_close,
|
---|
| 677 | timestring(debug_ctx(),
|
---|
| 678 | convert_timespec_to_time_t(lck->old_write_time)),
|
---|
| 679 | timestring(debug_ctx(),
|
---|
| 680 | convert_timespec_to_time_t(
|
---|
| 681 | lck->changed_write_time)),
|
---|
| 682 | (unsigned int)data->u.s.delete_token_size,
|
---|
| 683 | data->u.s.num_share_mode_entries));
|
---|
| 684 |
|
---|
| 685 | memcpy(result.dptr + sizeof(*data), lck->share_modes,
|
---|
| 686 | sizeof(struct share_mode_entry)*lck->num_share_modes);
|
---|
| 687 | offset = sizeof(*data) +
|
---|
| 688 | sizeof(struct share_mode_entry)*lck->num_share_modes;
|
---|
| 689 |
|
---|
| 690 | /* Store any delete on close token. */
|
---|
| 691 | if (lck->delete_token) {
|
---|
| 692 | uint8 *p = result.dptr + offset;
|
---|
| 693 |
|
---|
| 694 | memcpy(p, &lck->delete_token->uid, sizeof(uid_t));
|
---|
| 695 | p += sizeof(uid_t);
|
---|
| 696 |
|
---|
| 697 | memcpy(p, &lck->delete_token->gid, sizeof(gid_t));
|
---|
| 698 | p += sizeof(gid_t);
|
---|
| 699 |
|
---|
| 700 | for (i = 0; i < lck->delete_token->ngroups; i++) {
|
---|
| 701 | memcpy(p, &lck->delete_token->groups[i], sizeof(gid_t));
|
---|
| 702 | p += sizeof(gid_t);
|
---|
| 703 | }
|
---|
| 704 | offset = p - result.dptr;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | safe_strcpy((char *)result.dptr + offset, lck->servicepath,
|
---|
| 708 | result.dsize - offset - 1);
|
---|
| 709 | offset += sp_len + 1;
|
---|
| 710 | safe_strcpy((char *)result.dptr + offset, lck->filename,
|
---|
| 711 | result.dsize - offset - 1);
|
---|
| 712 |
|
---|
| 713 | if (DEBUGLEVEL >= 10) {
|
---|
| 714 | print_share_mode_table(data);
|
---|
| 715 | }
|
---|
| 716 |
|
---|
| 717 | return result;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | static int share_mode_lock_destructor(struct share_mode_lock *lck)
|
---|
| 721 | {
|
---|
| 722 | NTSTATUS status;
|
---|
| 723 | TDB_DATA data;
|
---|
| 724 |
|
---|
| 725 | if (!lck->modified) {
|
---|
| 726 | return 0;
|
---|
| 727 | }
|
---|
| 728 |
|
---|
| 729 | data = unparse_share_modes(lck);
|
---|
| 730 |
|
---|
| 731 | if (data.dptr == NULL) {
|
---|
| 732 | if (!lck->fresh) {
|
---|
| 733 | /* There has been an entry before, delete it */
|
---|
| 734 |
|
---|
| 735 | status = lck->record->delete_rec(lck->record);
|
---|
| 736 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 737 | DEBUG(0, ("delete_rec returned %s\n",
|
---|
| 738 | nt_errstr(status)));
|
---|
| 739 | smb_panic("could not delete share entry");
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 | goto done;
|
---|
| 743 | }
|
---|
| 744 |
|
---|
| 745 | status = lck->record->store(lck->record, data, TDB_REPLACE);
|
---|
| 746 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 747 | DEBUG(0, ("store returned %s\n", nt_errstr(status)));
|
---|
| 748 | smb_panic("could not store share mode entry");
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | done:
|
---|
| 752 |
|
---|
| 753 | return 0;
|
---|
| 754 | }
|
---|
| 755 |
|
---|
| 756 | static bool fill_share_mode_lock(struct share_mode_lock *lck,
|
---|
| 757 | struct file_id id,
|
---|
| 758 | const char *servicepath,
|
---|
| 759 | const char *fname,
|
---|
| 760 | TDB_DATA share_mode_data,
|
---|
| 761 | const struct timespec *old_write_time)
|
---|
| 762 | {
|
---|
| 763 | /* Ensure we set every field here as the destructor must be
|
---|
| 764 | valid even if parse_share_modes fails. */
|
---|
| 765 |
|
---|
| 766 | lck->servicepath = NULL;
|
---|
| 767 | lck->filename = NULL;
|
---|
| 768 | lck->id = id;
|
---|
| 769 | lck->num_share_modes = 0;
|
---|
| 770 | lck->share_modes = NULL;
|
---|
| 771 | lck->delete_token = NULL;
|
---|
| 772 | lck->delete_on_close = False;
|
---|
| 773 | ZERO_STRUCT(lck->old_write_time);
|
---|
| 774 | ZERO_STRUCT(lck->changed_write_time);
|
---|
| 775 | lck->fresh = False;
|
---|
| 776 | lck->modified = False;
|
---|
| 777 |
|
---|
| 778 | lck->fresh = (share_mode_data.dptr == NULL);
|
---|
| 779 |
|
---|
| 780 | if (lck->fresh) {
|
---|
| 781 | if (fname == NULL || servicepath == NULL
|
---|
| 782 | || old_write_time == NULL) {
|
---|
| 783 | return False;
|
---|
| 784 | }
|
---|
| 785 | lck->filename = talloc_strdup(lck, fname);
|
---|
| 786 | lck->servicepath = talloc_strdup(lck, servicepath);
|
---|
| 787 | if (lck->filename == NULL || lck->servicepath == NULL) {
|
---|
| 788 | DEBUG(0, ("talloc failed\n"));
|
---|
| 789 | return False;
|
---|
| 790 | }
|
---|
| 791 | lck->old_write_time = *old_write_time;
|
---|
| 792 | } else {
|
---|
| 793 | if (!parse_share_modes(share_mode_data, lck)) {
|
---|
| 794 | DEBUG(0, ("Could not parse share modes\n"));
|
---|
| 795 | return False;
|
---|
| 796 | }
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 | return True;
|
---|
| 800 | }
|
---|
| 801 |
|
---|
| 802 | struct share_mode_lock *get_share_mode_lock(TALLOC_CTX *mem_ctx,
|
---|
| 803 | const struct file_id id,
|
---|
| 804 | const char *servicepath,
|
---|
| 805 | const char *fname,
|
---|
| 806 | const struct timespec *old_write_time)
|
---|
| 807 | {
|
---|
| 808 | struct share_mode_lock *lck;
|
---|
| 809 | struct file_id tmp;
|
---|
| 810 | TDB_DATA key = locking_key(&id, &tmp);
|
---|
| 811 |
|
---|
| 812 | if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
|
---|
| 813 | DEBUG(0, ("talloc failed\n"));
|
---|
| 814 | return NULL;
|
---|
| 815 | }
|
---|
| 816 |
|
---|
| 817 | if (!(lck->record = lock_db->fetch_locked(lock_db, lck, key))) {
|
---|
| 818 | DEBUG(3, ("Could not lock share entry\n"));
|
---|
| 819 | TALLOC_FREE(lck);
|
---|
| 820 | return NULL;
|
---|
| 821 | }
|
---|
| 822 |
|
---|
| 823 | if (!fill_share_mode_lock(lck, id, servicepath, fname,
|
---|
| 824 | lck->record->value, old_write_time)) {
|
---|
| 825 | DEBUG(3, ("fill_share_mode_lock failed\n"));
|
---|
| 826 | TALLOC_FREE(lck);
|
---|
| 827 | return NULL;
|
---|
| 828 | }
|
---|
| 829 |
|
---|
| 830 | talloc_set_destructor(lck, share_mode_lock_destructor);
|
---|
| 831 |
|
---|
| 832 | return lck;
|
---|
| 833 | }
|
---|
| 834 |
|
---|
| 835 | struct share_mode_lock *fetch_share_mode_unlocked(TALLOC_CTX *mem_ctx,
|
---|
| 836 | const struct file_id id,
|
---|
| 837 | const char *servicepath,
|
---|
| 838 | const char *fname)
|
---|
| 839 | {
|
---|
| 840 | struct share_mode_lock *lck;
|
---|
| 841 | struct file_id tmp;
|
---|
| 842 | TDB_DATA key = locking_key(&id, &tmp);
|
---|
| 843 | TDB_DATA data;
|
---|
| 844 |
|
---|
| 845 | if (!(lck = TALLOC_P(mem_ctx, struct share_mode_lock))) {
|
---|
| 846 | DEBUG(0, ("talloc failed\n"));
|
---|
| 847 | return NULL;
|
---|
| 848 | }
|
---|
| 849 |
|
---|
| 850 | if (lock_db->fetch(lock_db, lck, key, &data) == -1) {
|
---|
| 851 | DEBUG(3, ("Could not fetch share entry\n"));
|
---|
| 852 | TALLOC_FREE(lck);
|
---|
| 853 | return NULL;
|
---|
| 854 | }
|
---|
| 855 |
|
---|
| 856 | if (!fill_share_mode_lock(lck, id, servicepath, fname, data, NULL)) {
|
---|
[233] | 857 | DEBUG(10, ("fetch_share_mode_unlocked: no share_mode record "
|
---|
| 858 | "around (file not open)\n"));
|
---|
[137] | 859 | TALLOC_FREE(lck);
|
---|
| 860 | return NULL;
|
---|
| 861 | }
|
---|
| 862 |
|
---|
| 863 | return lck;
|
---|
| 864 | }
|
---|
| 865 |
|
---|
| 866 | /*******************************************************************
|
---|
| 867 | Sets the service name and filename for rename.
|
---|
| 868 | At this point we emit "file renamed" messages to all
|
---|
| 869 | process id's that have this file open.
|
---|
| 870 | Based on an initial code idea from SATOH Fumiyasu <fumiya@samba.gr.jp>
|
---|
| 871 | ********************************************************************/
|
---|
| 872 |
|
---|
| 873 | bool rename_share_filename(struct messaging_context *msg_ctx,
|
---|
| 874 | struct share_mode_lock *lck,
|
---|
| 875 | const char *servicepath,
|
---|
| 876 | const char *newname)
|
---|
| 877 | {
|
---|
| 878 | size_t sp_len;
|
---|
| 879 | size_t fn_len;
|
---|
| 880 | size_t msg_len;
|
---|
| 881 | char *frm = NULL;
|
---|
| 882 | int i;
|
---|
| 883 |
|
---|
| 884 | DEBUG(10, ("rename_share_filename: servicepath %s newname %s\n",
|
---|
| 885 | servicepath, newname));
|
---|
| 886 |
|
---|
| 887 | /*
|
---|
| 888 | * rename_internal_fsp() and rename_internals() add './' to
|
---|
| 889 | * head of newname if newname does not contain a '/'.
|
---|
| 890 | */
|
---|
| 891 | while (newname[0] && newname[1] && newname[0] == '.' && newname[1] == '/') {
|
---|
| 892 | newname += 2;
|
---|
| 893 | }
|
---|
| 894 |
|
---|
| 895 | lck->servicepath = talloc_strdup(lck, servicepath);
|
---|
| 896 | lck->filename = talloc_strdup(lck, newname);
|
---|
| 897 | if (lck->filename == NULL || lck->servicepath == NULL) {
|
---|
| 898 | DEBUG(0, ("rename_share_filename: talloc failed\n"));
|
---|
| 899 | return False;
|
---|
| 900 | }
|
---|
| 901 | lck->modified = True;
|
---|
| 902 |
|
---|
| 903 | sp_len = strlen(lck->servicepath);
|
---|
| 904 | fn_len = strlen(lck->filename);
|
---|
| 905 |
|
---|
| 906 | msg_len = MSG_FILE_RENAMED_MIN_SIZE + sp_len + 1 + fn_len + 1;
|
---|
| 907 |
|
---|
| 908 | /* Set up the name changed message. */
|
---|
| 909 | frm = TALLOC_ARRAY(lck, char, msg_len);
|
---|
| 910 | if (!frm) {
|
---|
| 911 | return False;
|
---|
| 912 | }
|
---|
| 913 |
|
---|
| 914 | push_file_id_16(frm, &lck->id);
|
---|
| 915 |
|
---|
| 916 | DEBUG(10,("rename_share_filename: msg_len = %u\n", (unsigned int)msg_len ));
|
---|
| 917 |
|
---|
| 918 | safe_strcpy(&frm[16], lck->servicepath, sp_len);
|
---|
| 919 | safe_strcpy(&frm[16 + sp_len + 1], lck->filename, fn_len);
|
---|
| 920 |
|
---|
| 921 | /* Send the messages. */
|
---|
| 922 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
| 923 | struct share_mode_entry *se = &lck->share_modes[i];
|
---|
| 924 | if (!is_valid_share_mode_entry(se)) {
|
---|
| 925 | continue;
|
---|
| 926 | }
|
---|
| 927 | /* But not to ourselves... */
|
---|
| 928 | if (procid_is_me(&se->pid)) {
|
---|
| 929 | continue;
|
---|
| 930 | }
|
---|
| 931 |
|
---|
| 932 | DEBUG(10,("rename_share_filename: sending rename message to pid %s "
|
---|
| 933 | "file_id %s sharepath %s newname %s\n",
|
---|
| 934 | procid_str_static(&se->pid),
|
---|
| 935 | file_id_string_tos(&lck->id),
|
---|
| 936 | lck->servicepath, lck->filename ));
|
---|
| 937 |
|
---|
| 938 | messaging_send_buf(msg_ctx, se->pid, MSG_SMB_FILE_RENAME,
|
---|
| 939 | (uint8 *)frm, msg_len);
|
---|
| 940 | }
|
---|
| 941 |
|
---|
| 942 | return True;
|
---|
| 943 | }
|
---|
| 944 |
|
---|
| 945 | void get_file_infos(struct file_id id,
|
---|
| 946 | bool *delete_on_close,
|
---|
| 947 | struct timespec *write_time)
|
---|
| 948 | {
|
---|
| 949 | struct share_mode_lock *lck;
|
---|
| 950 |
|
---|
| 951 | if (delete_on_close) {
|
---|
| 952 | *delete_on_close = false;
|
---|
| 953 | }
|
---|
| 954 |
|
---|
| 955 | if (write_time) {
|
---|
| 956 | ZERO_STRUCTP(write_time);
|
---|
| 957 | }
|
---|
| 958 |
|
---|
| 959 | if (!(lck = fetch_share_mode_unlocked(talloc_tos(), id, NULL, NULL))) {
|
---|
| 960 | return;
|
---|
| 961 | }
|
---|
| 962 |
|
---|
| 963 | if (delete_on_close) {
|
---|
| 964 | *delete_on_close = lck->delete_on_close;
|
---|
| 965 | }
|
---|
| 966 |
|
---|
| 967 | if (write_time) {
|
---|
| 968 | struct timespec wt;
|
---|
| 969 |
|
---|
| 970 | wt = lck->changed_write_time;
|
---|
| 971 | if (null_timespec(wt)) {
|
---|
| 972 | wt = lck->old_write_time;
|
---|
| 973 | }
|
---|
| 974 |
|
---|
| 975 | *write_time = wt;
|
---|
| 976 | }
|
---|
| 977 |
|
---|
| 978 | TALLOC_FREE(lck);
|
---|
| 979 | }
|
---|
| 980 |
|
---|
| 981 | bool is_valid_share_mode_entry(const struct share_mode_entry *e)
|
---|
| 982 | {
|
---|
| 983 | int num_props = 0;
|
---|
| 984 |
|
---|
| 985 | if (e->op_type == UNUSED_SHARE_MODE_ENTRY) {
|
---|
| 986 | /* cope with dead entries from the process not
|
---|
| 987 | existing. These should not be considered valid,
|
---|
| 988 | otherwise we end up doing zero timeout sharing
|
---|
| 989 | violation */
|
---|
| 990 | return False;
|
---|
| 991 | }
|
---|
| 992 |
|
---|
| 993 | num_props += ((e->op_type == NO_OPLOCK) ? 1 : 0);
|
---|
| 994 | num_props += (EXCLUSIVE_OPLOCK_TYPE(e->op_type) ? 1 : 0);
|
---|
| 995 | num_props += (LEVEL_II_OPLOCK_TYPE(e->op_type) ? 1 : 0);
|
---|
| 996 |
|
---|
| 997 | SMB_ASSERT(num_props <= 1);
|
---|
| 998 | return (num_props != 0);
|
---|
| 999 | }
|
---|
| 1000 |
|
---|
| 1001 | bool is_deferred_open_entry(const struct share_mode_entry *e)
|
---|
| 1002 | {
|
---|
| 1003 | return (e->op_type == DEFERRED_OPEN_ENTRY);
|
---|
| 1004 | }
|
---|
| 1005 |
|
---|
| 1006 | bool is_unused_share_mode_entry(const struct share_mode_entry *e)
|
---|
| 1007 | {
|
---|
| 1008 | return (e->op_type == UNUSED_SHARE_MODE_ENTRY);
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | /*******************************************************************
|
---|
| 1012 | Fill a share mode entry.
|
---|
| 1013 | ********************************************************************/
|
---|
| 1014 |
|
---|
| 1015 | static void fill_share_mode_entry(struct share_mode_entry *e,
|
---|
| 1016 | files_struct *fsp,
|
---|
| 1017 | uid_t uid, uint16 mid, uint16 op_type)
|
---|
| 1018 | {
|
---|
| 1019 | ZERO_STRUCTP(e);
|
---|
| 1020 | e->pid = procid_self();
|
---|
| 1021 | e->share_access = fsp->share_access;
|
---|
| 1022 | e->private_options = fsp->fh->private_options;
|
---|
| 1023 | e->access_mask = fsp->access_mask;
|
---|
| 1024 | e->op_mid = mid;
|
---|
| 1025 | e->op_type = op_type;
|
---|
| 1026 | e->time.tv_sec = fsp->open_time.tv_sec;
|
---|
| 1027 | e->time.tv_usec = fsp->open_time.tv_usec;
|
---|
| 1028 | e->id = fsp->file_id;
|
---|
| 1029 | e->share_file_id = fsp->fh->gen_id;
|
---|
| 1030 | e->uid = (uint32)uid;
|
---|
| 1031 | e->flags = fsp->posix_open ? SHARE_MODE_FLAG_POSIX_OPEN : 0;
|
---|
| 1032 | }
|
---|
| 1033 |
|
---|
| 1034 | static void fill_deferred_open_entry(struct share_mode_entry *e,
|
---|
| 1035 | const struct timeval request_time,
|
---|
| 1036 | struct file_id id, uint16 mid)
|
---|
| 1037 | {
|
---|
| 1038 | ZERO_STRUCTP(e);
|
---|
| 1039 | e->pid = procid_self();
|
---|
| 1040 | e->op_mid = mid;
|
---|
| 1041 | e->op_type = DEFERRED_OPEN_ENTRY;
|
---|
| 1042 | e->time.tv_sec = request_time.tv_sec;
|
---|
| 1043 | e->time.tv_usec = request_time.tv_usec;
|
---|
| 1044 | e->id = id;
|
---|
| 1045 | e->uid = (uint32)-1;
|
---|
| 1046 | e->flags = 0;
|
---|
| 1047 | }
|
---|
| 1048 |
|
---|
| 1049 | static void add_share_mode_entry(struct share_mode_lock *lck,
|
---|
| 1050 | const struct share_mode_entry *entry)
|
---|
| 1051 | {
|
---|
| 1052 | int i;
|
---|
| 1053 |
|
---|
| 1054 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
| 1055 | struct share_mode_entry *e = &lck->share_modes[i];
|
---|
| 1056 | if (is_unused_share_mode_entry(e)) {
|
---|
| 1057 | *e = *entry;
|
---|
| 1058 | break;
|
---|
| 1059 | }
|
---|
| 1060 | }
|
---|
| 1061 |
|
---|
| 1062 | if (i == lck->num_share_modes) {
|
---|
| 1063 | /* No unused entry found */
|
---|
| 1064 | ADD_TO_ARRAY(lck, struct share_mode_entry, *entry,
|
---|
| 1065 | &lck->share_modes, &lck->num_share_modes);
|
---|
| 1066 | }
|
---|
| 1067 | lck->modified = True;
|
---|
| 1068 | }
|
---|
| 1069 |
|
---|
| 1070 | void set_share_mode(struct share_mode_lock *lck, files_struct *fsp,
|
---|
[232] | 1071 | uid_t uid, uint16 mid, uint16 op_type)
|
---|
[137] | 1072 | {
|
---|
| 1073 | struct share_mode_entry entry;
|
---|
| 1074 | fill_share_mode_entry(&entry, fsp, uid, mid, op_type);
|
---|
| 1075 | add_share_mode_entry(lck, &entry);
|
---|
| 1076 | }
|
---|
| 1077 |
|
---|
| 1078 | void add_deferred_open(struct share_mode_lock *lck, uint16 mid,
|
---|
| 1079 | struct timeval request_time,
|
---|
| 1080 | struct file_id id)
|
---|
| 1081 | {
|
---|
| 1082 | struct share_mode_entry entry;
|
---|
| 1083 | fill_deferred_open_entry(&entry, request_time, id, mid);
|
---|
| 1084 | add_share_mode_entry(lck, &entry);
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | /*******************************************************************
|
---|
| 1088 | Check if two share mode entries are identical, ignoring oplock
|
---|
| 1089 | and mid info and desired_access. (Removed paranoia test - it's
|
---|
| 1090 | not automatically a logic error if they are identical. JRA.)
|
---|
| 1091 | ********************************************************************/
|
---|
| 1092 |
|
---|
| 1093 | static bool share_modes_identical(struct share_mode_entry *e1,
|
---|
| 1094 | struct share_mode_entry *e2)
|
---|
| 1095 | {
|
---|
| 1096 | /* We used to check for e1->share_access == e2->share_access here
|
---|
| 1097 | as well as the other fields but 2 different DOS or FCB opens
|
---|
| 1098 | sharing the same share mode entry may validly differ in
|
---|
| 1099 | fsp->share_access field. */
|
---|
| 1100 |
|
---|
| 1101 | return (procid_equal(&e1->pid, &e2->pid) &&
|
---|
| 1102 | file_id_equal(&e1->id, &e2->id) &&
|
---|
| 1103 | e1->share_file_id == e2->share_file_id );
|
---|
| 1104 | }
|
---|
| 1105 |
|
---|
| 1106 | static bool deferred_open_identical(struct share_mode_entry *e1,
|
---|
| 1107 | struct share_mode_entry *e2)
|
---|
| 1108 | {
|
---|
| 1109 | return (procid_equal(&e1->pid, &e2->pid) &&
|
---|
| 1110 | (e1->op_mid == e2->op_mid) &&
|
---|
| 1111 | file_id_equal(&e1->id, &e2->id));
|
---|
| 1112 | }
|
---|
| 1113 |
|
---|
| 1114 | static struct share_mode_entry *find_share_mode_entry(struct share_mode_lock *lck,
|
---|
| 1115 | struct share_mode_entry *entry)
|
---|
| 1116 | {
|
---|
| 1117 | int i;
|
---|
| 1118 |
|
---|
| 1119 | for (i=0; i<lck->num_share_modes; i++) {
|
---|
| 1120 | struct share_mode_entry *e = &lck->share_modes[i];
|
---|
| 1121 | if (is_valid_share_mode_entry(entry) &&
|
---|
| 1122 | is_valid_share_mode_entry(e) &&
|
---|
| 1123 | share_modes_identical(e, entry)) {
|
---|
| 1124 | return e;
|
---|
| 1125 | }
|
---|
| 1126 | if (is_deferred_open_entry(entry) &&
|
---|
| 1127 | is_deferred_open_entry(e) &&
|
---|
| 1128 | deferred_open_identical(e, entry)) {
|
---|
| 1129 | return e;
|
---|
| 1130 | }
|
---|
| 1131 | }
|
---|
| 1132 | return NULL;
|
---|
| 1133 | }
|
---|
| 1134 |
|
---|
| 1135 | /*******************************************************************
|
---|
| 1136 | Del the share mode of a file for this process. Return the number of
|
---|
| 1137 | entries left.
|
---|
| 1138 | ********************************************************************/
|
---|
| 1139 |
|
---|
| 1140 | bool del_share_mode(struct share_mode_lock *lck, files_struct *fsp)
|
---|
| 1141 | {
|
---|
| 1142 | struct share_mode_entry entry, *e;
|
---|
| 1143 |
|
---|
| 1144 | /* Don't care about the pid owner being correct here - just a search. */
|
---|
| 1145 | fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
|
---|
| 1146 |
|
---|
| 1147 | e = find_share_mode_entry(lck, &entry);
|
---|
| 1148 | if (e == NULL) {
|
---|
| 1149 | return False;
|
---|
| 1150 | }
|
---|
| 1151 |
|
---|
| 1152 | e->op_type = UNUSED_SHARE_MODE_ENTRY;
|
---|
| 1153 | lck->modified = True;
|
---|
| 1154 | return True;
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | void del_deferred_open_entry(struct share_mode_lock *lck, uint16 mid)
|
---|
| 1158 | {
|
---|
| 1159 | struct share_mode_entry entry, *e;
|
---|
| 1160 |
|
---|
| 1161 | fill_deferred_open_entry(&entry, timeval_zero(),
|
---|
| 1162 | lck->id, mid);
|
---|
| 1163 |
|
---|
| 1164 | e = find_share_mode_entry(lck, &entry);
|
---|
| 1165 | if (e == NULL) {
|
---|
| 1166 | return;
|
---|
| 1167 | }
|
---|
| 1168 |
|
---|
| 1169 | e->op_type = UNUSED_SHARE_MODE_ENTRY;
|
---|
| 1170 | lck->modified = True;
|
---|
| 1171 | }
|
---|
| 1172 |
|
---|
| 1173 | /*******************************************************************
|
---|
| 1174 | Remove an oplock mid and mode entry from a share mode.
|
---|
| 1175 | ********************************************************************/
|
---|
| 1176 |
|
---|
| 1177 | bool remove_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
|
---|
| 1178 | {
|
---|
| 1179 | struct share_mode_entry entry, *e;
|
---|
| 1180 |
|
---|
| 1181 | /* Don't care about the pid owner being correct here - just a search. */
|
---|
| 1182 | fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
|
---|
| 1183 |
|
---|
| 1184 | e = find_share_mode_entry(lck, &entry);
|
---|
| 1185 | if (e == NULL) {
|
---|
| 1186 | return False;
|
---|
| 1187 | }
|
---|
| 1188 |
|
---|
| 1189 | e->op_mid = 0;
|
---|
[232] | 1190 | if (EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type)) {
|
---|
| 1191 | /*
|
---|
| 1192 | * Going from exclusive or batch,
|
---|
| 1193 | * we always go through FAKE_LEVEL_II
|
---|
| 1194 | * first.
|
---|
| 1195 | */
|
---|
| 1196 | e->op_type = FAKE_LEVEL_II_OPLOCK;
|
---|
| 1197 | } else {
|
---|
| 1198 | e->op_type = NO_OPLOCK;
|
---|
| 1199 | }
|
---|
[137] | 1200 | lck->modified = True;
|
---|
| 1201 | return True;
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
| 1204 | /*******************************************************************
|
---|
| 1205 | Downgrade a oplock type from exclusive to level II.
|
---|
| 1206 | ********************************************************************/
|
---|
| 1207 |
|
---|
| 1208 | bool downgrade_share_oplock(struct share_mode_lock *lck, files_struct *fsp)
|
---|
| 1209 | {
|
---|
| 1210 | struct share_mode_entry entry, *e;
|
---|
| 1211 |
|
---|
| 1212 | /* Don't care about the pid owner being correct here - just a search. */
|
---|
| 1213 | fill_share_mode_entry(&entry, fsp, (uid_t)-1, 0, NO_OPLOCK);
|
---|
| 1214 |
|
---|
| 1215 | e = find_share_mode_entry(lck, &entry);
|
---|
| 1216 | if (e == NULL) {
|
---|
| 1217 | return False;
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 | e->op_type = LEVEL_II_OPLOCK;
|
---|
| 1221 | lck->modified = True;
|
---|
| 1222 | return True;
|
---|
| 1223 | }
|
---|
| 1224 |
|
---|
| 1225 | /****************************************************************************
|
---|
| 1226 | Deal with the internal needs of setting the delete on close flag. Note that
|
---|
| 1227 | as the tdb locking is recursive, it is safe to call this from within
|
---|
| 1228 | open_file_ntcreate. JRA.
|
---|
| 1229 | ****************************************************************************/
|
---|
| 1230 |
|
---|
| 1231 | NTSTATUS can_set_delete_on_close(files_struct *fsp, bool delete_on_close,
|
---|
| 1232 | uint32 dosmode)
|
---|
| 1233 | {
|
---|
| 1234 | if (!delete_on_close) {
|
---|
| 1235 | return NT_STATUS_OK;
|
---|
| 1236 | }
|
---|
| 1237 |
|
---|
| 1238 | /*
|
---|
| 1239 | * Only allow delete on close for writable files.
|
---|
| 1240 | */
|
---|
| 1241 |
|
---|
| 1242 | if ((dosmode & aRONLY) &&
|
---|
| 1243 | !lp_delete_readonly(SNUM(fsp->conn))) {
|
---|
| 1244 | DEBUG(10,("can_set_delete_on_close: file %s delete on close "
|
---|
| 1245 | "flag set but file attribute is readonly.\n",
|
---|
| 1246 | fsp->fsp_name ));
|
---|
| 1247 | return NT_STATUS_CANNOT_DELETE;
|
---|
| 1248 | }
|
---|
| 1249 |
|
---|
| 1250 | /*
|
---|
| 1251 | * Only allow delete on close for writable shares.
|
---|
| 1252 | */
|
---|
| 1253 |
|
---|
| 1254 | if (!CAN_WRITE(fsp->conn)) {
|
---|
| 1255 | DEBUG(10,("can_set_delete_on_close: file %s delete on "
|
---|
| 1256 | "close flag set but write access denied on share.\n",
|
---|
| 1257 | fsp->fsp_name ));
|
---|
| 1258 | return NT_STATUS_ACCESS_DENIED;
|
---|
| 1259 | }
|
---|
| 1260 |
|
---|
| 1261 | /*
|
---|
| 1262 | * Only allow delete on close for files/directories opened with delete
|
---|
| 1263 | * intent.
|
---|
| 1264 | */
|
---|
| 1265 |
|
---|
| 1266 | if (!(fsp->access_mask & DELETE_ACCESS)) {
|
---|
| 1267 | DEBUG(10,("can_set_delete_on_close: file %s delete on "
|
---|
| 1268 | "close flag set but delete access denied.\n",
|
---|
| 1269 | fsp->fsp_name ));
|
---|
| 1270 | return NT_STATUS_ACCESS_DENIED;
|
---|
| 1271 | }
|
---|
| 1272 |
|
---|
| 1273 | /* Don't allow delete on close for non-empty directories. */
|
---|
| 1274 | if (fsp->is_directory) {
|
---|
| 1275 | return can_delete_directory(fsp->conn, fsp->fsp_name);
|
---|
| 1276 | }
|
---|
| 1277 |
|
---|
| 1278 | return NT_STATUS_OK;
|
---|
| 1279 | }
|
---|
| 1280 |
|
---|
| 1281 | /*************************************************************************
|
---|
| 1282 | Return a talloced copy of a UNIX_USER_TOKEN. NULL on fail.
|
---|
| 1283 | (Should this be in locking.c.... ?).
|
---|
| 1284 | *************************************************************************/
|
---|
| 1285 |
|
---|
| 1286 | static UNIX_USER_TOKEN *copy_unix_token(TALLOC_CTX *ctx, UNIX_USER_TOKEN *tok)
|
---|
| 1287 | {
|
---|
| 1288 | UNIX_USER_TOKEN *cpy;
|
---|
| 1289 |
|
---|
| 1290 | if (tok == NULL) {
|
---|
| 1291 | return NULL;
|
---|
| 1292 | }
|
---|
| 1293 |
|
---|
| 1294 | cpy = TALLOC_P(ctx, UNIX_USER_TOKEN);
|
---|
| 1295 | if (!cpy) {
|
---|
| 1296 | return NULL;
|
---|
| 1297 | }
|
---|
| 1298 |
|
---|
| 1299 | cpy->uid = tok->uid;
|
---|
| 1300 | cpy->gid = tok->gid;
|
---|
| 1301 | cpy->ngroups = tok->ngroups;
|
---|
| 1302 | if (tok->ngroups) {
|
---|
| 1303 | /* Make this a talloc child of cpy. */
|
---|
| 1304 | cpy->groups = TALLOC_ARRAY(cpy, gid_t, tok->ngroups);
|
---|
| 1305 | if (!cpy->groups) {
|
---|
| 1306 | return NULL;
|
---|
| 1307 | }
|
---|
| 1308 | memcpy(cpy->groups, tok->groups, tok->ngroups * sizeof(gid_t));
|
---|
| 1309 | }
|
---|
| 1310 | return cpy;
|
---|
| 1311 | }
|
---|
| 1312 |
|
---|
| 1313 | /****************************************************************************
|
---|
| 1314 | Replace the delete on close token.
|
---|
| 1315 | ****************************************************************************/
|
---|
| 1316 |
|
---|
| 1317 | void set_delete_on_close_token(struct share_mode_lock *lck, UNIX_USER_TOKEN *tok)
|
---|
| 1318 | {
|
---|
| 1319 | TALLOC_FREE(lck->delete_token); /* Also deletes groups... */
|
---|
| 1320 |
|
---|
| 1321 | /* Copy the new token (can be NULL). */
|
---|
| 1322 | lck->delete_token = copy_unix_token(lck, tok);
|
---|
| 1323 | lck->modified = True;
|
---|
| 1324 | }
|
---|
| 1325 |
|
---|
| 1326 | /****************************************************************************
|
---|
| 1327 | Sets the delete on close flag over all share modes on this file.
|
---|
| 1328 | Modify the share mode entry for all files open
|
---|
| 1329 | on this device and inode to tell other smbds we have
|
---|
| 1330 | changed the delete on close flag. This will be noticed
|
---|
| 1331 | in the close code, the last closer will delete the file
|
---|
| 1332 | if flag is set.
|
---|
| 1333 | This makes a copy of any UNIX_USER_TOKEN into the
|
---|
| 1334 | lck entry. This function is used when the lock is already granted.
|
---|
| 1335 | ****************************************************************************/
|
---|
| 1336 |
|
---|
| 1337 | void set_delete_on_close_lck(struct share_mode_lock *lck, bool delete_on_close, UNIX_USER_TOKEN *tok)
|
---|
| 1338 | {
|
---|
| 1339 | if (lck->delete_on_close != delete_on_close) {
|
---|
| 1340 | set_delete_on_close_token(lck, tok);
|
---|
| 1341 | lck->delete_on_close = delete_on_close;
|
---|
| 1342 | if (delete_on_close) {
|
---|
| 1343 | SMB_ASSERT(lck->delete_token != NULL);
|
---|
| 1344 | }
|
---|
| 1345 | lck->modified = True;
|
---|
| 1346 | }
|
---|
| 1347 | }
|
---|
| 1348 |
|
---|
| 1349 | bool set_delete_on_close(files_struct *fsp, bool delete_on_close, UNIX_USER_TOKEN *tok)
|
---|
| 1350 | {
|
---|
| 1351 | struct share_mode_lock *lck;
|
---|
| 1352 |
|
---|
| 1353 | DEBUG(10,("set_delete_on_close: %s delete on close flag for "
|
---|
| 1354 | "fnum = %d, file %s\n",
|
---|
| 1355 | delete_on_close ? "Adding" : "Removing", fsp->fnum,
|
---|
| 1356 | fsp->fsp_name ));
|
---|
| 1357 |
|
---|
| 1358 | if (fsp->is_stat) {
|
---|
| 1359 | return True;
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 | lck = get_share_mode_lock(talloc_tos(), fsp->file_id, NULL, NULL,
|
---|
| 1363 | NULL);
|
---|
| 1364 | if (lck == NULL) {
|
---|
| 1365 | return False;
|
---|
| 1366 | }
|
---|
| 1367 |
|
---|
| 1368 | set_delete_on_close_lck(lck, delete_on_close, tok);
|
---|
| 1369 |
|
---|
| 1370 | if (fsp->is_directory) {
|
---|
| 1371 | send_stat_cache_delete_message(fsp->fsp_name);
|
---|
| 1372 | }
|
---|
| 1373 |
|
---|
| 1374 | TALLOC_FREE(lck);
|
---|
| 1375 | return True;
|
---|
| 1376 | }
|
---|
| 1377 |
|
---|
[204] | 1378 | bool set_sticky_write_time(struct file_id fileid, struct timespec write_time)
|
---|
[137] | 1379 | {
|
---|
| 1380 | struct share_mode_lock *lck;
|
---|
| 1381 |
|
---|
[204] | 1382 | DEBUG(5,("set_sticky_write_time: %s id=%s\n",
|
---|
[137] | 1383 | timestring(debug_ctx(),
|
---|
| 1384 | convert_timespec_to_time_t(write_time)),
|
---|
[204] | 1385 | file_id_string_tos(&fileid)));
|
---|
[137] | 1386 |
|
---|
| 1387 | lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
|
---|
| 1388 | if (lck == NULL) {
|
---|
| 1389 | return False;
|
---|
| 1390 | }
|
---|
| 1391 |
|
---|
[204] | 1392 | if (timespec_compare(&lck->changed_write_time, &write_time) != 0) {
|
---|
[137] | 1393 | lck->modified = True;
|
---|
| 1394 | lck->changed_write_time = write_time;
|
---|
| 1395 | }
|
---|
| 1396 |
|
---|
| 1397 | TALLOC_FREE(lck);
|
---|
| 1398 | return True;
|
---|
| 1399 | }
|
---|
| 1400 |
|
---|
[204] | 1401 | bool set_write_time(struct file_id fileid, struct timespec write_time)
|
---|
| 1402 | {
|
---|
| 1403 | struct share_mode_lock *lck;
|
---|
| 1404 |
|
---|
| 1405 | DEBUG(5,("set_write_time: %s id=%s\n",
|
---|
| 1406 | timestring(debug_ctx(),
|
---|
| 1407 | convert_timespec_to_time_t(write_time)),
|
---|
| 1408 | file_id_string_tos(&fileid)));
|
---|
| 1409 |
|
---|
| 1410 | lck = get_share_mode_lock(NULL, fileid, NULL, NULL, NULL);
|
---|
| 1411 | if (lck == NULL) {
|
---|
| 1412 | return False;
|
---|
| 1413 | }
|
---|
| 1414 |
|
---|
| 1415 | if (timespec_compare(&lck->old_write_time, &write_time) != 0) {
|
---|
| 1416 | lck->modified = True;
|
---|
| 1417 | lck->old_write_time = write_time;
|
---|
| 1418 | }
|
---|
| 1419 |
|
---|
| 1420 | TALLOC_FREE(lck);
|
---|
| 1421 | return True;
|
---|
| 1422 | }
|
---|
| 1423 |
|
---|
| 1424 |
|
---|
[137] | 1425 | struct forall_state {
|
---|
| 1426 | void (*fn)(const struct share_mode_entry *entry,
|
---|
| 1427 | const char *sharepath,
|
---|
| 1428 | const char *fname,
|
---|
| 1429 | void *private_data);
|
---|
| 1430 | void *private_data;
|
---|
| 1431 | };
|
---|
| 1432 |
|
---|
| 1433 | static int traverse_fn(struct db_record *rec, void *_state)
|
---|
| 1434 | {
|
---|
| 1435 | struct forall_state *state = (struct forall_state *)_state;
|
---|
| 1436 | struct locking_data *data;
|
---|
| 1437 | struct share_mode_entry *shares;
|
---|
| 1438 | const char *sharepath;
|
---|
| 1439 | const char *fname;
|
---|
| 1440 | int i;
|
---|
| 1441 |
|
---|
| 1442 | /* Ensure this is a locking_key record. */
|
---|
| 1443 | if (rec->key.dsize != sizeof(struct file_id))
|
---|
| 1444 | return 0;
|
---|
| 1445 |
|
---|
| 1446 | data = (struct locking_data *)rec->value.dptr;
|
---|
| 1447 | shares = (struct share_mode_entry *)(rec->value.dptr + sizeof(*data));
|
---|
| 1448 | sharepath = (const char *)rec->value.dptr + sizeof(*data) +
|
---|
| 1449 | data->u.s.num_share_mode_entries*sizeof(*shares) +
|
---|
| 1450 | data->u.s.delete_token_size;
|
---|
| 1451 | fname = (const char *)rec->value.dptr + sizeof(*data) +
|
---|
| 1452 | data->u.s.num_share_mode_entries*sizeof(*shares) +
|
---|
| 1453 | data->u.s.delete_token_size +
|
---|
| 1454 | strlen(sharepath) + 1;
|
---|
| 1455 |
|
---|
| 1456 | for (i=0;i<data->u.s.num_share_mode_entries;i++) {
|
---|
| 1457 | state->fn(&shares[i], sharepath, fname,
|
---|
| 1458 | state->private_data);
|
---|
| 1459 | }
|
---|
| 1460 | return 0;
|
---|
| 1461 | }
|
---|
| 1462 |
|
---|
| 1463 | /*******************************************************************
|
---|
| 1464 | Call the specified function on each entry under management by the
|
---|
| 1465 | share mode system.
|
---|
| 1466 | ********************************************************************/
|
---|
| 1467 |
|
---|
| 1468 | int share_mode_forall(void (*fn)(const struct share_mode_entry *, const char *,
|
---|
| 1469 | const char *, void *),
|
---|
| 1470 | void *private_data)
|
---|
| 1471 | {
|
---|
| 1472 | struct forall_state state;
|
---|
| 1473 |
|
---|
| 1474 | if (lock_db == NULL)
|
---|
| 1475 | return 0;
|
---|
| 1476 |
|
---|
| 1477 | state.fn = fn;
|
---|
| 1478 | state.private_data = private_data;
|
---|
| 1479 |
|
---|
| 1480 | return lock_db->traverse_read(lock_db, traverse_fn, (void *)&state);
|
---|
| 1481 | }
|
---|