@@ -74,12 +74,17 @@ static struct file_lock *file_lock(struct file_lock_core *flc)
74
74
return container_of (flc , struct file_lock , c );
75
75
}
76
76
77
- static bool lease_breaking (struct file_lock * fl )
77
+ static struct file_lease * file_lease (struct file_lock_core * flc )
78
+ {
79
+ return container_of (flc , struct file_lease , c );
80
+ }
81
+
82
+ static bool lease_breaking (struct file_lease * fl )
78
83
{
79
84
return fl -> c .flc_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING );
80
85
}
81
86
82
- static int target_leasetype (struct file_lock * fl )
87
+ static int target_leasetype (struct file_lease * fl )
83
88
{
84
89
if (fl -> c .flc_flags & FL_UNLOCK_PENDING )
85
90
return F_UNLCK ;
@@ -166,6 +171,7 @@ static DEFINE_SPINLOCK(blocked_lock_lock);
166
171
167
172
static struct kmem_cache * flctx_cache __ro_after_init ;
168
173
static struct kmem_cache * filelock_cache __ro_after_init ;
174
+ static struct kmem_cache * filelease_cache __ro_after_init ;
169
175
170
176
static struct file_lock_context *
171
177
locks_get_lock_context (struct inode * inode , int type )
@@ -275,6 +281,18 @@ struct file_lock *locks_alloc_lock(void)
275
281
}
276
282
EXPORT_SYMBOL_GPL (locks_alloc_lock );
277
283
284
+ /* Allocate an empty lock structure. */
285
+ struct file_lease * locks_alloc_lease (void )
286
+ {
287
+ struct file_lease * fl = kmem_cache_zalloc (filelease_cache , GFP_KERNEL );
288
+
289
+ if (fl )
290
+ locks_init_lock_heads (& fl -> c );
291
+
292
+ return fl ;
293
+ }
294
+ EXPORT_SYMBOL_GPL (locks_alloc_lease );
295
+
278
296
void locks_release_private (struct file_lock * fl )
279
297
{
280
298
struct file_lock_core * flc = & fl -> c ;
@@ -336,15 +354,25 @@ void locks_free_lock(struct file_lock *fl)
336
354
}
337
355
EXPORT_SYMBOL (locks_free_lock );
338
356
357
+ /* Free a lease which is not in use. */
358
+ void locks_free_lease (struct file_lease * fl )
359
+ {
360
+ kmem_cache_free (filelease_cache , fl );
361
+ }
362
+ EXPORT_SYMBOL (locks_free_lease );
363
+
339
364
static void
340
365
locks_dispose_list (struct list_head * dispose )
341
366
{
342
- struct file_lock * fl ;
367
+ struct file_lock_core * flc ;
343
368
344
369
while (!list_empty (dispose )) {
345
- fl = list_first_entry (dispose , struct file_lock , c .flc_list );
346
- list_del_init (& fl -> c .flc_list );
347
- locks_free_lock (fl );
370
+ flc = list_first_entry (dispose , struct file_lock_core , flc_list );
371
+ list_del_init (& flc -> flc_list );
372
+ if (flc -> flc_flags & (FL_LEASE |FL_DELEG |FL_LAYOUT ))
373
+ locks_free_lease (file_lease (flc ));
374
+ else
375
+ locks_free_lock (file_lock (flc ));
348
376
}
349
377
}
350
378
@@ -355,6 +383,13 @@ void locks_init_lock(struct file_lock *fl)
355
383
}
356
384
EXPORT_SYMBOL (locks_init_lock );
357
385
386
+ void locks_init_lease (struct file_lease * fl )
387
+ {
388
+ memset (fl , 0 , sizeof (* fl ));
389
+ locks_init_lock_heads (& fl -> c );
390
+ }
391
+ EXPORT_SYMBOL (locks_init_lease );
392
+
358
393
/*
359
394
* Initialize a new lock from an existing file_lock structure.
360
395
*/
@@ -518,14 +553,14 @@ static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
518
553
519
554
/* default lease lock manager operations */
520
555
static bool
521
- lease_break_callback (struct file_lock * fl )
556
+ lease_break_callback (struct file_lease * fl )
522
557
{
523
558
kill_fasync (& fl -> fl_fasync , SIGIO , POLL_MSG );
524
559
return false;
525
560
}
526
561
527
562
static void
528
- lease_setup (struct file_lock * fl , void * * priv )
563
+ lease_setup (struct file_lease * fl , void * * priv )
529
564
{
530
565
struct file * filp = fl -> c .flc_file ;
531
566
struct fasync_struct * fa = * priv ;
@@ -541,7 +576,7 @@ lease_setup(struct file_lock *fl, void **priv)
541
576
__f_setown (filp , task_pid (current ), PIDTYPE_TGID , 0 );
542
577
}
543
578
544
- static const struct lock_manager_operations lease_manager_ops = {
579
+ static const struct lease_manager_operations lease_manager_ops = {
545
580
.lm_break = lease_break_callback ,
546
581
.lm_change = lease_modify ,
547
582
.lm_setup = lease_setup ,
@@ -550,7 +585,7 @@ static const struct lock_manager_operations lease_manager_ops = {
550
585
/*
551
586
* Initialize a lease, use the default lock manager operations
552
587
*/
553
- static int lease_init (struct file * filp , int type , struct file_lock * fl )
588
+ static int lease_init (struct file * filp , int type , struct file_lease * fl )
554
589
{
555
590
if (assign_type (& fl -> c , type ) != 0 )
556
591
return - EINVAL ;
@@ -560,25 +595,22 @@ static int lease_init(struct file *filp, int type, struct file_lock *fl)
560
595
561
596
fl -> c .flc_file = filp ;
562
597
fl -> c .flc_flags = FL_LEASE ;
563
- fl -> fl_start = 0 ;
564
- fl -> fl_end = OFFSET_MAX ;
565
- fl -> fl_ops = NULL ;
566
598
fl -> fl_lmops = & lease_manager_ops ;
567
599
return 0 ;
568
600
}
569
601
570
602
/* Allocate a file_lock initialised to this type of lease */
571
- static struct file_lock * lease_alloc (struct file * filp , int type )
603
+ static struct file_lease * lease_alloc (struct file * filp , int type )
572
604
{
573
- struct file_lock * fl = locks_alloc_lock ();
605
+ struct file_lease * fl = locks_alloc_lease ();
574
606
int error = - ENOMEM ;
575
607
576
608
if (fl == NULL )
577
609
return ERR_PTR (error );
578
610
579
611
error = lease_init (filp , type , fl );
580
612
if (error ) {
581
- locks_free_lock (fl );
613
+ locks_free_lease (fl );
582
614
return ERR_PTR (error );
583
615
}
584
616
return fl ;
@@ -1395,7 +1427,7 @@ static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
1395
1427
return error ;
1396
1428
}
1397
1429
1398
- static void lease_clear_pending (struct file_lock * fl , int arg )
1430
+ static void lease_clear_pending (struct file_lease * fl , int arg )
1399
1431
{
1400
1432
switch (arg ) {
1401
1433
case F_UNLCK :
@@ -1407,7 +1439,7 @@ static void lease_clear_pending(struct file_lock *fl, int arg)
1407
1439
}
1408
1440
1409
1441
/* We already had a lease on this file; just change its type */
1410
- int lease_modify (struct file_lock * fl , int arg , struct list_head * dispose )
1442
+ int lease_modify (struct file_lease * fl , int arg , struct list_head * dispose )
1411
1443
{
1412
1444
int error = assign_type (& fl -> c , arg );
1413
1445
@@ -1442,7 +1474,7 @@ static bool past_time(unsigned long then)
1442
1474
static void time_out_leases (struct inode * inode , struct list_head * dispose )
1443
1475
{
1444
1476
struct file_lock_context * ctx = inode -> i_flctx ;
1445
- struct file_lock * fl , * tmp ;
1477
+ struct file_lease * fl , * tmp ;
1446
1478
1447
1479
lockdep_assert_held (& ctx -> flc_lock );
1448
1480
@@ -1458,8 +1490,8 @@ static void time_out_leases(struct inode *inode, struct list_head *dispose)
1458
1490
static bool leases_conflict (struct file_lock_core * lc , struct file_lock_core * bc )
1459
1491
{
1460
1492
bool rc ;
1461
- struct file_lock * lease = file_lock (lc );
1462
- struct file_lock * breaker = file_lock (bc );
1493
+ struct file_lease * lease = file_lease (lc );
1494
+ struct file_lease * breaker = file_lease (bc );
1463
1495
1464
1496
if (lease -> fl_lmops -> lm_breaker_owns_lease
1465
1497
&& lease -> fl_lmops -> lm_breaker_owns_lease (lease ))
@@ -1480,7 +1512,7 @@ static bool leases_conflict(struct file_lock_core *lc, struct file_lock_core *bc
1480
1512
}
1481
1513
1482
1514
static bool
1483
- any_leases_conflict (struct inode * inode , struct file_lock * breaker )
1515
+ any_leases_conflict (struct inode * inode , struct file_lease * breaker )
1484
1516
{
1485
1517
struct file_lock_context * ctx = inode -> i_flctx ;
1486
1518
struct file_lock_core * flc ;
@@ -1511,7 +1543,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1511
1543
{
1512
1544
int error = 0 ;
1513
1545
struct file_lock_context * ctx ;
1514
- struct file_lock * new_fl , * fl , * tmp ;
1546
+ struct file_lease * new_fl , * fl , * tmp ;
1515
1547
unsigned long break_time ;
1516
1548
int want_write = (mode & O_ACCMODE ) != O_RDONLY ;
1517
1549
LIST_HEAD (dispose );
@@ -1571,7 +1603,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1571
1603
}
1572
1604
1573
1605
restart :
1574
- fl = list_first_entry (& ctx -> flc_lease , struct file_lock , c .flc_list );
1606
+ fl = list_first_entry (& ctx -> flc_lease , struct file_lease , c .flc_list );
1575
1607
break_time = fl -> fl_break_time ;
1576
1608
if (break_time != 0 )
1577
1609
break_time -= jiffies ;
@@ -1590,7 +1622,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1590
1622
percpu_down_read (& file_rwsem );
1591
1623
spin_lock (& ctx -> flc_lock );
1592
1624
trace_break_lease_unblock (inode , new_fl );
1593
- locks_delete_block ( new_fl );
1625
+ __locks_delete_block ( & new_fl -> c );
1594
1626
if (error >= 0 ) {
1595
1627
/*
1596
1628
* Wait for the next conflicting lease that has not been
@@ -1607,7 +1639,7 @@ int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
1607
1639
percpu_up_read (& file_rwsem );
1608
1640
locks_dispose_list (& dispose );
1609
1641
free_lock :
1610
- locks_free_lock (new_fl );
1642
+ locks_free_lease (new_fl );
1611
1643
return error ;
1612
1644
}
1613
1645
EXPORT_SYMBOL (__break_lease );
@@ -1625,14 +1657,14 @@ void lease_get_mtime(struct inode *inode, struct timespec64 *time)
1625
1657
{
1626
1658
bool has_lease = false;
1627
1659
struct file_lock_context * ctx ;
1628
- struct file_lock * fl ;
1660
+ struct file_lock_core * flc ;
1629
1661
1630
1662
ctx = locks_inode_context (inode );
1631
1663
if (ctx && !list_empty_careful (& ctx -> flc_lease )) {
1632
1664
spin_lock (& ctx -> flc_lock );
1633
- fl = list_first_entry_or_null (& ctx -> flc_lease ,
1634
- struct file_lock , c . flc_list );
1635
- if (fl && lock_is_write ( fl ) )
1665
+ flc = list_first_entry_or_null (& ctx -> flc_lease ,
1666
+ struct file_lock_core , flc_list );
1667
+ if (flc && flc -> flc_type == F_WRLCK )
1636
1668
has_lease = true;
1637
1669
spin_unlock (& ctx -> flc_lock );
1638
1670
}
@@ -1667,7 +1699,7 @@ EXPORT_SYMBOL(lease_get_mtime);
1667
1699
*/
1668
1700
int fcntl_getlease (struct file * filp )
1669
1701
{
1670
- struct file_lock * fl ;
1702
+ struct file_lease * fl ;
1671
1703
struct inode * inode = file_inode (filp );
1672
1704
struct file_lock_context * ctx ;
1673
1705
int type = F_UNLCK ;
@@ -1739,9 +1771,9 @@ check_conflicting_open(struct file *filp, const int arg, int flags)
1739
1771
}
1740
1772
1741
1773
static int
1742
- generic_add_lease (struct file * filp , int arg , struct file_lock * * flp , void * * priv )
1774
+ generic_add_lease (struct file * filp , int arg , struct file_lease * * flp , void * * priv )
1743
1775
{
1744
- struct file_lock * fl , * my_fl = NULL , * lease ;
1776
+ struct file_lease * fl , * my_fl = NULL , * lease ;
1745
1777
struct inode * inode = file_inode (filp );
1746
1778
struct file_lock_context * ctx ;
1747
1779
bool is_deleg = (* flp )-> c .flc_flags & FL_DELEG ;
@@ -1850,7 +1882,7 @@ generic_add_lease(struct file *filp, int arg, struct file_lock **flp, void **pri
1850
1882
static int generic_delete_lease (struct file * filp , void * owner )
1851
1883
{
1852
1884
int error = - EAGAIN ;
1853
- struct file_lock * fl , * victim = NULL ;
1885
+ struct file_lease * fl , * victim = NULL ;
1854
1886
struct inode * inode = file_inode (filp );
1855
1887
struct file_lock_context * ctx ;
1856
1888
LIST_HEAD (dispose );
@@ -1890,7 +1922,7 @@ static int generic_delete_lease(struct file *filp, void *owner)
1890
1922
* The (input) flp->fl_lmops->lm_break function is required
1891
1923
* by break_lease().
1892
1924
*/
1893
- int generic_setlease (struct file * filp , int arg , struct file_lock * * flp ,
1925
+ int generic_setlease (struct file * filp , int arg , struct file_lease * * flp ,
1894
1926
void * * priv )
1895
1927
{
1896
1928
struct inode * inode = file_inode (filp );
@@ -1937,7 +1969,7 @@ lease_notifier_chain_init(void)
1937
1969
}
1938
1970
1939
1971
static inline void
1940
- setlease_notifier (int arg , struct file_lock * lease )
1972
+ setlease_notifier (int arg , struct file_lease * lease )
1941
1973
{
1942
1974
if (arg != F_UNLCK )
1943
1975
srcu_notifier_call_chain (& lease_notifier_chain , arg , lease );
@@ -1973,7 +2005,7 @@ EXPORT_SYMBOL_GPL(lease_unregister_notifier);
1973
2005
* may be NULL if the lm_setup operation doesn't require it.
1974
2006
*/
1975
2007
int
1976
- vfs_setlease (struct file * filp , int arg , struct file_lock * * lease , void * * priv )
2008
+ vfs_setlease (struct file * filp , int arg , struct file_lease * * lease , void * * priv )
1977
2009
{
1978
2010
if (lease )
1979
2011
setlease_notifier (arg , * lease );
@@ -1986,7 +2018,7 @@ EXPORT_SYMBOL_GPL(vfs_setlease);
1986
2018
1987
2019
static int do_fcntl_add_lease (unsigned int fd , struct file * filp , int arg )
1988
2020
{
1989
- struct file_lock * fl ;
2021
+ struct file_lease * fl ;
1990
2022
struct fasync_struct * new ;
1991
2023
int error ;
1992
2024
@@ -1996,14 +2028,14 @@ static int do_fcntl_add_lease(unsigned int fd, struct file *filp, int arg)
1996
2028
1997
2029
new = fasync_alloc ();
1998
2030
if (!new ) {
1999
- locks_free_lock (fl );
2031
+ locks_free_lease (fl );
2000
2032
return - ENOMEM ;
2001
2033
}
2002
2034
new -> fa_fd = fd ;
2003
2035
2004
2036
error = vfs_setlease (filp , arg , & fl , (void * * )& new );
2005
2037
if (fl )
2006
- locks_free_lock (fl );
2038
+ locks_free_lease (fl );
2007
2039
if (new )
2008
2040
fasync_free (new );
2009
2041
return error ;
@@ -2626,7 +2658,7 @@ locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
2626
2658
static void
2627
2659
locks_remove_lease (struct file * filp , struct file_lock_context * ctx )
2628
2660
{
2629
- struct file_lock * fl , * tmp ;
2661
+ struct file_lease * fl , * tmp ;
2630
2662
LIST_HEAD (dispose );
2631
2663
2632
2664
if (list_empty (& ctx -> flc_lease ))
@@ -2755,14 +2787,16 @@ static void lock_get_status(struct seq_file *f, struct file_lock_core *flc,
2755
2787
} else if (flc -> flc_flags & FL_FLOCK ) {
2756
2788
seq_puts (f , "FLOCK ADVISORY " );
2757
2789
} else if (flc -> flc_flags & (FL_LEASE |FL_DELEG |FL_LAYOUT )) {
2758
- type = target_leasetype (fl );
2790
+ struct file_lease * lease = file_lease (flc );
2791
+
2792
+ type = target_leasetype (lease );
2759
2793
2760
2794
if (flc -> flc_flags & FL_DELEG )
2761
2795
seq_puts (f , "DELEG " );
2762
2796
else
2763
2797
seq_puts (f , "LEASE " );
2764
2798
2765
- if (lease_breaking (fl ))
2799
+ if (lease_breaking (lease ))
2766
2800
seq_puts (f , "BREAKING " );
2767
2801
else if (flc -> flc_file )
2768
2802
seq_puts (f , "ACTIVE " );
@@ -2945,6 +2979,9 @@ static int __init filelock_init(void)
2945
2979
filelock_cache = kmem_cache_create ("file_lock_cache" ,
2946
2980
sizeof (struct file_lock ), 0 , SLAB_PANIC , NULL );
2947
2981
2982
+ filelease_cache = kmem_cache_create ("file_lock_cache" ,
2983
+ sizeof (struct file_lease ), 0 , SLAB_PANIC , NULL );
2984
+
2948
2985
for_each_possible_cpu (i ) {
2949
2986
struct file_lock_list_struct * fll = per_cpu_ptr (& file_lock_list , i );
2950
2987
0 commit comments