summaryrefslogtreecommitdiff
path: root/libfshelp/rlock-tweak.c
blob: 8be8899386e3358369042cb701bc8d141752f2e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
   Copyright (C) 2001, 2014-2019 Free Software Foundation

   Written by Neal H Walfield <neal@cs.uml.edu>

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   The GNU Hurd is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with the GNU Hurd.  If not, see <http://www.gnu.org/licenses/>.  */

#include "fshelp.h"
#include "rlock.h"

#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <hurd.h>
#include <hurd/process.h>

static inline long overlap (loff_t start, loff_t len, struct rlock_list *l)
{
  return ((len == 0 && l->len == 0)
	  || (len == 0 && l->start + l->len > start)
	  || (l->len == 0 && start + len > l->start)
	  || (l->start + l->len > start && start + len > l->start));
}

error_t
fshelp_rlock_tweak (struct rlock_box *box, pthread_mutex_t *mutex,
		    struct rlock_peropen *po, int open_mode,
		    loff_t obj_size, loff_t cur_pointer, int cmd,
		    struct flock64 *lock, mach_port_t rendezvous)
{
  inline struct rlock_list *
  gen_lock (loff_t start, loff_t len, int type)
    {
      struct rlock_list *l = malloc (sizeof (struct rlock_list));
      if (! l)
        return NULL;

      rlock_list_init (po, l);
      l->start = start;
      l->len = len;
      l->type = type;

      list_link (po, po->locks, l);
      list_link (node, &box->locks, l);
      return l;
    }

  inline void
  rele_lock (struct rlock_list *l, int wake_waiters)
    {
      list_unlink (po, l);
      list_unlink (node, l);

      if (wake_waiters && l->waiting)
	pthread_cond_broadcast (&l->wait);

      pthread_cond_destroy(&l->wait);
      free (l);
    }

  error_t
  unlock_region (loff_t start, loff_t len)
    {
      struct rlock_list *l;

      for (l = *po->locks; l; l = l->po.next)
	{
	  if (l->len != 0 && l->start + l->len <= start)
	    /* We start after the locked region ends.  */
	    {
	      continue;
	    }
	  else if (len != 0 && start + len <= l->start)
	    /* We end before this region starts.  Since we are sorted,
	       we are done.  */
	    {
	      return 0;
	    }
	  else if (start <= l->start
		   && (len == 0
		       || (l->len != 0
			   && l->start + l->len <= start + len)))
	    /* We wrap the locked region; consume it.  */
	    {
	      rele_lock (l, 1);
	      continue;
	    }
	  else if (start <= l->start
		   && (l->len == 0
		       || (l->start < start + len)))
	    /* The locked region is having its head unlocked.  */
	    {
	      assert (len != 0);
	      assert (l->len == 0 || start + len < l->start + l->len);

	      if (l->len != 0)
		l->len -= start + len - l->start;
	      l->start = start + len;

	      if (l->waiting)
		{
		  l->waiting = 0;
		  pthread_cond_broadcast (&l->wait);
		}
	    }
	  else if (l->start < start
		   && ((start < l->start + l->len
		        && (len == 0 || l->start + l->len <= start + len))
		       || (len == 0 && l->len == 0)))
	    /* The locked region needs its tail unlocked.  */
	    {
	      assert (len == 0
		      || (l->len != 0 && l->start + l->len <= start + len));

	      l->len = start - l->start;

	      if (l->waiting)
		{
		  l->waiting = 0;
		  pthread_cond_broadcast (&l->wait);
		}

	      continue;
	    }
	  else if (l->start < start
		   && (l->len == 0
		       || (len != 0
			   && start + len < l->start + l->len)))
	    /* The locked region wraps us (absolutely); crave out the
	       middle.  */
	    {
	      struct rlock_list *upper_half;

	      assert (len != 0);

	      upper_half = gen_lock (start + len,
				     l->len
				       ? l->start + l->len - (start + len)
				       : 0,
				     l->type);
	      if (! upper_half)
		return ENOMEM;

	      l->len = start - l->start;

	      return 0;
	    }
	  else if (start < l->start
		   && len != 0
		   && start + len <= l->start)
	    /* The locked region starts after our end.  */
	    {
	      return 0;
	    }
	  else
	    assert (! "Impossible!");
	}

      return 0;
    }

