aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/md/raid5.c
diff options
context:
space:
mode:
authorNeilBrown <neilb@cse.unsw.edu.au>2005-09-09 19:23:54 -0400
committerLinus Torvalds <torvalds@g5.osdl.org>2005-09-09 19:39:12 -0400
commit72626685dc66d455742a7f215a0535c551628b9e (patch)
tree91e19a61a5a3b782007132b6b2e353e8936dd656 /drivers/md/raid5.c
parent0002b2718dd04da67c21f8a7830de8d95a9b0345 (diff)
[PATCH] md: add write-intent-bitmap support to raid5
Most awkward part of this is delaying write requests until bitmap updates have been flushed. To achieve this, we have a sequence number (seq_flush) which is incremented each time the raid5 is unplugged. If the raid thread notices that this has changed, it flushes bitmap changes, and assigned the value of seq_flush to seq_write. When a write request arrives, it is given the number from seq_write, and that write request may not complete until seq_flush is larger than the saved seq number. We have a new queue for storing stripes which are waiting for a bitmap flush and an extra flag for stripes to record if the write was 'degraded' and so should not clear the a bit in the bitmap. Signed-off-by: Neil Brown <neilb@cse.unsw.edu.au> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'drivers/md/raid5.c')
-rw-r--r--drivers/md/raid5.c133
1 files changed, 123 insertions, 10 deletions
diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c
index ed859e08d600..4683ca24c046 100644
--- a/drivers/md/raid5.c
+++ b/drivers/md/raid5.c
@@ -24,6 +24,8 @@
24#include <linux/bitops.h> 24#include <linux/bitops.h>
25#include <asm/atomic.h> 25#include <asm/atomic.h>
26 26
27#include <linux/raid/bitmap.h>
28
27/* 29/*
28 * Stripe cache 30 * Stripe cache
29 */ 31 */
@@ -79,8 +81,13 @@ static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
79 if (test_bit(STRIPE_HANDLE, &sh->state)) { 81 if (test_bit(STRIPE_HANDLE, &sh->state)) {
80 if (test_bit(STRIPE_DELAYED, &sh->state)) 82 if (test_bit(STRIPE_DELAYED, &sh->state))
81 list_add_tail(&sh->lru, &conf->delayed_list); 83 list_add_tail(&sh->lru, &conf->delayed_list);
82 else 84 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
85 conf->seq_write == sh->bm_seq)
86 list_add_tail(&sh->lru, &conf->bitmap_list);
87 else {
88 clear_bit(STRIPE_BIT_DELAY, &sh->state);
83 list_add_tail(&sh->lru, &conf->handle_list); 89 list_add_tail(&sh->lru, &conf->handle_list);
90 }
84 md_wakeup_thread(conf->mddev->thread); 91 md_wakeup_thread(conf->mddev->thread);
85 } else { 92 } else {
86 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { 93 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
@@ -244,6 +251,9 @@ static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector
244 spin_lock_irq(&conf->device_lock); 251 spin_lock_irq(&conf->device_lock);
245 252
246 do { 253 do {
254 wait_event_lock_irq(conf->wait_for_stripe,
255 conf->quiesce == 0,
256 conf->device_lock, /* nothing */);
247 sh = __find_stripe(conf, sector); 257 sh = __find_stripe(conf, sector);
248 if (!sh) { 258 if (!sh) {
249 if (!conf->inactive_blocked) 259 if (!conf->inactive_blocked)
@@ -803,6 +813,7 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
803{ 813{
804 struct bio **bip; 814 struct bio **bip;
805 raid5_conf_t *conf = sh->raid_conf; 815 raid5_conf_t *conf = sh->raid_conf;
816 int firstwrite=0;
806 817
807 PRINTK("adding bh b#%llu to stripe s#%llu\n", 818 PRINTK("adding bh b#%llu to stripe s#%llu\n",
808 (unsigned long long)bi->bi_sector, 819 (unsigned long long)bi->bi_sector,
@@ -811,9 +822,11 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
811 822
812 spin_lock(&sh->lock); 823 spin_lock(&sh->lock);
813 spin_lock_irq(&conf->device_lock); 824 spin_lock_irq(&conf->device_lock);
814 if (forwrite) 825 if (forwrite) {
815 bip = &sh->dev[dd_idx].towrite; 826 bip = &sh->dev[dd_idx].towrite;
816 else 827 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
828 firstwrite = 1;
829 } else
817 bip = &sh->dev[dd_idx].toread; 830 bip = &sh->dev[dd_idx].toread;
818 while (*bip && (*bip)->bi_sector < bi->bi_sector) { 831 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
819 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) 832 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
@@ -836,6 +849,13 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
836 (unsigned long long)bi->bi_sector, 849 (unsigned long long)bi->bi_sector,
837 (unsigned long long)sh->sector, dd_idx); 850 (unsigned long long)sh->sector, dd_idx);
838 851
852 if (conf->mddev->bitmap && firstwrite) {
853 sh->bm_seq = conf->seq_write;
854 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
855 STRIPE_SECTORS, 0);
856 set_bit(STRIPE_BIT_DELAY, &sh->state);
857 }
858
839 if (forwrite) { 859 if (forwrite) {
840 /* check if page is covered */ 860 /* check if page is covered */
841 sector_t sector = sh->dev[dd_idx].sector; 861 sector_t sector = sh->dev[dd_idx].sector;
@@ -958,12 +978,13 @@ static void handle_stripe(struct stripe_head *sh)
958 * need to be failed 978 * need to be failed
959 */ 979 */
960 if (failed > 1 && to_read+to_write+written) { 980 if (failed > 1 && to_read+to_write+written) {
961 spin_lock_irq(&conf->device_lock);
962 for (i=disks; i--; ) { 981 for (i=disks; i--; ) {
982 int bitmap_end = 0;
983 spin_lock_irq(&conf->device_lock);
963 /* fail all writes first */ 984 /* fail all writes first */
964 bi = sh->dev[i].towrite; 985 bi = sh->dev[i].towrite;
965 sh->dev[i].towrite = NULL; 986 sh->dev[i].towrite = NULL;
966 if (bi) to_write--; 987 if (bi) { to_write--; bitmap_end = 1; }
967 988
968 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) 989 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
969 wake_up(&conf->wait_for_overlap); 990 wake_up(&conf->wait_for_overlap);
@@ -981,6 +1002,7 @@ static void handle_stripe(struct stripe_head *sh)
981 /* and fail all 'written' */ 1002 /* and fail all 'written' */
982 bi = sh->dev[i].written; 1003 bi = sh->dev[i].written;
983 sh->dev[i].written = NULL; 1004 sh->dev[i].written = NULL;
1005 if (bi) bitmap_end = 1;
984 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { 1006 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
985 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); 1007 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
986 clear_bit(BIO_UPTODATE, &bi->bi_flags); 1008 clear_bit(BIO_UPTODATE, &bi->bi_flags);
@@ -1009,8 +1031,11 @@ static void handle_stripe(struct stripe_head *sh)
1009 bi = nextbi; 1031 bi = nextbi;
1010 } 1032 }
1011 } 1033 }
1034 spin_unlock_irq(&conf->device_lock);
1035 if (bitmap_end)
1036 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1037 STRIPE_SECTORS, 0, 0);
1012 } 1038 }
1013 spin_unlock_irq(&conf->device_lock);
1014 } 1039 }
1015 if (failed > 1 && syncing) { 1040 if (failed > 1 && syncing) {
1016 md_done_sync(conf->mddev, STRIPE_SECTORS,0); 1041 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
@@ -1038,6 +1063,7 @@ static void handle_stripe(struct stripe_head *sh)
1038 test_bit(R5_UPTODATE, &dev->flags) ) { 1063 test_bit(R5_UPTODATE, &dev->flags) ) {
1039 /* We can return any write requests */ 1064 /* We can return any write requests */
1040 struct bio *wbi, *wbi2; 1065 struct bio *wbi, *wbi2;
1066 int bitmap_end = 0;
1041 PRINTK("Return write for disc %d\n", i); 1067 PRINTK("Return write for disc %d\n", i);
1042 spin_lock_irq(&conf->device_lock); 1068 spin_lock_irq(&conf->device_lock);
1043 wbi = dev->written; 1069 wbi = dev->written;
@@ -1051,7 +1077,13 @@ static void handle_stripe(struct stripe_head *sh)
1051 } 1077 }
1052 wbi = wbi2; 1078 wbi = wbi2;
1053 } 1079 }
1080 if (dev->towrite == NULL)
1081 bitmap_end = 1;
1054 spin_unlock_irq(&conf->device_lock); 1082 spin_unlock_irq(&conf->device_lock);
1083 if (bitmap_end)
1084 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1085 STRIPE_SECTORS,
1086 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1055 } 1087 }
1056 } 1088 }
1057 } 1089 }
@@ -1175,7 +1207,8 @@ static void handle_stripe(struct stripe_head *sh)
1175 } 1207 }
1176 } 1208 }
1177 /* now if nothing is locked, and if we have enough data, we can start a write request */ 1209 /* now if nothing is locked, and if we have enough data, we can start a write request */
1178 if (locked == 0 && (rcw == 0 ||rmw == 0)) { 1210 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1211 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1179 PRINTK("Computing parity...\n"); 1212 PRINTK("Computing parity...\n");
1180 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); 1213 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1181 /* now every locked buffer is ready to be written */ 1214 /* now every locked buffer is ready to be written */
@@ -1231,6 +1264,7 @@ static void handle_stripe(struct stripe_head *sh)
1231 dev = &sh->dev[failed_num]; 1264 dev = &sh->dev[failed_num];
1232 set_bit(R5_LOCKED, &dev->flags); 1265 set_bit(R5_LOCKED, &dev->flags);
1233 set_bit(R5_Wantwrite, &dev->flags); 1266 set_bit(R5_Wantwrite, &dev->flags);
1267 clear_bit(STRIPE_DEGRADED, &sh->state);
1234 locked++; 1268 locked++;
1235 set_bit(STRIPE_INSYNC, &sh->state); 1269 set_bit(STRIPE_INSYNC, &sh->state);
1236 set_bit(R5_Syncio, &dev->flags); 1270 set_bit(R5_Syncio, &dev->flags);
@@ -1298,6 +1332,8 @@ static void handle_stripe(struct stripe_head *sh)
1298 bi->bi_next = NULL; 1332 bi->bi_next = NULL;
1299 generic_make_request(bi); 1333 generic_make_request(bi);
1300 } else { 1334 } else {
1335 if (rw == 1)
1336 set_bit(STRIPE_DEGRADED, &sh->state);
1301 PRINTK("skip op %ld on disc %d for sector %llu\n", 1337 PRINTK("skip op %ld on disc %d for sector %llu\n",
1302 bi->bi_rw, i, (unsigned long long)sh->sector); 1338 bi->bi_rw, i, (unsigned long long)sh->sector);
1303 clear_bit(R5_LOCKED, &sh->dev[i].flags); 1339 clear_bit(R5_LOCKED, &sh->dev[i].flags);
@@ -1322,6 +1358,20 @@ static inline void raid5_activate_delayed(raid5_conf_t *conf)
1322 } 1358 }
1323} 1359}
1324 1360
1361static inline void activate_bit_delay(raid5_conf_t *conf)
1362{
1363 /* device_lock is held */
1364 struct list_head head;
1365 list_add(&head, &conf->bitmap_list);
1366 list_del_init(&conf->bitmap_list);
1367 while (!list_empty(&head)) {
1368 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1369 list_del_init(&sh->lru);
1370 atomic_inc(&sh->count);
1371 __release_stripe(conf, sh);
1372 }
1373}
1374
1325static void unplug_slaves(mddev_t *mddev) 1375static void unplug_slaves(mddev_t *mddev)
1326{ 1376{
1327 raid5_conf_t *conf = mddev_to_conf(mddev); 1377 raid5_conf_t *conf = mddev_to_conf(mddev);
@@ -1354,8 +1404,10 @@ static void raid5_unplug_device(request_queue_t *q)
1354 1404
1355 spin_lock_irqsave(&conf->device_lock, flags); 1405 spin_lock_irqsave(&conf->device_lock, flags);
1356 1406
1357 if (blk_remove_plug(q)) 1407 if (blk_remove_plug(q)) {
1408 conf->seq_flush++;
1358 raid5_activate_delayed(conf); 1409 raid5_activate_delayed(conf);
1410 }
1359 md_wakeup_thread(mddev->thread); 1411 md_wakeup_thread(mddev->thread);
1360 1412
1361 spin_unlock_irqrestore(&conf->device_lock, flags); 1413 spin_unlock_irqrestore(&conf->device_lock, flags);
@@ -1493,10 +1545,20 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
1493 sector_t first_sector; 1545 sector_t first_sector;
1494 int raid_disks = conf->raid_disks; 1546 int raid_disks = conf->raid_disks;
1495 int data_disks = raid_disks-1; 1547 int data_disks = raid_disks-1;
1548 sector_t max_sector = mddev->size << 1;
1549 int sync_blocks;
1496 1550
1497 if (sector_nr >= mddev->size <<1) { 1551 if (sector_nr >= max_sector) {
1498 /* just being told to finish up .. nothing much to do */ 1552 /* just being told to finish up .. nothing much to do */
1499 unplug_slaves(mddev); 1553 unplug_slaves(mddev);
1554
1555 if (mddev->curr_resync < max_sector) /* aborted */
1556 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1557 &sync_blocks, 1);
1558 else /* compelted sync */
1559 conf->fullsync = 0;
1560 bitmap_close_sync(mddev->bitmap);
1561
1500 return 0; 1562 return 0;
1501 } 1563 }
1502 /* if there is 1 or more failed drives and we are trying 1564 /* if there is 1 or more failed drives and we are trying
@@ -1508,6 +1570,13 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
1508 *skipped = 1; 1570 *skipped = 1;
1509 return rv; 1571 return rv;
1510 } 1572 }
1573 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1574 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1575 /* we can skip this block, and probably more */
1576 sync_blocks /= STRIPE_SECTORS;
1577 *skipped = 1;
1578 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1579 }
1511 1580
1512 x = sector_nr; 1581 x = sector_nr;
1513 chunk_offset = sector_div(x, sectors_per_chunk); 1582 chunk_offset = sector_div(x, sectors_per_chunk);
@@ -1525,6 +1594,7 @@ static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, i
1525 set_current_state(TASK_UNINTERRUPTIBLE); 1594 set_current_state(TASK_UNINTERRUPTIBLE);
1526 schedule_timeout(1); 1595 schedule_timeout(1);
1527 } 1596 }
1597 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
1528 spin_lock(&sh->lock); 1598 spin_lock(&sh->lock);
1529 set_bit(STRIPE_SYNCING, &sh->state); 1599 set_bit(STRIPE_SYNCING, &sh->state);
1530 clear_bit(STRIPE_INSYNC, &sh->state); 1600 clear_bit(STRIPE_INSYNC, &sh->state);
@@ -1558,6 +1628,13 @@ static void raid5d (mddev_t *mddev)
1558 while (1) { 1628 while (1) {
1559 struct list_head *first; 1629 struct list_head *first;
1560 1630
1631 if (conf->seq_flush - conf->seq_write > 0) {
1632 int seq = conf->seq_flush;
1633 bitmap_unplug(mddev->bitmap);
1634 conf->seq_write = seq;
1635 activate_bit_delay(conf);
1636 }
1637
1561 if (list_empty(&conf->handle_list) && 1638 if (list_empty(&conf->handle_list) &&
1562 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && 1639 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1563 !blk_queue_plugged(mddev->queue) && 1640 !blk_queue_plugged(mddev->queue) &&
@@ -1591,7 +1668,7 @@ static void raid5d (mddev_t *mddev)
1591 PRINTK("--- raid5d inactive\n"); 1668 PRINTK("--- raid5d inactive\n");
1592} 1669}
1593 1670
1594static int run (mddev_t *mddev) 1671static int run(mddev_t *mddev)
1595{ 1672{
1596 raid5_conf_t *conf; 1673 raid5_conf_t *conf;
1597 int raid_disk, memory; 1674 int raid_disk, memory;
@@ -1621,6 +1698,7 @@ static int run (mddev_t *mddev)
1621 init_waitqueue_head(&conf->wait_for_overlap); 1698 init_waitqueue_head(&conf->wait_for_overlap);
1622 INIT_LIST_HEAD(&conf->handle_list); 1699 INIT_LIST_HEAD(&conf->handle_list);
1623 INIT_LIST_HEAD(&conf->delayed_list); 1700 INIT_LIST_HEAD(&conf->delayed_list);
1701 INIT_LIST_HEAD(&conf->bitmap_list);
1624 INIT_LIST_HEAD(&conf->inactive_list); 1702 INIT_LIST_HEAD(&conf->inactive_list);
1625 atomic_set(&conf->active_stripes, 0); 1703 atomic_set(&conf->active_stripes, 0);
1626 atomic_set(&conf->preread_active_stripes, 0); 1704 atomic_set(&conf->preread_active_stripes, 0);
@@ -1732,6 +1810,9 @@ memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1732 1810
1733 /* Ok, everything is just fine now */ 1811 /* Ok, everything is just fine now */
1734 1812
1813 if (mddev->bitmap)
1814 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1815
1735 mddev->queue->unplug_fn = raid5_unplug_device; 1816 mddev->queue->unplug_fn = raid5_unplug_device;
1736 mddev->queue->issue_flush_fn = raid5_issue_flush; 1817 mddev->queue->issue_flush_fn = raid5_issue_flush;
1737 1818
@@ -1912,6 +1993,8 @@ static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1912 rdev->in_sync = 0; 1993 rdev->in_sync = 0;
1913 rdev->raid_disk = disk; 1994 rdev->raid_disk = disk;
1914 found = 1; 1995 found = 1;
1996 if (rdev->saved_raid_disk != disk)
1997 conf->fullsync = 1;
1915 p->rdev = rdev; 1998 p->rdev = rdev;
1916 break; 1999 break;
1917 } 2000 }
@@ -1941,6 +2024,35 @@ static int raid5_resize(mddev_t *mddev, sector_t sectors)
1941 return 0; 2024 return 0;
1942} 2025}
1943 2026
2027static void raid5_quiesce(mddev_t *mddev, int state)
2028{
2029 raid5_conf_t *conf = mddev_to_conf(mddev);
2030
2031 switch(state) {
2032 case 1: /* stop all writes */
2033 spin_lock_irq(&conf->device_lock);
2034 conf->quiesce = 1;
2035 wait_event_lock_irq(conf->wait_for_stripe,
2036 atomic_read(&conf->active_stripes) == 0,
2037 conf->device_lock, /* nothing */);
2038 spin_unlock_irq(&conf->device_lock);
2039 break;
2040
2041 case 0: /* re-enable writes */
2042 spin_lock_irq(&conf->device_lock);
2043 conf->quiesce = 0;
2044 wake_up(&conf->wait_for_stripe);
2045 spin_unlock_irq(&conf->device_lock);
2046 break;
2047 }
2048 if (mddev->thread) {
2049 if (mddev->bitmap)
2050 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2051 else
2052 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2053 md_wakeup_thread(mddev->thread);
2054 }
2055}
1944static mdk_personality_t raid5_personality= 2056static mdk_personality_t raid5_personality=
1945{ 2057{
1946 .name = "raid5", 2058 .name = "raid5",
@@ -1955,6 +2067,7 @@ static mdk_personality_t raid5_personality=
1955 .spare_active = raid5_spare_active, 2067 .spare_active = raid5_spare_active,
1956 .sync_request = sync_request, 2068 .sync_request = sync_request,
1957 .resize = raid5_resize, 2069 .resize = raid5_resize,
2070 .quiesce = raid5_quiesce,
1958}; 2071};
1959 2072
1960static int __init raid5_init (void) 2073static int __init raid5_init (void)
gt;l_whence) { case SEEK_SET: start = 0; break; case SEEK_CUR: start = filp->f_pos; break; case SEEK_END: start = i_size_read(filp->f_path.dentry->d_inode); break; default: return -EINVAL; } start += l->l_start; if (start < 0) return -EINVAL; fl->fl_end = OFFSET_MAX; if (l->l_len > 0) { fl->fl_end = start + l->l_len - 1; } else if (l->l_len < 0) { fl->fl_end = start - 1; start += l->l_len; if (start < 0) return -EINVAL; } fl->fl_start = start; /* we record the absolute position */ if (fl->fl_end < fl->fl_start) return -EOVERFLOW; fl->fl_owner = current->files; fl->fl_pid = current->tgid; fl->fl_file = filp; fl->fl_flags = FL_POSIX; fl->fl_ops = NULL; fl->fl_lmops = NULL; switch (l->l_type) { case F_RDLCK: case F_WRLCK: case F_UNLCK: fl->fl_type = l->l_type; break; default: return -EINVAL; } return (0); } #endif /* default lease lock manager operations */ static void lease_break_callback(struct file_lock *fl) { kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG); } static void lease_release_private_callback(struct file_lock *fl) { if (!fl->fl_file) return; f_delown(fl->fl_file); fl->fl_file->f_owner.signum = 0; } static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try) { return fl->fl_file == try->fl_file; } static struct lock_manager_operations lease_manager_ops = { .fl_break = lease_break_callback, .fl_release_private = lease_release_private_callback, .fl_mylease = lease_mylease_callback, .fl_change = lease_modify, }; /* * Initialize a lease, use the default lock manager operations */ static int lease_init(struct file *filp, int type, struct file_lock *fl) { if (assign_type(fl, type) != 0) return -EINVAL; fl->fl_owner = current->files; fl->fl_pid = current->tgid; fl->fl_file = filp; fl->fl_flags = FL_LEASE; fl->fl_start = 0; fl->fl_end = OFFSET_MAX; fl->fl_ops = NULL; fl->fl_lmops = &lease_manager_ops; return 0; } /* Allocate a file_lock initialised to this type of lease */ static int lease_alloc(struct file *filp, int type, struct file_lock **flp) { struct file_lock *fl = locks_alloc_lock(); int error = -ENOMEM; if (fl == NULL) goto out; error = lease_init(filp, type, fl); if (error) { locks_free_lock(fl); fl = NULL; } out: *flp = fl; return error; } /* Check if two locks overlap each other. */ static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2) { return ((fl1->fl_end >= fl2->fl_start) && (fl2->fl_end >= fl1->fl_start)); } /* * Check whether two locks have the same owner. */ static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2) { if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner) return fl2->fl_lmops == fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner(fl1, fl2); return fl1->fl_owner == fl2->fl_owner; } /* Remove waiter from blocker's block list. * When blocker ends up pointing to itself then the list is empty. */ static void __locks_delete_block(struct file_lock *waiter) { list_del_init(&waiter->fl_block); list_del_init(&waiter->fl_link); waiter->fl_next = NULL; } /* */ static void locks_delete_block(struct file_lock *waiter) { lock_kernel(); __locks_delete_block(waiter); unlock_kernel(); } /* Insert waiter into blocker's block list. * We use a circular list so that processes can be easily woken up in * the order they blocked. The documentation doesn't require this but * it seems like the reasonable thing to do. */ static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter) { BUG_ON(!list_empty(&waiter->fl_block)); list_add_tail(&waiter->fl_block, &blocker->fl_block); waiter->fl_next = blocker; if (IS_POSIX(blocker)) list_add(&waiter->fl_link, &blocked_list); } /* Wake up processes blocked waiting for blocker. * If told to wait then schedule the processes until the block list * is empty, otherwise empty the block list ourselves. */ static void locks_wake_up_blocks(struct file_lock *blocker) { while (!list_empty(&blocker->fl_block)) { struct file_lock *waiter = list_entry(blocker->fl_block.next, struct file_lock, fl_block); __locks_delete_block(waiter); if (waiter->fl_lmops && waiter->fl_lmops->fl_notify) waiter->fl_lmops->fl_notify(waiter); else wake_up(&waiter->fl_wait); } } /* Insert file lock fl into an inode's lock list at the position indicated * by pos. At the same time add the lock to the global file lock list. */ static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl) { list_add(&fl->fl_link, &file_lock_list); /* insert into file's list */ fl->fl_next = *pos; *pos = fl; if (fl->fl_ops && fl->fl_ops->fl_insert) fl->fl_ops->fl_insert(fl); } /* * Delete a lock and then free it. * Wake up processes that are blocked waiting for this lock, * notify the FS that the lock has been cleared and * finally free the lock. */ static void locks_delete_lock(struct file_lock **thisfl_p) { struct file_lock *fl = *thisfl_p; *thisfl_p = fl->fl_next; fl->fl_next = NULL; list_del_init(&fl->fl_link); fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync); if (fl->fl_fasync != NULL) { printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync); fl->fl_fasync = NULL; } if (fl->fl_ops && fl->fl_ops->fl_remove) fl->fl_ops->fl_remove(fl); locks_wake_up_blocks(fl); locks_free_lock(fl); } /* Determine if lock sys_fl blocks lock caller_fl. Common functionality * checks for shared/exclusive status of overlapping locks. */ static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) { if (sys_fl->fl_type == F_WRLCK) return 1; if (caller_fl->fl_type == F_WRLCK) return 1; return 0; } /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific * checking before calling the locks_conflict(). */ static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) { /* POSIX locks owned by the same process do not conflict with * each other. */ if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl)) return (0); /* Check whether they overlap */ if (!locks_overlap(caller_fl, sys_fl)) return 0; return (locks_conflict(caller_fl, sys_fl)); } /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific * checking before calling the locks_conflict(). */ static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl) { /* FLOCK locks referring to the same filp do not conflict with * each other. */ if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file)) return (0); if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND)) return 0; return (locks_conflict(caller_fl, sys_fl)); } static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout) { int result = 0; DECLARE_WAITQUEUE(wait, current); __set_current_state(TASK_INTERRUPTIBLE); add_wait_queue(fl_wait, &wait); if (timeout == 0) schedule(); else result = schedule_timeout(timeout); if (signal_pending(current)) result = -ERESTARTSYS; remove_wait_queue(fl_wait, &wait); __set_current_state(TASK_RUNNING); return result; } static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time) { int result; locks_insert_block(blocker, waiter); result = interruptible_sleep_on_locked(&waiter->fl_wait, time); __locks_delete_block(waiter); return result; } int posix_test_lock(struct file *filp, struct file_lock *fl) { struct file_lock *cfl; lock_kernel(); for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) { if (!IS_POSIX(cfl)) continue; if (posix_locks_conflict(cfl, fl)) break; } if (cfl) { __locks_copy_lock(fl, cfl); unlock_kernel(); return 1; } else fl->fl_type = F_UNLCK; unlock_kernel(); return 0; } EXPORT_SYMBOL(posix_test_lock); /* This function tests for deadlock condition before putting a process to * sleep. The detection scheme is no longer recursive. Recursive was neat, * but dangerous - we risked stack corruption if the lock data was bad, or * if the recursion was too deep for any other reason. * * We rely on the fact that a task can only be on one lock's wait queue * at a time. When we find blocked_task on a wait queue we can re-search * with blocked_task equal to that queue's owner, until either blocked_task * isn't found, or blocked_task is found on a queue owned by my_task. * * Note: the above assumption may not be true when handling lock requests * from a broken NFS client. But broken NFS clients have a lot more to * worry about than proper deadlock detection anyway... --okir */ static int posix_locks_deadlock(struct file_lock *caller_fl, struct file_lock *block_fl) { struct list_head *tmp; next_task: if (posix_same_owner(caller_fl, block_fl)) return 1; list_for_each(tmp, &blocked_list) { struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link); if (posix_same_owner(fl, block_fl)) { fl = fl->fl_next; block_fl = fl; goto next_task; } } return 0; } /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks * at the head of the list, but that's secret knowledge known only to * flock_lock_file and posix_lock_file. * * Note that if called with an FL_EXISTS argument, the caller may determine * whether or not a lock was successfully freed by testing the return * value for -ENOENT. */ static int flock_lock_file(struct file *filp, struct file_lock *request) { struct file_lock *new_fl = NULL; struct file_lock **before; struct inode * inode = filp->f_path.dentry->d_inode; int error = 0; int found = 0; lock_kernel(); if (request->fl_flags & FL_ACCESS) goto find_conflict; for_each_lock(inode, before) { struct file_lock *fl = *before; if (IS_POSIX(fl)) break; if (IS_LEASE(fl)) continue; if (filp != fl->fl_file) continue; if (request->fl_type == fl->fl_type) goto out; found = 1; locks_delete_lock(before); break; } if (request->fl_type == F_UNLCK) { if ((request->fl_flags & FL_EXISTS) && !found) error = -ENOENT; goto out; } error = -ENOMEM; new_fl = locks_alloc_lock(); if (new_fl == NULL) goto out; /* * If a higher-priority process was blocked on the old file lock, * give it the opportunity to lock the file. */ if (found) cond_resched(); find_conflict: for_each_lock(inode, before) { struct file_lock *fl = *before; if (IS_POSIX(fl)) break; if (IS_LEASE(fl)) continue; if (!flock_locks_conflict(request, fl)) continue; error = -EAGAIN; if (request->fl_flags & FL_SLEEP) locks_insert_block(fl, request); goto out; } if (request->fl_flags & FL_ACCESS) goto out; locks_copy_lock(new_fl, request); locks_insert_lock(&inode->i_flock, new_fl); new_fl = NULL; error = 0; out: unlock_kernel(); if (new_fl) locks_free_lock(new_fl); return error; } static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock) { struct file_lock *fl; struct file_lock *new_fl = NULL; struct file_lock *new_fl2 = NULL; struct file_lock *left = NULL; struct file_lock *right = NULL; struct file_lock **before; int error, added = 0; /* * We may need two file_lock structures for this operation, * so we get them in advance to avoid races. * * In some cases we can be sure, that no new locks will be needed */ if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK || request->fl_start != 0 || request->fl_end != OFFSET_MAX)) { new_fl = locks_alloc_lock(); new_fl2 = locks_alloc_lock(); } lock_kernel(); if (request->fl_type != F_UNLCK) { for_each_lock(inode, before) { struct file_lock *fl = *before; if (!IS_POSIX(fl)) continue; if (!posix_locks_conflict(request, fl)) continue; if (conflock) locks_copy_lock(conflock, fl); error = -EAGAIN; if (!(request->fl_flags & FL_SLEEP)) goto out; error = -EDEADLK; if (posix_locks_deadlock(request, fl)) goto out; error = -EAGAIN; locks_insert_block(fl, request); goto out; } } /* If we're just looking for a conflict, we're done. */ error = 0; if (request->fl_flags & FL_ACCESS) goto out; /* * Find the first old lock with the same owner as the new lock. */ before = &inode->i_flock; /* First skip locks owned by other processes. */ while ((fl = *before) && (!IS_POSIX(fl) || !posix_same_owner(request, fl))) { before = &fl->fl_next; } /* Process locks with this owner. */ while ((fl = *before) && posix_same_owner(request, fl)) { /* Detect adjacent or overlapping regions (if same lock type) */ if (request->fl_type == fl->fl_type) { /* In all comparisons of start vs end, use * "start - 1" rather than "end + 1". If end * is OFFSET_MAX, end + 1 will become negative. */ if (fl->fl_end < request->fl_start - 1) goto next_lock; /* If the next lock in the list has entirely bigger * addresses than the new one, insert the lock here. */ if (fl->fl_start - 1 > request->fl_end) break; /* If we come here, the new and old lock are of the * same type and adjacent or overlapping. Make one * lock yielding from the lower start address of both * locks to the higher end address. */ if (fl->fl_start > request->fl_start) fl->fl_start = request->fl_start; else request->fl_start = fl->fl_start; if (fl->fl_end < request->fl_end) fl->fl_end = request->fl_end; else request->fl_end = fl->fl_end; if (added) { locks_delete_lock(before); continue; } request = fl; added = 1; } else { /* Processing for different lock types is a bit * more complex. */ if (fl->fl_end < request->fl_start) goto next_lock; if (fl->fl_start > request->fl_end) break; if (request->fl_type == F_UNLCK) added = 1; if (fl->fl_start < request->fl_start) left = fl; /* If the next lock in the list has a higher end * address than the new one, insert the new one here. */ if (fl->fl_end > request->fl_end) { right = fl; break; } if (fl->fl_start >= request->fl_start) { /* The new lock completely replaces an old * one (This may happen several times). */ if (added) { locks_delete_lock(before); continue; } /* Replace the old lock with the new one. * Wake up anybody waiting for the old one, * as the change in lock type might satisfy * their needs. */ locks_wake_up_blocks(fl); fl->fl_start = request->fl_start; fl->fl_end = request->fl_end; fl->fl_type = request->fl_type; locks_release_private(fl); locks_copy_private(fl, request); request = fl; added = 1; } } /* Go on to next lock. */ next_lock: before = &fl->fl_next; } /* * The above code only modifies existing locks in case of * merging or replacing. If new lock(s) need to be inserted * all modifications are done bellow this, so it's safe yet to * bail out. */ error = -ENOLCK; /* "no luck" */ if (right && left == right && !new_fl2) goto out; error = 0; if (!added) { if (request->fl_type == F_UNLCK) { if (request->fl_flags & FL_EXISTS) error = -ENOENT; goto out; } if (!new_fl) { error = -ENOLCK; goto out; } locks_copy_lock(new_fl, request); locks_insert_lock(before, new_fl); new_fl = NULL; } if (right) { if (left == right) { /* The new lock breaks the old one in two pieces, * so we have to use the second new lock. */ left = new_fl2; new_fl2 = NULL; locks_copy_lock(left, right); locks_insert_lock(before, left); } right->fl_start = request->fl_end + 1; locks_wake_up_blocks(right); } if (left) { left->fl_end = request->fl_start - 1; locks_wake_up_blocks(left); } out: unlock_kernel(); /* * Free any unused locks. */ if (new_fl) locks_free_lock(new_fl); if (new_fl2) locks_free_lock(new_fl2); return error; } /** * posix_lock_file - Apply a POSIX-style lock to a file * @filp: The file to apply the lock to * @fl: The lock to be applied * @conflock: Place to return a copy of the conflicting lock, if found. * * Add a POSIX style lock to a file. * We merge adjacent & overlapping locks whenever possible. * POSIX locks are sorted by owner task, then by starting address * * Note that if called with an FL_EXISTS argument, the caller may determine * whether or not a lock was successfully freed by testing the return * value for -ENOENT. */ int posix_lock_file(struct file *filp, struct file_lock *fl, struct file_lock *conflock) { return __posix_lock_file(filp->f_path.dentry->d_inode, fl, conflock); } EXPORT_SYMBOL(posix_lock_file); /** * posix_lock_file_wait - Apply a POSIX-style lock to a file * @filp: The file to apply the lock to * @fl: The lock to be applied * * Add a POSIX style lock to a file. * We merge adjacent & overlapping locks whenever possible. * POSIX locks are sorted by owner task, then by starting address */ int posix_lock_file_wait(struct file *filp, struct file_lock *fl) { int error; might_sleep (); for (;;) { error = posix_lock_file(filp, fl, NULL); if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) break; error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); if (!error) continue; locks_delete_block(fl); break; } return error; } EXPORT_SYMBOL(posix_lock_file_wait); /** * locks_mandatory_locked - Check for an active lock * @inode: the file to check * * Searches the inode's list of locks to find any POSIX locks which conflict. * This function is called from locks_verify_locked() only. */ int locks_mandatory_locked(struct inode *inode) { fl_owner_t owner = current->files; struct file_lock *fl; /* * Search the lock list for this inode for any POSIX locks. */ lock_kernel(); for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) { if (!IS_POSIX(fl)) continue; if (fl->fl_owner != owner) break; } unlock_kernel(); return fl ? -EAGAIN : 0; } /** * locks_mandatory_area - Check for a conflicting lock * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ * for shared * @inode: the file to check * @filp: how the file was opened (if it was) * @offset: start of area to check * @count: length of area to check * * Searches the inode's list of locks to find any POSIX locks which conflict. * This function is called from rw_verify_area() and * locks_verify_truncate(). */ int locks_mandatory_area(int read_write, struct inode *inode, struct file *filp, loff_t offset, size_t count) { struct file_lock fl; int error; locks_init_lock(&fl); fl.fl_owner = current->files; fl.fl_pid = current->tgid; fl.fl_file = filp; fl.fl_flags = FL_POSIX | FL_ACCESS; if (filp && !(filp->f_flags & O_NONBLOCK)) fl.fl_flags |= FL_SLEEP; fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK; fl.fl_start = offset; fl.fl_end = offset + count - 1; for (;;) { error = __posix_lock_file(inode, &fl, NULL); if (error != -EAGAIN) break; if (!(fl.fl_flags & FL_SLEEP)) break; error = wait_event_interruptible(fl.fl_wait, !fl.fl_next); if (!error) { /* * If we've been sleeping someone might have * changed the permissions behind our back. */ if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) continue; } locks_delete_block(&fl); break; } return error; } EXPORT_SYMBOL(locks_mandatory_area); /* We already had a lease on this file; just change its type */ int lease_modify(struct file_lock **before, int arg) { struct file_lock *fl = *before; int error = assign_type(fl, arg); if (error) return error; locks_wake_up_blocks(fl); if (arg == F_UNLCK) locks_delete_lock(before); return 0; } EXPORT_SYMBOL(lease_modify); static void time_out_leases(struct inode *inode) { struct file_lock **before; struct file_lock *fl; before = &inode->i_flock; while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) { if ((fl->fl_break_time == 0) || time_before(jiffies, fl->fl_break_time)) { before = &fl->fl_next; continue; } lease_modify(before, fl->fl_type & ~F_INPROGRESS); if (fl == *before) /* lease_modify may have freed fl */ before = &fl->fl_next; } } /** * __break_lease - revoke all outstanding leases on file * @inode: the inode of the file to return * @mode: the open mode (read or write) * * break_lease (inlined for speed) has checked there already * is a lease on this file. Leases are broken on a call to open() * or truncate(). This function can sleep unless you * specified %O_NONBLOCK to your open(). */ int __break_lease(struct inode *inode, unsigned int mode) { int error = 0, future; struct file_lock *new_fl, *flock; struct file_lock *fl; int alloc_err; unsigned long break_time; int i_have_this_lease = 0; alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK, &new_fl); lock_kernel(); time_out_leases(inode); flock = inode->i_flock; if ((flock == NULL) || !IS_LEASE(flock)) goto out; for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) if (fl->fl_owner == current->files) i_have_this_lease = 1; if (mode & FMODE_WRITE) { /* If we want write access, we have to revoke any lease. */ future = F_UNLCK | F_INPROGRESS; } else if (flock->fl_type & F_INPROGRESS) { /* If the lease is already being broken, we just leave it */ future = flock->fl_type; } else if (flock->fl_type & F_WRLCK) { /* Downgrade the exclusive lease to a read-only lease. */ future = F_RDLCK | F_INPROGRESS; } else { /* the existing lease was read-only, so we can read too. */ goto out; } if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) { error = alloc_err; goto out; } break_time = 0; if (lease_break_time > 0) { break_time = jiffies + lease_break_time * HZ; if (break_time == 0) break_time++; /* so that 0 means no break time */ } for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) { if (fl->fl_type != future) { fl->fl_type = future; fl->fl_break_time = break_time; /* lease must have lmops break callback */ fl->fl_lmops->fl_break(fl); } } if (i_have_this_lease || (mode & O_NONBLOCK)) { error = -EWOULDBLOCK; goto out; } restart: break_time = flock->fl_break_time; if (break_time != 0) { break_time -= jiffies; if (break_time == 0) break_time++; } error = locks_block_on_timeout(flock, new_fl, break_time); if (error >= 0) { if (error == 0) time_out_leases(inode); /* Wait for the next lease that has not been broken yet */ for (flock = inode->i_flock; flock && IS_LEASE(flock); flock = flock->fl_next) { if (flock->fl_type & F_INPROGRESS) goto restart; } error = 0; } out: unlock_kernel(); if (!alloc_err) locks_free_lock(new_fl); return error; } EXPORT_SYMBOL(__break_lease); /** * lease_get_mtime * @inode: the inode * @time: pointer to a timespec which will contain the last modified time * * This is to force NFS clients to flush their caches for files with * exclusive leases. The justification is that if someone has an * exclusive lease, then they could be modifiying it. */ void lease_get_mtime(struct inode *inode, struct timespec *time) { struct file_lock *flock = inode->i_flock; if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK)) *time = current_fs_time(inode->i_sb); else *time = inode->i_mtime; } EXPORT_SYMBOL(lease_get_mtime); /** * fcntl_getlease - Enquire what lease is currently active * @filp: the file * * The value returned by this function will be one of * (if no lease break is pending): * * %F_RDLCK to indicate a shared lease is held. * * %F_WRLCK to indicate an exclusive lease is held. * * %F_UNLCK to indicate no lease is held. * * (if a lease break is pending): * * %F_RDLCK to indicate an exclusive lease needs to be * changed to a shared lease (or removed). * * %F_UNLCK to indicate the lease needs to be removed. * * XXX: sfr & willy disagree over whether F_INPROGRESS * should be returned to userspace. */ int fcntl_getlease(struct file *filp) { struct file_lock *fl; int type = F_UNLCK; lock_kernel(); time_out_leases(filp->f_path.dentry->d_inode); for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl); fl = fl->fl_next) { if (fl->fl_file == filp) { type = fl->fl_type & ~F_INPROGRESS; break; } } unlock_kernel(); return type; } /** * __setlease - sets a lease on an open file * @filp: file pointer * @arg: type of lease to obtain * @flp: input - file_lock to use, output - file_lock inserted * * The (input) flp->fl_lmops->fl_break function is required * by break_lease(). * * Called with kernel lock held. */ static int __setlease(struct file *filp, long arg, struct file_lock **flp) { struct file_lock *fl, **before, **my_before = NULL, *lease; struct dentry *dentry = filp->f_path.dentry; struct inode *inode = dentry->d_inode; int error, rdlease_count = 0, wrlease_count = 0; time_out_leases(inode); error = -EINVAL; if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break) goto out; lease = *flp; error = -EAGAIN; if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0)) goto out; if ((arg == F_WRLCK) && ((atomic_read(&dentry->d_count) > 1) || (atomic_read(&inode->i_count) > 1))) goto out; /* * At this point, we know that if there is an exclusive * lease on this file, then we hold it on this filp * (otherwise our open of this file would have blocked). * And if we are trying to acquire an exclusive lease, * then the file is not open by anyone (including us) * except for this filp. */ for (before = &inode->i_flock; ((fl = *before) != NULL) && IS_LEASE(fl); before = &fl->fl_next) { if (lease->fl_lmops->fl_mylease(fl, lease)) my_before = before; else if (fl->fl_type == (F_INPROGRESS | F_UNLCK)) /* * Someone is in the process of opening this * file for writing so we may not take an * exclusive lease on it. */ wrlease_count++; else rdlease_count++; } if ((arg == F_RDLCK && (wrlease_count > 0)) || (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0))) goto out; if (my_before != NULL) { *flp = *my_before; error = lease->fl_lmops->fl_change(my_before, arg); goto out; } error = 0; if (arg == F_UNLCK) goto out; error = -EINVAL; if (!leases_enable) goto out; error = -ENOMEM; fl = locks_alloc_lock(); if (fl == NULL) goto out; locks_copy_lock(fl, lease); locks_insert_lock(before, fl); *flp = fl; error = 0; out: return error; } /** * setlease - sets a lease on an open file * @filp: file pointer * @arg: type of lease to obtain * @lease: file_lock to use * * Call this to establish a lease on the file. * The fl_lmops fl_break function is required by break_lease */ int setlease(struct file *filp, long arg, struct file_lock **lease) { struct dentry *dentry = filp->f_path.dentry; struct inode *inode = dentry->d_inode; int error; if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) return -EACCES; if (!S_ISREG(inode->i_mode)) return -EINVAL; error = security_file_lock(filp, arg); if (error) return error; lock_kernel(); error = __setlease(filp, arg, lease); unlock_kernel(); return error; } EXPORT_SYMBOL(setlease); /** * fcntl_setlease - sets a lease on an open file * @fd: open file descriptor * @filp: file pointer * @arg: type of lease to obtain * * Call this fcntl to establish a lease on the file. * Note that you also need to call %F_SETSIG to * receive a signal when the lease is broken. */ int fcntl_setlease(unsigned int fd, struct file *filp, long arg) { struct file_lock fl, *flp = &fl; struct dentry *dentry = filp->f_path.dentry; struct inode *inode = dentry->d_inode; int error; if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE)) return -EACCES; if (!S_ISREG(inode->i_mode)) return -EINVAL; error = security_file_lock(filp, arg); if (error) return error; locks_init_lock(&fl); error = lease_init(filp, arg, &fl); if (error) return error; lock_kernel(); error = __setlease(filp, arg, &flp); if (error || arg == F_UNLCK) goto out_unlock; error = fasync_helper(fd, filp, 1, &flp->fl_fasync); if (error < 0) { /* remove lease just inserted by __setlease */ flp->fl_type = F_UNLCK | F_INPROGRESS; flp->fl_break_time = jiffies- 10; time_out_leases(inode); goto out_unlock; } error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0); out_unlock: unlock_kernel(); return error; } /** * flock_lock_file_wait - Apply a FLOCK-style lock to a file * @filp: The file to apply the lock to * @fl: The lock to be applied * * Add a FLOCK style lock to a file. */ int flock_lock_file_wait(struct file *filp, struct file_lock *fl) { int error; might_sleep(); for (;;) { error = flock_lock_file(filp, fl); if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP)) break; error = wait_event_interruptible(fl->fl_wait, !fl->fl_next); if (!error) continue; locks_delete_block(fl); break; } return error; } EXPORT_SYMBOL(flock_lock_file_wait); /** * sys_flock: - flock() system call. * @fd: the file descriptor to lock. * @cmd: the type of lock to apply. * * Apply a %FL_FLOCK style lock to an open file descriptor. * The @cmd can be one of * * %LOCK_SH -- a shared lock. * * %LOCK_EX -- an exclusive lock. * * %LOCK_UN -- remove an existing lock. * * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes. * * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other * processes read and write access respectively. */ asmlinkage long sys_flock(unsigned int fd, unsigned int cmd) { struct file *filp; struct file_lock *lock; int can_sleep, unlock; int error; error = -EBADF; filp = fget(fd); if (!filp) goto out; can_sleep = !(cmd & LOCK_NB); cmd &= ~LOCK_NB; unlock = (cmd == LOCK_UN); if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3)) goto out_putf; error = flock_make_lock(filp, &lock, cmd); if (error) goto out_putf; if (can_sleep) lock->fl_flags |= FL_SLEEP; error = security_file_lock(filp, cmd); if (error) goto out_free; if (filp->f_op && filp->f_op->flock) error = filp->f_op->flock(filp, (can_sleep) ? F_SETLKW : F_SETLK, lock); else error = flock_lock_file_wait(filp, lock); out_free: locks_free_lock(lock); out_putf: fput(filp); out: return error; } /** * vfs_test_lock - test file byte range lock * @filp: The file to test lock for * @fl: The lock to test * @conf: Place to return a copy of the conflicting lock, if found * * Returns -ERRNO on failure. Indicates presence of conflicting lock by * setting conf->fl_type to something other than F_UNLCK. */ int vfs_test_lock(struct file *filp, struct file_lock *fl) { if (filp->f_op && filp->f_op->lock) return filp->f_op->lock(filp, F_GETLK, fl); posix_test_lock(filp, fl); return 0; } EXPORT_SYMBOL_GPL(vfs_test_lock); static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl) { flock->l_pid = fl->fl_pid; #if BITS_PER_LONG == 32 /* * Make sure we can represent the posix lock via * legacy 32bit flock. */ if (fl->fl_start > OFFT_OFFSET_MAX) return -EOVERFLOW; if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX) return -EOVERFLOW; #endif flock->l_start = fl->fl_start; flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : fl->fl_end - fl->fl_start + 1; flock->l_whence = 0; flock->l_type = fl->fl_type; return 0; } #if BITS_PER_LONG == 32 static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl) { flock->l_pid = fl->fl_pid; flock->l_start = fl->fl_start; flock->l_len = fl->fl_end == OFFSET_MAX ? 0 : fl->fl_end - fl->fl_start + 1; flock->l_whence = 0; flock->l_type = fl->fl_type; } #endif /* Report the first existing lock that would conflict with l. * This implements the F_GETLK command of fcntl(). */ int fcntl_getlk(struct file *filp, struct flock __user *l) { struct file_lock file_lock; struct flock flock; int error; error = -EFAULT; if (copy_from_user(&flock, l, sizeof(flock))) goto out; error = -EINVAL; if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) goto out; error = flock_to_posix_lock(filp, &file_lock, &flock); if (error) goto out; error = vfs_test_lock(filp, &file_lock); if (error) goto out; flock.l_type = file_lock.fl_type; if (file_lock.fl_type != F_UNLCK) { error = posix_lock_to_flock(&flock, &file_lock); if (error) goto out; } error = -EFAULT; if (!copy_to_user(l, &flock, sizeof(flock))) error = 0; out: return error; } /** * vfs_lock_file - file byte range lock * @filp: The file to apply the lock to * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.) * @fl: The lock to be applied * @conf: Place to return a copy of the conflicting lock, if found. * * A caller that doesn't care about the conflicting lock may pass NULL * as the final argument. * * If the filesystem defines a private ->lock() method, then @conf will * be left unchanged; so a caller that cares should initialize it to * some acceptable default. * * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX * locks, the ->lock() interface may return asynchronously, before the lock has * been granted or denied by the underlying filesystem, if (and only if) * fl_grant is set. Callers expecting ->lock() to return asynchronously * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if) * the request is for a blocking lock. When ->lock() does return asynchronously, * it must return -EINPROGRESS, and call ->fl_grant() when the lock * request completes. * If the request is for non-blocking lock the file system should return * -EINPROGRESS then try to get the lock and call the callback routine with * the result. If the request timed out the callback routine will return a * nonzero return code and the file system should release the lock. The file * system is also responsible to keep a corresponding posix lock when it * grants a lock so the VFS can find out which locks are locally held and do * the correct lock cleanup when required. * The underlying filesystem must not drop the kernel lock or call * ->fl_grant() before returning to the caller with a -EINPROGRESS * return code. */ int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf) { if (filp->f_op && filp->f_op->lock) return filp->f_op->lock(filp, cmd, fl); else return posix_lock_file(filp, fl, conf); } EXPORT_SYMBOL_GPL(vfs_lock_file); /* Apply the lock described by l to an open file descriptor. * This implements both the F_SETLK and F_SETLKW commands of fcntl(). */ int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd, struct flock __user *l) { struct file_lock *file_lock = locks_alloc_lock(); struct flock flock; struct inode *inode; int error; if (file_lock == NULL) return -ENOLCK; /* * This might block, so we do it before checking the inode. */ error = -EFAULT; if (copy_from_user(&flock, l, sizeof(flock))) goto out; inode = filp->f_path.dentry->d_inode; /* Don't allow mandatory locks on files that may be memory mapped * and shared. */ if (IS_MANDLOCK(inode) && (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && mapping_writably_mapped(filp->f_mapping)) { error = -EAGAIN; goto out; } again: error = flock_to_posix_lock(filp, file_lock, &flock); if (error) goto out; if (cmd == F_SETLKW) { file_lock->fl_flags |= FL_SLEEP; } error = -EBADF; switch (flock.l_type) { case F_RDLCK: if (!(filp->f_mode & FMODE_READ)) goto out; break; case F_WRLCK: if (!(filp->f_mode & FMODE_WRITE)) goto out; break; case F_UNLCK: break; default: error = -EINVAL; goto out; } error = security_file_lock(filp, file_lock->fl_type); if (error) goto out; for (;;) { error = vfs_lock_file(filp, cmd, file_lock, NULL); if (error != -EAGAIN || cmd == F_SETLK) break; error = wait_event_interruptible(file_lock->fl_wait, !file_lock->fl_next); if (!error) continue; locks_delete_block(file_lock); break; } /* * Attempt to detect a close/fcntl race and recover by * releasing the lock that was just acquired. */ if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { flock.l_type = F_UNLCK; goto again; } out: locks_free_lock(file_lock); return error; } #if BITS_PER_LONG == 32 /* Report the first existing lock that would conflict with l. * This implements the F_GETLK command of fcntl(). */ int fcntl_getlk64(struct file *filp, struct flock64 __user *l) { struct file_lock file_lock; struct flock64 flock; int error; error = -EFAULT; if (copy_from_user(&flock, l, sizeof(flock))) goto out; error = -EINVAL; if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK)) goto out; error = flock64_to_posix_lock(filp, &file_lock, &flock); if (error) goto out; error = vfs_test_lock(filp, &file_lock); if (error) goto out; flock.l_type = file_lock.fl_type; if (file_lock.fl_type != F_UNLCK) posix_lock_to_flock64(&flock, &file_lock); error = -EFAULT; if (!copy_to_user(l, &flock, sizeof(flock))) error = 0; out: return error; } /* Apply the lock described by l to an open file descriptor. * This implements both the F_SETLK and F_SETLKW commands of fcntl(). */ int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd, struct flock64 __user *l) { struct file_lock *file_lock = locks_alloc_lock(); struct flock64 flock; struct inode *inode; int error; if (file_lock == NULL) return -ENOLCK; /* * This might block, so we do it before checking the inode. */ error = -EFAULT; if (copy_from_user(&flock, l, sizeof(flock))) goto out; inode = filp->f_path.dentry->d_inode; /* Don't allow mandatory locks on files that may be memory mapped * and shared. */ if (IS_MANDLOCK(inode) && (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID && mapping_writably_mapped(filp->f_mapping)) { error = -EAGAIN; goto out; } again: error = flock64_to_posix_lock(filp, file_lock, &flock); if (error) goto out; if (cmd == F_SETLKW64) { file_lock->fl_flags |= FL_SLEEP; } error = -EBADF; switch (flock.l_type) { case F_RDLCK: if (!(filp->f_mode & FMODE_READ)) goto out; break; case F_WRLCK: if (!(filp->f_mode & FMODE_WRITE)) goto out; break; case F_UNLCK: break; default: error = -EINVAL; goto out; } error = security_file_lock(filp, file_lock->fl_type); if (error) goto out; for (;;) { error = vfs_lock_file(filp, cmd, file_lock, NULL); if (error != -EAGAIN || cmd == F_SETLK64) break; error = wait_event_interruptible(file_lock->fl_wait, !file_lock->fl_next); if (!error) continue; locks_delete_block(file_lock); break; } /* * Attempt to detect a close/fcntl race and recover by * releasing the lock that was just acquired. */ if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) { flock.l_type = F_UNLCK; goto again; } out: locks_free_lock(file_lock); return error; } #endif /* BITS_PER_LONG == 32 */ /* * This function is called when the file is being removed * from the task's fd array. POSIX locks belonging to this task * are deleted at this time. */ void locks_remove_posix(struct file *filp, fl_owner_t owner) { struct file_lock lock; /* * If there are no locks held on this file, we don't need to call * posix_lock_file(). Another process could be setting a lock on this * file at the same time, but we wouldn't remove that lock anyway. */ if (!filp->f_path.dentry->d_inode->i_flock) return; lock.fl_type = F_UNLCK; lock.fl_flags = FL_POSIX | FL_CLOSE; lock.fl_start = 0; lock.fl_end = OFFSET_MAX; lock.fl_owner = owner; lock.fl_pid = current->tgid; lock.fl_file = filp; lock.fl_ops = NULL; lock.fl_lmops = NULL; vfs_lock_file(filp, F_SETLK, &lock, NULL); if (lock.fl_ops && lock.fl_ops->fl_release_private) lock.fl_ops->fl_release_private(&lock); } EXPORT_SYMBOL(locks_remove_posix); /* * This function is called on the last close of an open file. */ void locks_remove_flock(struct file *filp) { struct inode * inode = filp->f_path.dentry->d_inode; struct file_lock *fl; struct file_lock **before; if (!inode->i_flock) return; if (filp->f_op && filp->f_op->flock) { struct file_lock fl = { .fl_pid = current->tgid, .fl_file = filp, .fl_flags = FL_FLOCK, .fl_type = F_UNLCK, .fl_end = OFFSET_MAX, }; filp->f_op->flock(filp, F_SETLKW, &fl); if (fl.fl_ops && fl.fl_ops->fl_release_private) fl.fl_ops->fl_release_private(&fl); } lock_kernel(); before = &inode->i_flock; while ((fl = *before) != NULL) { if (fl->fl_file == filp) { if (IS_FLOCK(fl)) { locks_delete_lock(before); continue; } if (IS_LEASE(fl)) { lease_modify(before, F_UNLCK); continue; } /* What? */ BUG(); } before = &fl->fl_next; } unlock_kernel(); } /** * posix_unblock_lock - stop waiting for a file lock * @filp: how the file was opened * @waiter: the lock which was waiting * * lockd needs to block waiting for locks. */ int posix_unblock_lock(struct file *filp, struct file_lock *waiter) { int status = 0; lock_kernel(); if (waiter->fl_next) __locks_delete_block(waiter); else status = -ENOENT; unlock_kernel(); return status; } EXPORT_SYMBOL(posix_unblock_lock); /** * vfs_cancel_lock - file byte range unblock lock * @filp: The file to apply the unblock to * @fl: The lock to be unblocked * * Used by lock managers to cancel blocked requests */ int vfs_cancel_lock(struct file *filp, struct file_lock *fl) { if (filp->f_op && filp->f_op->lock) return filp->f_op->lock(filp, F_CANCELLK, fl); return 0; } EXPORT_SYMBOL_GPL(vfs_cancel_lock); static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx) { struct inode *inode = NULL; if (fl->fl_file != NULL) inode = fl->fl_file->f_path.dentry->d_inode; out += sprintf(out, "%d:%s ", id, pfx); if (IS_POSIX(fl)) { out += sprintf(out, "%6s %s ", (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ", (inode == NULL) ? "*NOINODE*" : (IS_MANDLOCK(inode) && (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ? "MANDATORY" : "ADVISORY "); } else if (IS_FLOCK(fl)) { if (fl->fl_type & LOCK_MAND) { out += sprintf(out, "FLOCK MSNFS "); } else { out += sprintf(out, "FLOCK ADVISORY "); } } else if (IS_LEASE(fl)) { out += sprintf(out, "LEASE "); if (fl->fl_type & F_INPROGRESS) out += sprintf(out, "BREAKING "); else if (fl->fl_file) out += sprintf(out, "ACTIVE "); else out += sprintf(out, "BREAKER "); } else { out += sprintf(out, "UNKNOWN UNKNOWN "); } if (fl->fl_type & LOCK_MAND) { out += sprintf(out, "%s ", (fl->fl_type & LOCK_READ) ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ " : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE "); } else { out += sprintf(out, "%s ", (fl->fl_type & F_INPROGRESS) ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ " : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ "); } if (inode) { #ifdef WE_CAN_BREAK_LSLK_NOW