1 /* SPDX-License-Identifier: GPL-2.0 */ 2 #ifndef _LINUX_FILELOCK_H 3 #define _LINUX_FILELOCK_H 4 5 #include <linux/fs.h> 6 7 #define FL_POSIX 1 8 #define FL_FLOCK 2 9 #define FL_DELEG 4 /* NFSv4 delegation */ 10 #define FL_ACCESS 8 /* not trying to lock, just looking */ 11 #define FL_EXISTS 16 /* when unlocking, test for existence */ 12 #define FL_LEASE 32 /* lease held on this file */ 13 #define FL_CLOSE 64 /* unlock on close */ 14 #define FL_SLEEP 128 /* A blocking lock */ 15 #define FL_DOWNGRADE_PENDING 256 /* Lease is being downgraded */ 16 #define FL_UNLOCK_PENDING 512 /* Lease is being broken */ 17 #define FL_OFDLCK 1024 /* lock is "owned" by struct file */ 18 #define FL_LAYOUT 2048 /* outstanding pNFS layout */ 19 #define FL_RECLAIM 4096 /* reclaiming from a reboot server */ 20 21 #define FL_CLOSE_POSIX (FL_POSIX | FL_CLOSE) 22 23 /* 24 * Special return value from posix_lock_file() and vfs_lock_file() for 25 * asynchronous locking. 26 */ 27 #define FILE_LOCK_DEFERRED 1 28 29 struct file_lock; 30 struct file_lease; 31 32 struct file_lock_operations { 33 void (*fl_copy_lock)(struct file_lock *, struct file_lock *); 34 void (*fl_release_private)(struct file_lock *); 35 }; 36 37 struct lock_manager_operations { 38 void *lm_mod_owner; 39 fl_owner_t (*lm_get_owner)(fl_owner_t); 40 void (*lm_put_owner)(fl_owner_t); 41 void (*lm_notify)(struct file_lock *); /* unblock callback */ 42 int (*lm_grant)(struct file_lock *, int); 43 bool (*lm_lock_expirable)(struct file_lock *cfl); 44 void (*lm_expire_lock)(void); 45 }; 46 47 struct lease_manager_operations { 48 bool (*lm_break)(struct file_lease *); 49 int (*lm_change)(struct file_lease *, int, struct list_head *); 50 void (*lm_setup)(struct file_lease *, void **); 51 bool (*lm_breaker_owns_lease)(struct file_lease *); 52 }; 53 54 struct lock_manager { 55 struct list_head list; 56 /* 57 * NFSv4 and up also want opens blocked during the grace period; 58 * NLM doesn't care: 59 */ 60 bool block_opens; 61 }; 62 63 struct net; 64 void locks_start_grace(struct net *, struct lock_manager *); 65 void locks_end_grace(struct lock_manager *); 66 bool locks_in_grace(struct net *); 67 bool opens_in_grace(struct net *); 68 69 /* 70 * struct file_lock has a union that some filesystems use to track 71 * their own private info. The NFS side of things is defined here: 72 */ 73 #include <linux/nfs_fs_i.h> 74 75 /* 76 * struct file_lock represents a generic "file lock". It's used to represent 77 * POSIX byte range locks, BSD (flock) locks, and leases. It's important to 78 * note that the same struct is used to represent both a request for a lock and 79 * the lock itself, but the same object is never used for both. 80 * 81 * FIXME: should we create a separate "struct lock_request" to help distinguish 82 * these two uses? 83 * 84 * The varous i_flctx lists are ordered by: 85 * 86 * 1) lock owner 87 * 2) lock range start 88 * 3) lock range end 89 * 90 * Obviously, the last two criteria only matter for POSIX locks. 91 */ 92 93 struct file_lock_core { 94 struct file_lock_core *flc_blocker; /* The lock that is blocking us */ 95 struct list_head flc_list; /* link into file_lock_context */ 96 struct hlist_node flc_link; /* node in global lists */ 97 struct list_head flc_blocked_requests; /* list of requests with 98 * ->fl_blocker pointing here 99 */ 100 struct list_head flc_blocked_member; /* node in 101 * ->fl_blocker->fl_blocked_requests 102 */ 103 fl_owner_t flc_owner; 104 unsigned int flc_flags; 105 unsigned char flc_type; 106 pid_t flc_pid; 107 int flc_link_cpu; /* what cpu's list is this on? */ 108 wait_queue_head_t flc_wait; 109 struct file *flc_file; 110 }; 111 112 struct file_lock { 113 struct file_lock_core c; 114 loff_t fl_start; 115 loff_t fl_end; 116 117 const struct file_lock_operations *fl_ops; /* Callbacks for filesystems */ 118 const struct lock_manager_operations *fl_lmops; /* Callbacks for lockmanagers */ 119 union { 120 struct nfs_lock_info nfs_fl; 121 struct nfs4_lock_info nfs4_fl; 122 struct { 123 struct list_head link; /* link in AFS vnode's pending_locks list */ 124 int state; /* state of grant or error if -ve */ 125 unsigned int debug_id; 126 } afs; 127 struct { 128 struct inode *inode; 129 } ceph; 130 } fl_u; 131 } __randomize_layout; 132 133 struct file_lease { 134 struct file_lock_core c; 135 struct fasync_struct * fl_fasync; /* for lease break notifications */ 136 /* for lease breaks: */ 137 unsigned long fl_break_time; 138 unsigned long fl_downgrade_time; 139 const struct lease_manager_operations *fl_lmops; /* Callbacks for lease managers */ 140 } __randomize_layout; 141 142 struct file_lock_context { 143 spinlock_t flc_lock; 144 struct list_head flc_flock; 145 struct list_head flc_posix; 146 struct list_head flc_lease; 147 }; 148 149 #ifdef CONFIG_FILE_LOCKING 150 int fcntl_getlk(struct file *, unsigned int, struct flock *); 151 int fcntl_setlk(unsigned int, struct file *, unsigned int, 152 struct flock *); 153 154 #if BITS_PER_LONG == 32 155 int fcntl_getlk64(struct file *, unsigned int, struct flock64 *); 156 int fcntl_setlk64(unsigned int, struct file *, unsigned int, 157 struct flock64 *); 158 #endif 159 160 int fcntl_setlease(unsigned int fd, struct file *filp, int arg); 161 int fcntl_getlease(struct file *filp); 162 163 static inline bool lock_is_unlock(struct file_lock *fl) 164 { 165 return fl->c.flc_type == F_UNLCK; 166 } 167 168 static inline bool lock_is_read(struct file_lock *fl) 169 { 170 return fl->c.flc_type == F_RDLCK; 171 } 172 173 static inline bool lock_is_write(struct file_lock *fl) 174 { 175 return fl->c.flc_type == F_WRLCK; 176 } 177 178 static inline void locks_wake_up(struct file_lock *fl) 179 { 180 wake_up(&fl->c.flc_wait); 181 } 182 183 /* for walking lists of file_locks linked by fl_list */ 184 #define for_each_file_lock(_fl, _head) list_for_each_entry(_fl, _head, c.flc_list) 185 186 /* fs/locks.c */ 187 void locks_free_lock_context(struct inode *inode); 188 void locks_free_lock(struct file_lock *fl); 189 void locks_init_lock(struct file_lock *); 190 struct file_lock *locks_alloc_lock(void); 191 void locks_copy_lock(struct file_lock *, struct file_lock *); 192 void locks_copy_conflock(struct file_lock *, struct file_lock *); 193 void locks_remove_posix(struct file *, fl_owner_t); 194 void locks_remove_file(struct file *); 195 void locks_release_private(struct file_lock *); 196 void posix_test_lock(struct file *, struct file_lock *); 197 int posix_lock_file(struct file *, struct file_lock *, struct file_lock *); 198 int locks_delete_block(struct file_lock *); 199 int vfs_test_lock(struct file *, struct file_lock *); 200 int vfs_lock_file(struct file *, unsigned int, struct file_lock *, struct file_lock *); 201 int vfs_cancel_lock(struct file *filp, struct file_lock *fl); 202 bool vfs_inode_has_locks(struct inode *inode); 203 int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl); 204 205 void locks_init_lease(struct file_lease *); 206 void locks_free_lease(struct file_lease *fl); 207 struct file_lease *locks_alloc_lease(void); 208 int __break_lease(struct inode *inode, unsigned int flags, unsigned int type); 209 void lease_get_mtime(struct inode *, struct timespec64 *time); 210 int generic_setlease(struct file *, int, struct file_lease **, void **priv); 211 int vfs_setlease(struct file *, int, struct file_lease **, void **); 212 int lease_modify(struct file_lease *, int, struct list_head *); 213 214 struct notifier_block; 215 int lease_register_notifier(struct notifier_block *); 216 void lease_unregister_notifier(struct notifier_block *); 217 218 struct files_struct; 219 void show_fd_locks(struct seq_file *f, 220 struct file *filp, struct files_struct *files); 221 bool locks_owner_has_blockers(struct file_lock_context *flctx, 222 fl_owner_t owner); 223 224 static inline struct file_lock_context * 225 locks_inode_context(const struct inode *inode) 226 { 227 return smp_load_acquire(&inode->i_flctx); 228 } 229 230 #else /* !CONFIG_FILE_LOCKING */ 231 static inline int fcntl_getlk(struct file *file, unsigned int cmd, 232 struct flock __user *user) 233 { 234 return -EINVAL; 235 } 236 237 static inline int fcntl_setlk(unsigned int fd, struct file *file, 238 unsigned int cmd, struct flock __user *user) 239 { 240 return -EACCES; 241 } 242 243 #if BITS_PER_LONG == 32 244 static inline int fcntl_getlk64(struct file *file, unsigned int cmd, 245 struct flock64 *user) 246 { 247 return -EINVAL; 248 } 249 250 static inline int fcntl_setlk64(unsigned int fd, struct file *file, 251 unsigned int cmd, struct flock64 *user) 252 { 253 return -EACCES; 254 } 255 #endif 256 static inline int fcntl_setlease(unsigned int fd, struct file *filp, int arg) 257 { 258 return -EINVAL; 259 } 260 261 static inline int fcntl_getlease(struct file *filp) 262 { 263 return F_UNLCK; 264 } 265 266 static inline bool lock_is_unlock(struct file_lock *fl) 267 { 268 return false; 269 } 270 271 static inline bool lock_is_read(struct file_lock *fl) 272 { 273 return false; 274 } 275 276 static inline bool lock_is_write(struct file_lock *fl) 277 { 278 return false; 279 } 280 281 static inline void locks_wake_up(struct file_lock *fl) 282 { 283 } 284 285 #define for_each_file_lock(_fl, _head) while(false) 286 287 static inline void 288 locks_free_lock_context(struct inode *inode) 289 { 290 } 291 292 static inline void locks_init_lock(struct file_lock *fl) 293 { 294 return; 295 } 296 297 static inline void locks_init_lease(struct file_lease *fl) 298 { 299 return; 300 } 301 302 static inline void locks_copy_conflock(struct file_lock *new, struct file_lock *fl) 303 { 304 return; 305 } 306 307 static inline void locks_copy_lock(struct file_lock *new, struct file_lock *fl) 308 { 309 return; 310 } 311 312 static inline void locks_remove_posix(struct file *filp, fl_owner_t owner) 313 { 314 return; 315 } 316 317 static inline void locks_remove_file(struct file *filp) 318 { 319 return; 320 } 321 322 static inline void posix_test_lock(struct file *filp, struct file_lock *fl) 323 { 324 return; 325 } 326 327 static inline int posix_lock_file(struct file *filp, struct file_lock *fl, 328 struct file_lock *conflock) 329 { 330 return -ENOLCK; 331 } 332 333 static inline int locks_delete_block(struct file_lock *waiter) 334 { 335 return -ENOENT; 336 } 337 338 static inline int vfs_test_lock(struct file *filp, struct file_lock *fl) 339 { 340 return 0; 341 } 342 343 static inline int vfs_lock_file(struct file *filp, unsigned int cmd, 344 struct file_lock *fl, struct file_lock *conf) 345 { 346 return -ENOLCK; 347 } 348 349 static inline int vfs_cancel_lock(struct file *filp, struct file_lock *fl) 350 { 351 return 0; 352 } 353 354 static inline bool vfs_inode_has_locks(struct inode *inode) 355 { 356 return false; 357 } 358 359 static inline int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl) 360 { 361 return -ENOLCK; 362 } 363 364 static inline int __break_lease(struct inode *inode, unsigned int mode, unsigned int type) 365 { 366 return 0; 367 } 368 369 static inline void lease_get_mtime(struct inode *inode, 370 struct timespec64 *time) 371 { 372 return; 373 } 374 375 static inline int generic_setlease(struct file *filp, int arg, 376 struct file_lease **flp, void **priv) 377 { 378 return -EINVAL; 379 } 380 381 static inline int vfs_setlease(struct file *filp, int arg, 382 struct file_lease **lease, void **priv) 383 { 384 return -EINVAL; 385 } 386 387 static inline int lease_modify(struct file_lease *fl, int arg, 388 struct list_head *dispose) 389 { 390 return -EINVAL; 391 } 392 393 struct files_struct; 394 static inline void show_fd_locks(struct seq_file *f, 395 struct file *filp, struct files_struct *files) {} 396 static inline bool locks_owner_has_blockers(struct file_lock_context *flctx, 397 fl_owner_t owner) 398 { 399 return false; 400 } 401 402 static inline struct file_lock_context * 403 locks_inode_context(const struct inode *inode) 404 { 405 return NULL; 406 } 407 408 #endif /* !CONFIG_FILE_LOCKING */ 409 410 static inline int locks_lock_file_wait(struct file *filp, struct file_lock *fl) 411 { 412 return locks_lock_inode_wait(file_inode(filp), fl); 413 } 414 415 #ifdef CONFIG_FILE_LOCKING 416 static inline int break_lease(struct inode *inode, unsigned int mode) 417 { 418 /* 419 * Since this check is lockless, we must ensure that any refcounts 420 * taken are done before checking i_flctx->flc_lease. Otherwise, we 421 * could end up racing with tasks trying to set a new lease on this 422 * file. 423 */ 424 smp_mb(); 425 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease)) 426 return __break_lease(inode, mode, FL_LEASE); 427 return 0; 428 } 429 430 static inline int break_deleg(struct inode *inode, unsigned int mode) 431 { 432 /* 433 * Since this check is lockless, we must ensure that any refcounts 434 * taken are done before checking i_flctx->flc_lease. Otherwise, we 435 * could end up racing with tasks trying to set a new lease on this 436 * file. 437 */ 438 smp_mb(); 439 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease)) 440 return __break_lease(inode, mode, FL_DELEG); 441 return 0; 442 } 443 444 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode) 445 { 446 int ret; 447 448 ret = break_deleg(inode, O_WRONLY|O_NONBLOCK); 449 if (ret == -EWOULDBLOCK && delegated_inode) { 450 *delegated_inode = inode; 451 ihold(inode); 452 } 453 return ret; 454 } 455 456 static inline int break_deleg_wait(struct inode **delegated_inode) 457 { 458 int ret; 459 460 ret = break_deleg(*delegated_inode, O_WRONLY); 461 iput(*delegated_inode); 462 *delegated_inode = NULL; 463 return ret; 464 } 465 466 static inline int break_layout(struct inode *inode, bool wait) 467 { 468 smp_mb(); 469 if (inode->i_flctx && !list_empty_careful(&inode->i_flctx->flc_lease)) 470 return __break_lease(inode, 471 wait ? O_WRONLY : O_WRONLY | O_NONBLOCK, 472 FL_LAYOUT); 473 return 0; 474 } 475 476 #else /* !CONFIG_FILE_LOCKING */ 477 static inline int break_lease(struct inode *inode, unsigned int mode) 478 { 479 return 0; 480 } 481 482 static inline int break_deleg(struct inode *inode, unsigned int mode) 483 { 484 return 0; 485 } 486 487 static inline int try_break_deleg(struct inode *inode, struct inode **delegated_inode) 488 { 489 return 0; 490 } 491 492 static inline int break_deleg_wait(struct inode **delegated_inode) 493 { 494 BUG(); 495 return 0; 496 } 497 498 static inline int break_layout(struct inode *inode, bool wait) 499 { 500 return 0; 501 } 502 503 #endif /* CONFIG_FILE_LOCKING */ 504 505 #endif /* _LINUX_FILELOCK_H */ 506