  inline struct rlock_list *
  find_conflict (loff_t start, loff_t len, int type)
    {
      struct rlock_list *l;

      for (l = box->locks; l; l = l->node.next)
	{
	  if (po->locks == l->po_id)
	    continue;

	  if ((l->type == F_WRLCK || type == F_WRLCK)
	      && overlap (start, len, l))
	    return l;
	}

      return NULL;
    }

  inline error_t
  merge_in (loff_t start, loff_t len, int type)
    {
      struct rlock_list *l;

      for (l = *po->locks; l; l = l->po.next)
	{
	  if (l->start <= start
	      && (l->len == 0
		  || (len != 0
		      && start + len <= l->start + l->len)))
	    /* Our start and end fall between the locked region
	       (i.e. we are wrapped).  */
	    {
	      struct rlock_list *head = NULL;
	      struct rlock_list *tail = NULL;

	      if (type == l->type || type == F_RDLCK)
		return 0;

	      assert (type == F_WRLCK && l->type == F_RDLCK);

	      if (l->start < start)
	        /* We need to split the head off.  */
		{
		  head = gen_lock (l->start, start - l->start, F_RDLCK);
		  if (! head)
		    return ENOMEM;
		}

	      if ((l->len == 0 && len != 0)
		  || start + len < l->start + l->len)
		/* We need to split the tail off.  */
	        {
		  tail = gen_lock (start + len,
				   l->len
				     ? l->start + l->len - (start + len)
				     : 0,
				   F_RDLCK);
		  if (! tail)
		    {
		      if (head)
			rele_lock (head, 0);
		      return ENOMEM;
		    }
		}

	      if (head)
		{
		  loff_t shift = start - l->start;

		  if (l->len != 0)
		    l->len -= shift;
		  l->start += shift;
		}

	      if (tail)
	        l->len = tail->start - l->start;

	      if (! tail)
		/* There is a chance we can merge some more.  */
	        {
		  start = l->start;
		  len = l->len;

		  rele_lock (l, 1);
		  continue;
		}
	      else
	        {
	          l->type = F_WRLCK;
		  return 0;
		}
	    }
	  else if (start <= l->start
		   && (len == 0
		       || (l->len != 0
			   && l->start + l->len <= start + len)))
	    /* We fully wrap the locked region.  */
	    {
	      struct rlock_list *head;

	      if (type == l->type || type == F_WRLCK)
		{
		  rele_lock (l, 1);
		  /* Try to merge more.  */
		  continue;
		}

	      assert (type == F_RDLCK && l->type == F_WRLCK);

	      if (start < l->start)
		/* Generate our head.  */
		{
		  head = gen_lock (start, l->start - start, F_RDLCK);
		  if (! head)
		    return ENOMEM;
		}
	      else
		head = NULL;

	      if (l->len != 0
		  && (len == 0 || l->start + l->len < start + len))
		/* We have a tail, try to merge it also.  */
		{
		  if (len != 0)
		    len = start + len - (l->start + l->len);
		  start = l->start + l->len;

		  continue;
		}
	      else
		/* Our end is silently consumed.  */
	        {
		  /* If we do not have a tail, we must have had a head
		     (if not, the first case would have caught us).  */
		  assert (head);
	          return 0;
		}
	    }
	  else if (l->start < start && start <= l->start + l->len
		   && (len == 0 || start + len > l->start + l->len))
	    /* Our start falls within the locked region or exactly one
	       byte after it and our end falls beyond it.  We know that
	       we cannot consume the entire region.  */
	    {
	      assert (l->len != 0);

	      if (type == l->type)
		/* Merge the two areas.  */
		{
		  if (len != 0)
		    len += start - l->start;
		  start = l->start;

		  rele_lock (l, 1);

		  /* Try to merge in more.  */
		  continue;
		}
	      else if (start == l->start + l->len)
		  /* We fall just after the locked region (there is no
		     intersection) and we are not the same type.  */
		{
		  /* The is nothing to do except continue the search.  */
		  continue;
		}
	      else if (type == F_WRLCK)
		/* We comsume the intersection.  */
		{
		  assert (l->type == F_RDLCK);

		  l->len -= l->start + l->len - start;

		  /* Don't create the lock now; we might be able to
		     consume more locks.  */
		  continue;
		}
	      else
		/* We are dominated; the locked region comsumes the
		   intersection.  */
		{
		  loff_t common = l->start + l->len - start;

		  assert (type == F_RDLCK);
		  assert (l->type == F_WRLCK);

		  start += common;
		  if (len != 0)
		    len -= common;

		  /* There is still a chance that we can consume more
		     locks.  */
		  continue;
		}
	    }
	  else if (start < l->start
		   && (l->len == 0
		       || l->start <= start + len))
	    /* Our start falls before the locked region and our
	       end falls (inclusively) between it or one byte before it.
	       Note, we know that we do not consume the entire locked
	       area.  */
	    {
	      assert (len != 0);
	      assert (l->len == 0 || start + len < l->start + l->len);

	      if (type == l->type)
		/* Merge the two areas.  */
		{
		  if (l->len)
		    l->len += l->start - start;
		  l->start = start;
		  return 0;
		}
	      else if (l->start == start + len)
		/* Our end falls just before the start of the locked
		   region, however, we are not the same type.  Just
		   insert it.  */
		{
		  continue;
		}
	      else if (type == F_WRLCK)
		/* We consume the intersection.  */
		{
		  struct rlock_list *e;
		  loff_t common = start + len - l->start;

		  assert (l->type == F_RDLCK);

		  e = gen_lock (start, len, F_WRLCK);
		  if (! e)
		    return ENOMEM;

		  if (l->len)
		    l->len -= common;
		  l->start += common;

		  return 0;
		}
	      else
		/* The locked region comsumes the intersection.  */
		{
		  struct rlock_list *e;

		  assert (l->type == F_WRLCK);
		  assert (type == F_RDLCK);

		  e = gen_lock (start, l->start - start, F_RDLCK);
		  if (! e)
		    return ENOMEM;

		  return 0;
		}
	    }
	  else if (start < l->start
		   && len != 0
		   && start + len <= l->start)
	    /* We start and end before this locked region.  Therefore,
	       knowing that the list is sorted, the merge is done.  */
	    {
	      break;
	    }
	  else
	    /* We start beyond the end of this locked region.  */
	    {
	      assert (start >= l->start + l->len);
	      assert (l->len != 0);
	      continue;
	    }
	}

      return (gen_lock (start, len, type) ? 0 : ENOMEM);
    }

