This source file includes following definitions.
- fcntl_init_locks
- fcntl_getlk
- fcntl_setlk
- fcntl_remove_locks
- copy_flock
- conflict
- overlap
- lock_it
- unlock_it
- alloc_lock
- free_lock
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <asm/segment.h>
14
15 #include <linux/sched.h>
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/stat.h>
19 #include <linux/fcntl.h>
20
21 #define OFFSET_MAX 0x7fffffff
22
23 static int copy_flock(struct file *filp, struct file_lock *fl, struct flock *l);
24 static int conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
25 static int overlap(struct file_lock *fl1, struct file_lock *fl2);
26 static int lock_it(struct file *filp, struct file_lock *caller);
27 static int unlock_it(struct file *filp, struct file_lock *caller);
28 static struct file_lock *alloc_lock(struct file *filp, struct file_lock *template);
29 static void free_lock(struct file *filp, struct file_lock *fl);
30
31 static struct file_lock file_lock_table[NR_FILE_LOCKS];
32 static struct file_lock *file_lock_free_list;
33
34
35
36
37
38 void fcntl_init_locks(void)
39 {
40 struct file_lock *fl;
41
42 for (fl = &file_lock_table[0]; fl < file_lock_table + NR_FILE_LOCKS - 1; fl++) {
43 fl->fl_next = fl + 1;
44 fl->fl_owner = NULL;
45 }
46 file_lock_table[NR_FILE_LOCKS - 1].fl_next = NULL;
47 file_lock_table[NR_FILE_LOCKS - 1].fl_owner = NULL;
48 file_lock_free_list = &file_lock_table[0];
49 }
50
51 int fcntl_getlk(unsigned int fd, struct flock *l)
52 {
53 struct flock flock;
54 struct file *filp;
55 struct file_lock *fl,file_lock;
56
57 if (fd >= NR_OPEN || !(filp = current->filp[fd]))
58 return -EBADF;
59 verify_area(l, sizeof(*l));
60 memcpy_fromfs(&flock, l, sizeof(flock));
61 if (flock.l_type == F_UNLCK)
62 return -EINVAL;
63 if (!copy_flock(filp, &file_lock, &flock))
64 return -EINVAL;
65
66 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
67 if (conflict(&file_lock, fl)) {
68 flock.l_pid = fl->fl_owner->pid;
69 flock.l_start = fl->fl_start;
70 flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
71 fl->fl_end - fl->fl_start + 1;
72 flock.l_whence = fl->fl_whence;
73 flock.l_type = fl->fl_type;
74 memcpy_tofs(l, &flock, sizeof(flock));
75 return 0;
76 }
77 }
78
79 flock.l_type = F_UNLCK;
80 memcpy_tofs(l, &flock, sizeof(flock));
81 return 0;
82 }
83
84
85
86
87
88 int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
89 {
90 struct file *filp;
91 struct file_lock *fl,file_lock;
92 struct flock flock;
93
94
95
96
97
98 if (fd >= NR_OPEN || !(filp = current->filp[fd]))
99 return -EBADF;
100 verify_area(l, sizeof(*l));
101 memcpy_fromfs(&flock, l, sizeof(flock));
102 if (!copy_flock(filp, &file_lock, &flock))
103 return -EINVAL;
104 switch (file_lock.fl_type) {
105 case F_RDLCK :
106 if (!(filp->f_mode & 1))
107 return -EBADF;
108 break;
109 case F_WRLCK :
110 if (!(filp->f_mode & 2))
111 return -EBADF;
112 break;
113 case F_SHLCK :
114 if (!(filp->f_mode & 3))
115 return -EBADF;
116 file_lock.fl_type = F_RDLCK;
117 break;
118 case F_EXLCK :
119 if (!(filp->f_mode & 3))
120 return -EBADF;
121 file_lock.fl_type = F_WRLCK;
122 break;
123 case F_UNLCK :
124 break;
125 }
126
127
128
129
130
131 if (file_lock.fl_type == F_UNLCK)
132 return unlock_it(filp, &file_lock);
133
134
135
136
137
138 repeat:
139 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
140 if (!conflict(&file_lock, fl))
141 continue;
142
143
144
145
146
147 if (cmd == F_SETLKW) {
148 interruptible_sleep_on(&fl->fl_wait);
149 goto repeat;
150 }
151 return -EAGAIN;
152 }
153
154
155
156
157
158 return lock_it(filp, &file_lock);
159 }
160
161
162
163
164
165 void fcntl_remove_locks(struct task_struct *task, struct file *filp)
166 {
167 struct file_lock *fl,*next;
168
169 for (fl = filp->f_inode->i_flock; fl != NULL; ) {
170
171
172
173
174 next = fl->fl_next;
175 if (fl->fl_owner == task)
176 free_lock(filp, fl);
177 fl = next;
178 }
179 }
180
181
182
183
184
185
186 static int copy_flock(struct file *filp, struct file_lock *fl, struct flock *l)
187 {
188 off_t start;
189
190 if (!filp->f_inode)
191 return 0;
192 if (!S_ISREG(filp->f_inode->i_mode))
193 return 0;
194 if (l->l_type != F_UNLCK && l->l_type != F_RDLCK && l->l_type != F_WRLCK)
195 return 0;
196 switch (l->l_whence) {
197 case 0 : start = 0; break;
198 case 1 : start = filp->f_pos; break;
199 case 2 : start = filp->f_inode->i_size; break;
200 default : return 0;
201 }
202 if ((start += l->l_start) < 0 || l->l_len < 0)
203 return 0;
204 fl->fl_type = l->l_type;
205 fl->fl_start = start;
206 fl->fl_whence = 0;
207 if (l->l_len == 0 || (fl->fl_end = start + l->l_len - 1) < 0)
208 fl->fl_end = OFFSET_MAX;
209 fl->fl_owner = current;
210 fl->fl_wait = NULL;
211 return 1;
212 }
213
214
215
216
217
218 static int conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
219 {
220 if (caller_fl->fl_owner == sys_fl->fl_owner)
221 return 0;
222 if (!overlap(caller_fl, sys_fl))
223 return 0;
224 switch (caller_fl->fl_type) {
225 case F_RDLCK :
226 return sys_fl->fl_type != F_RDLCK;
227 case F_WRLCK :
228 return 1;
229 }
230 return 0;
231 }
232
233 static int overlap(struct file_lock *fl1, struct file_lock *fl2)
234 {
235 if (fl1->fl_start <= fl2->fl_start) {
236 return fl1->fl_end >= fl2->fl_start;
237 } else {
238 return fl2->fl_end >= fl1->fl_start;
239 }
240 }
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256 static int lock_it(struct file *filp, struct file_lock *caller)
257 {
258 struct file_lock *fl,*new;
259
260
261
262
263
264
265
266
267
268 if ((caller = alloc_lock(filp, caller)) == NULL)
269 return -ENOLCK;
270
271
272
273
274
275 for (fl = filp->f_inode->i_flock; fl != NULL; fl = fl->fl_next) {
276 if (caller->fl_owner != fl->fl_owner)
277 continue;
278 if (caller == fl)
279 continue;
280 if (!overlap(caller, fl)) {
281
282
283
284 if (caller->fl_type != fl->fl_type)
285 continue;
286 if (caller->fl_end + 1 == fl->fl_start) {
287 fl->fl_start = caller->fl_start;
288 free_lock(filp, caller);
289 caller = fl;
290
291 } else if (caller->fl_start - 1 == fl->fl_end) {
292 fl->fl_end = caller->fl_end;
293 free_lock(filp, caller);
294 caller = fl;
295
296 }
297 continue;
298 }
299
300
301
302
303 if (caller->fl_type != fl->fl_type) {
304 if (caller->fl_start > fl->fl_start && caller->fl_end < fl->fl_end) {
305
306
307
308
309
310 if ((new = alloc_lock(filp, fl)) == NULL) {
311 free_lock(filp, caller);
312 return -ENOLCK;
313 }
314 fl->fl_end = caller->fl_start - 1;
315 new->fl_start = caller->fl_end + 1;
316 return 0;
317 }
318 if (caller->fl_start <= fl->fl_start && caller->fl_end >= fl->fl_end) {
319
320
321
322 free_lock(filp, fl);
323 return 0;
324 }
325 if (caller->fl_end < fl->fl_end) {
326 fl->fl_start = caller->fl_end + 1;
327
328 } else if (caller->fl_start > fl->fl_start) {
329 fl->fl_end = caller->fl_start - 1;
330
331 } else {
332 printk("lock_it: program bug: unanticipated overlap\n");
333 free_lock(filp, caller);
334 return -ENOLCK;
335 }
336 } else {
337 int grew = 0;
338
339 if (caller->fl_start < fl->fl_start) {
340 fl->fl_start = caller->fl_start;
341 grew = 1;
342 }
343 if (caller->fl_end > fl->fl_end) {
344 fl->fl_end = caller->fl_end;
345 grew = 1;
346 }
347 free_lock(filp, caller);
348 caller = fl;
349 if (!grew)
350 return 0;
351
352 }
353 }
354
355
356
357
358
359
360 return 0;
361 }
362
363
364
365
366
367
368
369 static int unlock_it(struct file *filp, struct file_lock *caller)
370 {
371 int one_unlocked = 0;
372 struct file_lock *fl,*next;
373
374 for (fl = filp->f_inode->i_flock; fl != NULL; ) {
375 if (caller->fl_owner != fl->fl_owner || !overlap(caller, fl)) {
376 fl = fl->fl_next;
377 continue;
378 }
379 one_unlocked = 1;
380 if (caller->fl_start > fl->fl_start && caller->fl_end < fl->fl_end) {
381
382
383
384
385 if ((next = alloc_lock(filp, fl)) == NULL)
386 return -ENOLCK;
387 fl->fl_end = caller->fl_start - 1;
388 next->fl_start = caller->fl_end + 1;
389 return 0;
390 }
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408 next = fl->fl_next;
409 if (caller->fl_start <= fl->fl_start && caller->fl_end >= fl->fl_end) {
410 free_lock(filp, fl);
411 } else if (caller->fl_start > fl->fl_start) {
412 fl->fl_end = caller->fl_start - 1;
413 } else {
414
415 fl->fl_start = caller->fl_end + 1;
416 }
417 if (caller->fl_start >= fl->fl_start && caller->fl_end <= fl->fl_end)
418 return 0;
419 fl = next;
420
421 }
422
423 return one_unlocked ? 0 : -EINVAL;
424 }
425
426 static struct file_lock *alloc_lock(struct file *filp, struct file_lock *template)
427 {
428 struct file_lock *new;
429
430 if (file_lock_free_list == NULL)
431 return NULL;
432 if (file_lock_free_list->fl_owner != NULL)
433 panic("alloc_lock: broken free list\n");
434
435 new = file_lock_free_list;
436 file_lock_free_list = file_lock_free_list->fl_next;
437
438 *new = *template;
439
440 new->fl_next = filp->f_inode->i_flock;
441 filp->f_inode->i_flock = new;
442
443 new->fl_owner = current;
444 new->fl_wait = NULL;
445 return new;
446 }
447
448
449
450
451
452
453
454 static void free_lock(struct file *filp, struct file_lock *fl)
455 {
456 struct file_lock **fl_p;
457
458 if (fl->fl_owner == NULL)
459 panic("free_lock: broken lock list\n");
460
461
462
463
464
465
466 for (fl_p = &filp->f_inode->i_flock; *fl_p != NULL; fl_p = &(*fl_p)->fl_next) {
467 if (*fl_p == fl)
468 break;
469 }
470 if (*fl_p == NULL) {
471 printk("free_lock: lock is not in file's lock list\n");
472 } else {
473 *fl_p = (*fl_p)->fl_next;
474 }
475
476 fl->fl_next = file_lock_free_list;
477 file_lock_free_list = fl;
478 fl->fl_owner = NULL;
479
480 wake_up(&fl->fl_wait);
481 }