  struct rlock_list *e;
  loff_t start;
  loff_t len;

  if (rendezvous != MACH_PORT_NULL)
    return EOPNOTSUPP;

  if (cmd != F_GETLK64
      && cmd != F_SETLK64
      && cmd != F_SETLKW64)
    return EOPNOTSUPP;

  if (lock->l_type != F_UNLCK
      && lock->l_type != F_RDLCK
      && lock->l_type != F_WRLCK)
    return EINVAL;

  if (lock->l_type == F_UNLCK)
    {
      if (cmd == F_SETLKW64)
        /* If we are unlocking a region, map F_SETLKW64 to F_SETLK64.  */
        cmd = F_SETLK64;
      else if (cmd == F_GETLK64)
	/* Impossible!  */
	return EINVAL;
    }

  /* From POSIX-1003.1: A request for an exclusive lock shall fail if
     the file descriptor was not opened with write access. */
  if ((cmd == F_SETLK64 || cmd == F_SETLKW64 )
      && lock->l_type == F_WRLCK && !(open_mode & O_WRITE))
    return EBADF;

  switch (lock->l_whence)
    {
    case SEEK_SET:
      start = lock->l_start;
      break;

    case SEEK_CUR:
      start = cur_pointer + lock->l_start;
      break;

    case SEEK_END:
      start = obj_size + lock->l_start;
      break;

    default:
      return EINVAL;
    }

  if (start < 0)
    return EINVAL;

  len = lock->l_len;
  if (len < 0)
    return EINVAL;

  if (cmd == F_SETLK64 && lock->l_type == F_UNLCK)
    return unlock_region (start, len);

retry:
  e = find_conflict (start, len, lock->l_type);

  if (cmd == F_GETLK64)
    {
      if (e)
	{
	  lock->l_type = e->type;
	  lock->l_start = e->start;
	  lock->l_whence = SEEK_SET;
	  lock->l_len = e->len;
	  /* XXX: This will be fixed when the proc_{user,server}_identify
             RPCs are implemented */
	  lock->l_pid = -1;
	  return 0;
	}
      else
	{
	  lock->l_type = F_UNLCK;
	  return 0;
	}
    }
  else
    {
      assert (cmd == F_SETLK64 || cmd == F_SETLKW64);

      if (! e)
	return merge_in (start, len, lock->l_type);
      else
        {
	  if (cmd == F_SETLKW64)
	    {
	      e->waiting = 1;
	      if (pthread_hurd_cond_wait_np (&e->wait, mutex))
	        return EINTR;
	      goto retry;
	    }
	  else
	    return EAGAIN;
	}
    }
}