PageRenderTime 34ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/linux-2.6.21.x/fs/locks.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 2284 lines | 1450 code | 242 blank | 592 comment | 418 complexity | 3227967cd345c1dd9a8b0a529f7f85e9 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * linux/fs/locks.c
  3. *
  4. * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
  5. * Doug Evans (dje@spiff.uucp), August 07, 1992
  6. *
  7. * Deadlock detection added.
  8. * FIXME: one thing isn't handled yet:
  9. * - mandatory locks (requires lots of changes elsewhere)
  10. * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
  11. *
  12. * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
  13. * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
  14. *
  15. * Converted file_lock_table to a linked list from an array, which eliminates
  16. * the limits on how many active file locks are open.
  17. * Chad Page (pageone@netcom.com), November 27, 1994
  18. *
  19. * Removed dependency on file descriptors. dup()'ed file descriptors now
  20. * get the same locks as the original file descriptors, and a close() on
  21. * any file descriptor removes ALL the locks on the file for the current
  22. * process. Since locks still depend on the process id, locks are inherited
  23. * after an exec() but not after a fork(). This agrees with POSIX, and both
  24. * BSD and SVR4 practice.
  25. * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
  26. *
  27. * Scrapped free list which is redundant now that we allocate locks
  28. * dynamically with kmalloc()/kfree().
  29. * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
  30. *
  31. * Implemented two lock personalities - FL_FLOCK and FL_POSIX.
  32. *
  33. * FL_POSIX locks are created with calls to fcntl() and lockf() through the
  34. * fcntl() system call. They have the semantics described above.
  35. *
  36. * FL_FLOCK locks are created with calls to flock(), through the flock()
  37. * system call, which is new. Old C libraries implement flock() via fcntl()
  38. * and will continue to use the old, broken implementation.
  39. *
  40. * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
  41. * with a file pointer (filp). As a result they can be shared by a parent
  42. * process and its children after a fork(). They are removed when the last
  43. * file descriptor referring to the file pointer is closed (unless explicitly
  44. * unlocked).
  45. *
  46. * FL_FLOCK locks never deadlock, an existing lock is always removed before
  47. * upgrading from shared to exclusive (or vice versa). When this happens
  48. * any processes blocked by the current lock are woken up and allowed to
  49. * run before the new lock is applied.
  50. * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
  51. *
  52. * Removed some race conditions in flock_lock_file(), marked other possible
  53. * races. Just grep for FIXME to see them.
  54. * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
  55. *
  56. * Addressed Dmitry's concerns. Deadlock checking no longer recursive.
  57. * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
  58. * once we've checked for blocking and deadlocking.
  59. * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
  60. *
  61. * Initial implementation of mandatory locks. SunOS turned out to be
  62. * a rotten model, so I implemented the "obvious" semantics.
  63. * See 'Documentation/mandatory.txt' for details.
  64. * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
  65. *
  66. * Don't allow mandatory locks on mmap()'ed files. Added simple functions to
  67. * check if a file has mandatory locks, used by mmap(), open() and creat() to
  68. * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
  69. * Manual, Section 2.
  70. * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
  71. *
  72. * Tidied up block list handling. Added '/proc/locks' interface.
  73. * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
  74. *
  75. * Fixed deadlock condition for pathological code that mixes calls to
  76. * flock() and fcntl().
  77. * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
  78. *
  79. * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
  80. * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
  81. * guarantee sensible behaviour in the case where file system modules might
  82. * be compiled with different options than the kernel itself.
  83. * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  84. *
  85. * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
  86. * (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
  87. * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  88. *
  89. * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
  90. * locks. Changed process synchronisation to avoid dereferencing locks that
  91. * have already been freed.
  92. * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
  93. *
  94. * Made the block list a circular list to minimise searching in the list.
  95. * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
  96. *
  97. * Made mandatory locking a mount option. Default is not to allow mandatory
  98. * locking.
  99. * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
  100. *
  101. * Some adaptations for NFS support.
  102. * Olaf Kirch (okir@monad.swb.de), Dec 1996,
  103. *
  104. * Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
  105. * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
  106. *
  107. * Use slab allocator instead of kmalloc/kfree.
  108. * Use generic list implementation from <linux/list.h>.
  109. * Sped up posix_locks_deadlock by only considering blocked locks.
  110. * Matthew Wilcox <willy@debian.org>, March, 2000.
  111. *
  112. * Leases and LOCK_MAND
  113. * Matthew Wilcox <willy@debian.org>, June, 2000.
  114. * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
  115. */
  116. #include <linux/capability.h>
  117. #include <linux/file.h>
  118. #include <linux/fs.h>
  119. #include <linux/init.h>
  120. #include <linux/module.h>
  121. #include <linux/security.h>
  122. #include <linux/slab.h>
  123. #include <linux/smp_lock.h>
  124. #include <linux/syscalls.h>
  125. #include <linux/time.h>
  126. #include <linux/rcupdate.h>
  127. #include <asm/semaphore.h>
  128. #include <asm/uaccess.h>
  129. #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
  130. #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
  131. #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
  132. int leases_enable = 1;
  133. int lease_break_time = 45;
  134. #define for_each_lock(inode, lockp) \
  135. for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
  136. static LIST_HEAD(file_lock_list);
  137. static LIST_HEAD(blocked_list);
  138. static struct kmem_cache *filelock_cache __read_mostly;
  139. /* Allocate an empty lock structure. */
  140. static struct file_lock *locks_alloc_lock(void)
  141. {
  142. return kmem_cache_alloc(filelock_cache, GFP_KERNEL);
  143. }
  144. static void locks_release_private(struct file_lock *fl)
  145. {
  146. if (fl->fl_ops) {
  147. if (fl->fl_ops->fl_release_private)
  148. fl->fl_ops->fl_release_private(fl);
  149. fl->fl_ops = NULL;
  150. }
  151. if (fl->fl_lmops) {
  152. if (fl->fl_lmops->fl_release_private)
  153. fl->fl_lmops->fl_release_private(fl);
  154. fl->fl_lmops = NULL;
  155. }
  156. }
  157. /* Free a lock which is not in use. */
  158. static void locks_free_lock(struct file_lock *fl)
  159. {
  160. BUG_ON(waitqueue_active(&fl->fl_wait));
  161. BUG_ON(!list_empty(&fl->fl_block));
  162. BUG_ON(!list_empty(&fl->fl_link));
  163. locks_release_private(fl);
  164. kmem_cache_free(filelock_cache, fl);
  165. }
  166. void locks_init_lock(struct file_lock *fl)
  167. {
  168. INIT_LIST_HEAD(&fl->fl_link);
  169. INIT_LIST_HEAD(&fl->fl_block);
  170. init_waitqueue_head(&fl->fl_wait);
  171. fl->fl_next = NULL;
  172. fl->fl_fasync = NULL;
  173. fl->fl_owner = NULL;
  174. fl->fl_pid = 0;
  175. fl->fl_file = NULL;
  176. fl->fl_flags = 0;
  177. fl->fl_type = 0;
  178. fl->fl_start = fl->fl_end = 0;
  179. fl->fl_ops = NULL;
  180. fl->fl_lmops = NULL;
  181. }
  182. EXPORT_SYMBOL(locks_init_lock);
  183. /*
  184. * Initialises the fields of the file lock which are invariant for
  185. * free file_locks.
  186. */
  187. static void init_once(void *foo, struct kmem_cache *cache, unsigned long flags)
  188. {
  189. struct file_lock *lock = (struct file_lock *) foo;
  190. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) !=
  191. SLAB_CTOR_CONSTRUCTOR)
  192. return;
  193. locks_init_lock(lock);
  194. }
  195. static void locks_copy_private(struct file_lock *new, struct file_lock *fl)
  196. {
  197. if (fl->fl_ops) {
  198. if (fl->fl_ops->fl_copy_lock)
  199. fl->fl_ops->fl_copy_lock(new, fl);
  200. new->fl_ops = fl->fl_ops;
  201. }
  202. if (fl->fl_lmops) {
  203. if (fl->fl_lmops->fl_copy_lock)
  204. fl->fl_lmops->fl_copy_lock(new, fl);
  205. new->fl_lmops = fl->fl_lmops;
  206. }
  207. }
  208. /*
  209. * Initialize a new lock from an existing file_lock structure.
  210. */
  211. static void __locks_copy_lock(struct file_lock *new, const struct file_lock *fl)
  212. {
  213. new->fl_owner = fl->fl_owner;
  214. new->fl_pid = fl->fl_pid;
  215. new->fl_file = NULL;
  216. new->fl_flags = fl->fl_flags;
  217. new->fl_type = fl->fl_type;
  218. new->fl_start = fl->fl_start;
  219. new->fl_end = fl->fl_end;
  220. new->fl_ops = NULL;
  221. new->fl_lmops = NULL;
  222. }
  223. void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  224. {
  225. locks_release_private(new);
  226. __locks_copy_lock(new, fl);
  227. new->fl_file = fl->fl_file;
  228. new->fl_ops = fl->fl_ops;
  229. new->fl_lmops = fl->fl_lmops;
  230. locks_copy_private(new, fl);
  231. }
  232. EXPORT_SYMBOL(locks_copy_lock);
  233. static inline int flock_translate_cmd(int cmd) {
  234. if (cmd & LOCK_MAND)
  235. return cmd & (LOCK_MAND | LOCK_RW);
  236. switch (cmd) {
  237. case LOCK_SH:
  238. return F_RDLCK;
  239. case LOCK_EX:
  240. return F_WRLCK;
  241. case LOCK_UN:
  242. return F_UNLCK;
  243. }
  244. return -EINVAL;
  245. }
  246. /* Fill in a file_lock structure with an appropriate FLOCK lock. */
  247. static int flock_make_lock(struct file *filp, struct file_lock **lock,
  248. unsigned int cmd)
  249. {
  250. struct file_lock *fl;
  251. int type = flock_translate_cmd(cmd);
  252. if (type < 0)
  253. return type;
  254. fl = locks_alloc_lock();
  255. if (fl == NULL)
  256. return -ENOMEM;
  257. fl->fl_file = filp;
  258. fl->fl_pid = current->tgid;
  259. fl->fl_flags = FL_FLOCK;
  260. fl->fl_type = type;
  261. fl->fl_end = OFFSET_MAX;
  262. *lock = fl;
  263. return 0;
  264. }
  265. static int assign_type(struct file_lock *fl, int type)
  266. {
  267. switch (type) {
  268. case F_RDLCK:
  269. case F_WRLCK:
  270. case F_UNLCK:
  271. fl->fl_type = type;
  272. break;
  273. default:
  274. return -EINVAL;
  275. }
  276. return 0;
  277. }
  278. /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  279. * style lock.
  280. */
  281. static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
  282. struct flock *l)
  283. {
  284. off_t start, end;
  285. switch (l->l_whence) {
  286. case SEEK_SET:
  287. start = 0;
  288. break;
  289. case SEEK_CUR:
  290. start = filp->f_pos;
  291. break;
  292. case SEEK_END:
  293. start = i_size_read(filp->f_path.dentry->d_inode);
  294. break;
  295. default:
  296. return -EINVAL;
  297. }
  298. /* POSIX-1996 leaves the case l->l_len < 0 undefined;
  299. POSIX-2001 defines it. */
  300. start += l->l_start;
  301. if (start < 0)
  302. return -EINVAL;
  303. fl->fl_end = OFFSET_MAX;
  304. if (l->l_len > 0) {
  305. end = start + l->l_len - 1;
  306. fl->fl_end = end;
  307. } else if (l->l_len < 0) {
  308. end = start - 1;
  309. fl->fl_end = end;
  310. start += l->l_len;
  311. if (start < 0)
  312. return -EINVAL;
  313. }
  314. fl->fl_start = start; /* we record the absolute position */
  315. if (fl->fl_end < fl->fl_start)
  316. return -EOVERFLOW;
  317. fl->fl_owner = current->files;
  318. fl->fl_pid = current->tgid;
  319. fl->fl_file = filp;
  320. fl->fl_flags = FL_POSIX;
  321. fl->fl_ops = NULL;
  322. fl->fl_lmops = NULL;
  323. return assign_type(fl, l->l_type);
  324. }
  325. #if BITS_PER_LONG == 32
  326. static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
  327. struct flock64 *l)
  328. {
  329. loff_t start;
  330. switch (l->l_whence) {
  331. case SEEK_SET:
  332. start = 0;
  333. break;
  334. case SEEK_CUR:
  335. start = filp->f_pos;
  336. break;
  337. case SEEK_END:
  338. start = i_size_read(filp->f_path.dentry->d_inode);
  339. break;
  340. default:
  341. return -EINVAL;
  342. }
  343. start += l->l_start;
  344. if (start < 0)
  345. return -EINVAL;
  346. fl->fl_end = OFFSET_MAX;
  347. if (l->l_len > 0) {
  348. fl->fl_end = start + l->l_len - 1;
  349. } else if (l->l_len < 0) {
  350. fl->fl_end = start - 1;
  351. start += l->l_len;
  352. if (start < 0)
  353. return -EINVAL;
  354. }
  355. fl->fl_start = start; /* we record the absolute position */
  356. if (fl->fl_end < fl->fl_start)
  357. return -EOVERFLOW;
  358. fl->fl_owner = current->files;
  359. fl->fl_pid = current->tgid;
  360. fl->fl_file = filp;
  361. fl->fl_flags = FL_POSIX;
  362. fl->fl_ops = NULL;
  363. fl->fl_lmops = NULL;
  364. switch (l->l_type) {
  365. case F_RDLCK:
  366. case F_WRLCK:
  367. case F_UNLCK:
  368. fl->fl_type = l->l_type;
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. return (0);
  374. }
  375. #endif
  376. /* default lease lock manager operations */
  377. static void lease_break_callback(struct file_lock *fl)
  378. {
  379. kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
  380. }
  381. static void lease_release_private_callback(struct file_lock *fl)
  382. {
  383. if (!fl->fl_file)
  384. return;
  385. f_delown(fl->fl_file);
  386. fl->fl_file->f_owner.signum = 0;
  387. }
  388. static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try)
  389. {
  390. return fl->fl_file == try->fl_file;
  391. }
  392. static struct lock_manager_operations lease_manager_ops = {
  393. .fl_break = lease_break_callback,
  394. .fl_release_private = lease_release_private_callback,
  395. .fl_mylease = lease_mylease_callback,
  396. .fl_change = lease_modify,
  397. };
  398. /*
  399. * Initialize a lease, use the default lock manager operations
  400. */
  401. static int lease_init(struct file *filp, int type, struct file_lock *fl)
  402. {
  403. if (assign_type(fl, type) != 0)
  404. return -EINVAL;
  405. fl->fl_owner = current->files;
  406. fl->fl_pid = current->tgid;
  407. fl->fl_file = filp;
  408. fl->fl_flags = FL_LEASE;
  409. fl->fl_start = 0;
  410. fl->fl_end = OFFSET_MAX;
  411. fl->fl_ops = NULL;
  412. fl->fl_lmops = &lease_manager_ops;
  413. return 0;
  414. }
  415. /* Allocate a file_lock initialised to this type of lease */
  416. static struct file_lock *lease_alloc(struct file *filp, int type)
  417. {
  418. struct file_lock *fl = locks_alloc_lock();
  419. int error = -ENOMEM;
  420. if (fl == NULL)
  421. return ERR_PTR(error);
  422. error = lease_init(filp, type, fl);
  423. if (error) {
  424. locks_free_lock(fl);
  425. return ERR_PTR(error);
  426. }
  427. return fl;
  428. }
  429. /* Check if two locks overlap each other.
  430. */
  431. static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
  432. {
  433. return ((fl1->fl_end >= fl2->fl_start) &&
  434. (fl2->fl_end >= fl1->fl_start));
  435. }
  436. /*
  437. * Check whether two locks have the same owner.
  438. */
  439. static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  440. {
  441. if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner)
  442. return fl2->fl_lmops == fl1->fl_lmops &&
  443. fl1->fl_lmops->fl_compare_owner(fl1, fl2);
  444. return fl1->fl_owner == fl2->fl_owner;
  445. }
  446. /* Remove waiter from blocker's block list.
  447. * When blocker ends up pointing to itself then the list is empty.
  448. */
  449. static void __locks_delete_block(struct file_lock *waiter)
  450. {
  451. list_del_init(&waiter->fl_block);
  452. list_del_init(&waiter->fl_link);
  453. waiter->fl_next = NULL;
  454. }
  455. /*
  456. */
  457. static void locks_delete_block(struct file_lock *waiter)
  458. {
  459. lock_kernel();
  460. __locks_delete_block(waiter);
  461. unlock_kernel();
  462. }
  463. /* Insert waiter into blocker's block list.
  464. * We use a circular list so that processes can be easily woken up in
  465. * the order they blocked. The documentation doesn't require this but
  466. * it seems like the reasonable thing to do.
  467. */
  468. static void locks_insert_block(struct file_lock *blocker,
  469. struct file_lock *waiter)
  470. {
  471. BUG_ON(!list_empty(&waiter->fl_block));
  472. list_add_tail(&waiter->fl_block, &blocker->fl_block);
  473. waiter->fl_next = blocker;
  474. if (IS_POSIX(blocker))
  475. list_add(&waiter->fl_link, &blocked_list);
  476. }
  477. /* Wake up processes blocked waiting for blocker.
  478. * If told to wait then schedule the processes until the block list
  479. * is empty, otherwise empty the block list ourselves.
  480. */
  481. static void locks_wake_up_blocks(struct file_lock *blocker)
  482. {
  483. while (!list_empty(&blocker->fl_block)) {
  484. struct file_lock *waiter;
  485. waiter = list_first_entry(&blocker->fl_block,
  486. struct file_lock, fl_block);
  487. __locks_delete_block(waiter);
  488. if (waiter->fl_lmops && waiter->fl_lmops->fl_notify)
  489. waiter->fl_lmops->fl_notify(waiter);
  490. else
  491. wake_up(&waiter->fl_wait);
  492. }
  493. }
  494. /* Insert file lock fl into an inode's lock list at the position indicated
  495. * by pos. At the same time add the lock to the global file lock list.
  496. */
  497. static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
  498. {
  499. list_add(&fl->fl_link, &file_lock_list);
  500. /* insert into file's list */
  501. fl->fl_next = *pos;
  502. *pos = fl;
  503. if (fl->fl_ops && fl->fl_ops->fl_insert)
  504. fl->fl_ops->fl_insert(fl);
  505. }
  506. /*
  507. * Delete a lock and then free it.
  508. * Wake up processes that are blocked waiting for this lock,
  509. * notify the FS that the lock has been cleared and
  510. * finally free the lock.
  511. */
  512. static void locks_delete_lock(struct file_lock **thisfl_p)
  513. {
  514. struct file_lock *fl = *thisfl_p;
  515. *thisfl_p = fl->fl_next;
  516. fl->fl_next = NULL;
  517. list_del_init(&fl->fl_link);
  518. fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
  519. if (fl->fl_fasync != NULL) {
  520. printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
  521. fl->fl_fasync = NULL;
  522. }
  523. if (fl->fl_ops && fl->fl_ops->fl_remove)
  524. fl->fl_ops->fl_remove(fl);
  525. locks_wake_up_blocks(fl);
  526. locks_free_lock(fl);
  527. }
  528. /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
  529. * checks for shared/exclusive status of overlapping locks.
  530. */
  531. static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  532. {
  533. if (sys_fl->fl_type == F_WRLCK)
  534. return 1;
  535. if (caller_fl->fl_type == F_WRLCK)
  536. return 1;
  537. return 0;
  538. }
  539. /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
  540. * checking before calling the locks_conflict().
  541. */
  542. static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  543. {
  544. /* POSIX locks owned by the same process do not conflict with
  545. * each other.
  546. */
  547. if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
  548. return (0);
  549. /* Check whether they overlap */
  550. if (!locks_overlap(caller_fl, sys_fl))
  551. return 0;
  552. return (locks_conflict(caller_fl, sys_fl));
  553. }
  554. /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  555. * checking before calling the locks_conflict().
  556. */
  557. static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  558. {
  559. /* FLOCK locks referring to the same filp do not conflict with
  560. * each other.
  561. */
  562. if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
  563. return (0);
  564. if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
  565. return 0;
  566. return (locks_conflict(caller_fl, sys_fl));
  567. }
  568. void
  569. posix_test_lock(struct file *filp, struct file_lock *fl)
  570. {
  571. struct file_lock *cfl;
  572. lock_kernel();
  573. for (cfl = filp->f_path.dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
  574. if (!IS_POSIX(cfl))
  575. continue;
  576. if (posix_locks_conflict(fl, cfl))
  577. break;
  578. }
  579. if (cfl)
  580. __locks_copy_lock(fl, cfl);
  581. else
  582. fl->fl_type = F_UNLCK;
  583. unlock_kernel();
  584. return;
  585. }
  586. EXPORT_SYMBOL(posix_test_lock);
  587. /* This function tests for deadlock condition before putting a process to
  588. * sleep. The detection scheme is no longer recursive. Recursive was neat,
  589. * but dangerous - we risked stack corruption if the lock data was bad, or
  590. * if the recursion was too deep for any other reason.
  591. *
  592. * We rely on the fact that a task can only be on one lock's wait queue
  593. * at a time. When we find blocked_task on a wait queue we can re-search
  594. * with blocked_task equal to that queue's owner, until either blocked_task
  595. * isn't found, or blocked_task is found on a queue owned by my_task.
  596. *
  597. * Note: the above assumption may not be true when handling lock requests
  598. * from a broken NFS client. But broken NFS clients have a lot more to
  599. * worry about than proper deadlock detection anyway... --okir
  600. *
  601. * However, the failure of this assumption (also possible in the case of
  602. * multiple tasks sharing the same open file table) also means there's no
  603. * guarantee that the loop below will terminate. As a hack, we give up
  604. * after a few iterations.
  605. */
  606. #define MAX_DEADLK_ITERATIONS 10
  607. static int posix_locks_deadlock(struct file_lock *caller_fl,
  608. struct file_lock *block_fl)
  609. {
  610. struct list_head *tmp;
  611. int i = 0;
  612. next_task:
  613. if (posix_same_owner(caller_fl, block_fl))
  614. return 1;
  615. list_for_each(tmp, &blocked_list) {
  616. struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
  617. if (posix_same_owner(fl, block_fl)) {
  618. if (i++ > MAX_DEADLK_ITERATIONS)
  619. return 0;
  620. fl = fl->fl_next;
  621. block_fl = fl;
  622. goto next_task;
  623. }
  624. }
  625. return 0;
  626. }
  627. /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
  628. * after any leases, but before any posix locks.
  629. *
  630. * Note that if called with an FL_EXISTS argument, the caller may determine
  631. * whether or not a lock was successfully freed by testing the return
  632. * value for -ENOENT.
  633. */
  634. static int flock_lock_file(struct file *filp, struct file_lock *request)
  635. {
  636. struct file_lock *new_fl = NULL;
  637. struct file_lock **before;
  638. struct inode * inode = filp->f_path.dentry->d_inode;
  639. int error = 0;
  640. int found = 0;
  641. lock_kernel();
  642. if (request->fl_flags & FL_ACCESS)
  643. goto find_conflict;
  644. if (request->fl_type != F_UNLCK) {
  645. error = -ENOMEM;
  646. new_fl = locks_alloc_lock();
  647. if (new_fl == NULL)
  648. goto out;
  649. error = 0;
  650. }
  651. for_each_lock(inode, before) {
  652. struct file_lock *fl = *before;
  653. if (IS_POSIX(fl))
  654. break;
  655. if (IS_LEASE(fl))
  656. continue;
  657. if (filp != fl->fl_file)
  658. continue;
  659. if (request->fl_type == fl->fl_type)
  660. goto out;
  661. found = 1;
  662. locks_delete_lock(before);
  663. break;
  664. }
  665. if (request->fl_type == F_UNLCK) {
  666. if ((request->fl_flags & FL_EXISTS) && !found)
  667. error = -ENOENT;
  668. goto out;
  669. }
  670. /*
  671. * If a higher-priority process was blocked on the old file lock,
  672. * give it the opportunity to lock the file.
  673. */
  674. if (found)
  675. cond_resched();
  676. find_conflict:
  677. for_each_lock(inode, before) {
  678. struct file_lock *fl = *before;
  679. if (IS_POSIX(fl))
  680. break;
  681. if (IS_LEASE(fl))
  682. continue;
  683. if (!flock_locks_conflict(request, fl))
  684. continue;
  685. error = -EAGAIN;
  686. if (request->fl_flags & FL_SLEEP)
  687. locks_insert_block(fl, request);
  688. goto out;
  689. }
  690. if (request->fl_flags & FL_ACCESS)
  691. goto out;
  692. locks_copy_lock(new_fl, request);
  693. locks_insert_lock(&inode->i_flock, new_fl);
  694. new_fl = NULL;
  695. error = 0;
  696. out:
  697. unlock_kernel();
  698. if (new_fl)
  699. locks_free_lock(new_fl);
  700. return error;
  701. }
  702. static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
  703. {
  704. struct file_lock *fl;
  705. struct file_lock *new_fl = NULL;
  706. struct file_lock *new_fl2 = NULL;
  707. struct file_lock *left = NULL;
  708. struct file_lock *right = NULL;
  709. struct file_lock **before;
  710. int error, added = 0;
  711. /*
  712. * We may need two file_lock structures for this operation,
  713. * so we get them in advance to avoid races.
  714. *
  715. * In some cases we can be sure, that no new locks will be needed
  716. */
  717. if (!(request->fl_flags & FL_ACCESS) &&
  718. (request->fl_type != F_UNLCK ||
  719. request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
  720. new_fl = locks_alloc_lock();
  721. new_fl2 = locks_alloc_lock();
  722. }
  723. lock_kernel();
  724. if (request->fl_type != F_UNLCK) {
  725. for_each_lock(inode, before) {
  726. fl = *before;
  727. if (!IS_POSIX(fl))
  728. continue;
  729. if (!posix_locks_conflict(request, fl))
  730. continue;
  731. if (conflock)
  732. locks_copy_lock(conflock, fl);
  733. error = -EAGAIN;
  734. if (!(request->fl_flags & FL_SLEEP))
  735. goto out;
  736. error = -EDEADLK;
  737. if (posix_locks_deadlock(request, fl))
  738. goto out;
  739. error = -EAGAIN;
  740. locks_insert_block(fl, request);
  741. goto out;
  742. }
  743. }
  744. /* If we're just looking for a conflict, we're done. */
  745. error = 0;
  746. if (request->fl_flags & FL_ACCESS)
  747. goto out;
  748. /*
  749. * Find the first old lock with the same owner as the new lock.
  750. */
  751. before = &inode->i_flock;
  752. /* First skip locks owned by other processes. */
  753. while ((fl = *before) && (!IS_POSIX(fl) ||
  754. !posix_same_owner(request, fl))) {
  755. before = &fl->fl_next;
  756. }
  757. /* Process locks with this owner. */
  758. while ((fl = *before) && posix_same_owner(request, fl)) {
  759. /* Detect adjacent or overlapping regions (if same lock type)
  760. */
  761. if (request->fl_type == fl->fl_type) {
  762. /* In all comparisons of start vs end, use
  763. * "start - 1" rather than "end + 1". If end
  764. * is OFFSET_MAX, end + 1 will become negative.
  765. */
  766. if (fl->fl_end < request->fl_start - 1)
  767. goto next_lock;
  768. /* If the next lock in the list has entirely bigger
  769. * addresses than the new one, insert the lock here.
  770. */
  771. if (fl->fl_start - 1 > request->fl_end)
  772. break;
  773. /* If we come here, the new and old lock are of the
  774. * same type and adjacent or overlapping. Make one
  775. * lock yielding from the lower start address of both
  776. * locks to the higher end address.
  777. */
  778. if (fl->fl_start > request->fl_start)
  779. fl->fl_start = request->fl_start;
  780. else
  781. request->fl_start = fl->fl_start;
  782. if (fl->fl_end < request->fl_end)
  783. fl->fl_end = request->fl_end;
  784. else
  785. request->fl_end = fl->fl_end;
  786. if (added) {
  787. locks_delete_lock(before);
  788. continue;
  789. }
  790. request = fl;
  791. added = 1;
  792. }
  793. else {
  794. /* Processing for different lock types is a bit
  795. * more complex.
  796. */
  797. if (fl->fl_end < request->fl_start)
  798. goto next_lock;
  799. if (fl->fl_start > request->fl_end)
  800. break;
  801. if (request->fl_type == F_UNLCK)
  802. added = 1;
  803. if (fl->fl_start < request->fl_start)
  804. left = fl;
  805. /* If the next lock in the list has a higher end
  806. * address than the new one, insert the new one here.
  807. */
  808. if (fl->fl_end > request->fl_end) {
  809. right = fl;
  810. break;
  811. }
  812. if (fl->fl_start >= request->fl_start) {
  813. /* The new lock completely replaces an old
  814. * one (This may happen several times).
  815. */
  816. if (added) {
  817. locks_delete_lock(before);
  818. continue;
  819. }
  820. /* Replace the old lock with the new one.
  821. * Wake up anybody waiting for the old one,
  822. * as the change in lock type might satisfy
  823. * their needs.
  824. */
  825. locks_wake_up_blocks(fl);
  826. fl->fl_start = request->fl_start;
  827. fl->fl_end = request->fl_end;
  828. fl->fl_type = request->fl_type;
  829. locks_release_private(fl);
  830. locks_copy_private(fl, request);
  831. request = fl;
  832. added = 1;
  833. }
  834. }
  835. /* Go on to next lock.
  836. */
  837. next_lock:
  838. before = &fl->fl_next;
  839. }
  840. /*
  841. * The above code only modifies existing locks in case of
  842. * merging or replacing. If new lock(s) need to be inserted
  843. * all modifications are done bellow this, so it's safe yet to
  844. * bail out.
  845. */
  846. error = -ENOLCK; /* "no luck" */
  847. if (right && left == right && !new_fl2)
  848. goto out;
  849. error = 0;
  850. if (!added) {
  851. if (request->fl_type == F_UNLCK) {
  852. if (request->fl_flags & FL_EXISTS)
  853. error = -ENOENT;
  854. goto out;
  855. }
  856. if (!new_fl) {
  857. error = -ENOLCK;
  858. goto out;
  859. }
  860. locks_copy_lock(new_fl, request);
  861. locks_insert_lock(before, new_fl);
  862. new_fl = NULL;
  863. }
  864. if (right) {
  865. if (left == right) {
  866. /* The new lock breaks the old one in two pieces,
  867. * so we have to use the second new lock.
  868. */
  869. left = new_fl2;
  870. new_fl2 = NULL;
  871. locks_copy_lock(left, right);
  872. locks_insert_lock(before, left);
  873. }
  874. right->fl_start = request->fl_end + 1;
  875. locks_wake_up_blocks(right);
  876. }
  877. if (left) {
  878. left->fl_end = request->fl_start - 1;
  879. locks_wake_up_blocks(left);
  880. }
  881. out:
  882. unlock_kernel();
  883. /*
  884. * Free any unused locks.
  885. */
  886. if (new_fl)
  887. locks_free_lock(new_fl);
  888. if (new_fl2)
  889. locks_free_lock(new_fl2);
  890. return error;
  891. }
  892. /**
  893. * posix_lock_file - Apply a POSIX-style lock to a file
  894. * @filp: The file to apply the lock to
  895. * @fl: The lock to be applied
  896. * @conflock: Place to return a copy of the conflicting lock, if found.
  897. *
  898. * Add a POSIX style lock to a file.
  899. * We merge adjacent & overlapping locks whenever possible.
  900. * POSIX locks are sorted by owner task, then by starting address
  901. *
  902. * Note that if called with an FL_EXISTS argument, the caller may determine
  903. * whether or not a lock was successfully freed by testing the return
  904. * value for -ENOENT.
  905. */
  906. int posix_lock_file(struct file *filp, struct file_lock *fl,
  907. struct file_lock *conflock)
  908. {
  909. return __posix_lock_file(filp->f_path.dentry->d_inode, fl, conflock);
  910. }
  911. EXPORT_SYMBOL(posix_lock_file);
  912. /**
  913. * posix_lock_file_wait - Apply a POSIX-style lock to a file
  914. * @filp: The file to apply the lock to
  915. * @fl: The lock to be applied
  916. *
  917. * Add a POSIX style lock to a file.
  918. * We merge adjacent & overlapping locks whenever possible.
  919. * POSIX locks are sorted by owner task, then by starting address
  920. */
  921. int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
  922. {
  923. int error;
  924. might_sleep ();
  925. for (;;) {
  926. error = posix_lock_file(filp, fl, NULL);
  927. if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
  928. break;
  929. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  930. if (!error)
  931. continue;
  932. locks_delete_block(fl);
  933. break;
  934. }
  935. return error;
  936. }
  937. EXPORT_SYMBOL(posix_lock_file_wait);
  938. /**
  939. * locks_mandatory_locked - Check for an active lock
  940. * @inode: the file to check
  941. *
  942. * Searches the inode's list of locks to find any POSIX locks which conflict.
  943. * This function is called from locks_verify_locked() only.
  944. */
  945. int locks_mandatory_locked(struct inode *inode)
  946. {
  947. fl_owner_t owner = current->files;
  948. struct file_lock *fl;
  949. /*
  950. * Search the lock list for this inode for any POSIX locks.
  951. */
  952. lock_kernel();
  953. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  954. if (!IS_POSIX(fl))
  955. continue;
  956. if (fl->fl_owner != owner)
  957. break;
  958. }
  959. unlock_kernel();
  960. return fl ? -EAGAIN : 0;
  961. }
  962. /**
  963. * locks_mandatory_area - Check for a conflicting lock
  964. * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
  965. * for shared
  966. * @inode: the file to check
  967. * @filp: how the file was opened (if it was)
  968. * @offset: start of area to check
  969. * @count: length of area to check
  970. *
  971. * Searches the inode's list of locks to find any POSIX locks which conflict.
  972. * This function is called from rw_verify_area() and
  973. * locks_verify_truncate().
  974. */
  975. int locks_mandatory_area(int read_write, struct inode *inode,
  976. struct file *filp, loff_t offset,
  977. size_t count)
  978. {
  979. struct file_lock fl;
  980. int error;
  981. locks_init_lock(&fl);
  982. fl.fl_owner = current->files;
  983. fl.fl_pid = current->tgid;
  984. fl.fl_file = filp;
  985. fl.fl_flags = FL_POSIX | FL_ACCESS;
  986. if (filp && !(filp->f_flags & O_NONBLOCK))
  987. fl.fl_flags |= FL_SLEEP;
  988. fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
  989. fl.fl_start = offset;
  990. fl.fl_end = offset + count - 1;
  991. for (;;) {
  992. error = __posix_lock_file(inode, &fl, NULL);
  993. if (error != -EAGAIN)
  994. break;
  995. if (!(fl.fl_flags & FL_SLEEP))
  996. break;
  997. error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
  998. if (!error) {
  999. /*
  1000. * If we've been sleeping someone might have
  1001. * changed the permissions behind our back.
  1002. */
  1003. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  1004. continue;
  1005. }
  1006. locks_delete_block(&fl);
  1007. break;
  1008. }
  1009. return error;
  1010. }
  1011. EXPORT_SYMBOL(locks_mandatory_area);
  1012. /* We already had a lease on this file; just change its type */
  1013. int lease_modify(struct file_lock **before, int arg)
  1014. {
  1015. struct file_lock *fl = *before;
  1016. int error = assign_type(fl, arg);
  1017. if (error)
  1018. return error;
  1019. locks_wake_up_blocks(fl);
  1020. if (arg == F_UNLCK)
  1021. locks_delete_lock(before);
  1022. return 0;
  1023. }
  1024. EXPORT_SYMBOL(lease_modify);
  1025. static void time_out_leases(struct inode *inode)
  1026. {
  1027. struct file_lock **before;
  1028. struct file_lock *fl;
  1029. before = &inode->i_flock;
  1030. while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) {
  1031. if ((fl->fl_break_time == 0)
  1032. || time_before(jiffies, fl->fl_break_time)) {
  1033. before = &fl->fl_next;
  1034. continue;
  1035. }
  1036. lease_modify(before, fl->fl_type & ~F_INPROGRESS);
  1037. if (fl == *before) /* lease_modify may have freed fl */
  1038. before = &fl->fl_next;
  1039. }
  1040. }
  1041. /**
  1042. * __break_lease - revoke all outstanding leases on file
  1043. * @inode: the inode of the file to return
  1044. * @mode: the open mode (read or write)
  1045. *
  1046. * break_lease (inlined for speed) has checked there already
  1047. * is a lease on this file. Leases are broken on a call to open()
  1048. * or truncate(). This function can sleep unless you
  1049. * specified %O_NONBLOCK to your open().
  1050. */
  1051. int __break_lease(struct inode *inode, unsigned int mode)
  1052. {
  1053. int error = 0, future;
  1054. struct file_lock *new_fl, *flock;
  1055. struct file_lock *fl;
  1056. unsigned long break_time;
  1057. int i_have_this_lease = 0;
  1058. new_fl = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK);
  1059. lock_kernel();
  1060. time_out_leases(inode);
  1061. flock = inode->i_flock;
  1062. if ((flock == NULL) || !IS_LEASE(flock))
  1063. goto out;
  1064. for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
  1065. if (fl->fl_owner == current->files)
  1066. i_have_this_lease = 1;
  1067. if (mode & FMODE_WRITE) {
  1068. /* If we want write access, we have to revoke any lease. */
  1069. future = F_UNLCK | F_INPROGRESS;
  1070. } else if (flock->fl_type & F_INPROGRESS) {
  1071. /* If the lease is already being broken, we just leave it */
  1072. future = flock->fl_type;
  1073. } else if (flock->fl_type & F_WRLCK) {
  1074. /* Downgrade the exclusive lease to a read-only lease. */
  1075. future = F_RDLCK | F_INPROGRESS;
  1076. } else {
  1077. /* the existing lease was read-only, so we can read too. */
  1078. goto out;
  1079. }
  1080. if (IS_ERR(new_fl) && !i_have_this_lease
  1081. && ((mode & O_NONBLOCK) == 0)) {
  1082. error = PTR_ERR(new_fl);
  1083. goto out;
  1084. }
  1085. break_time = 0;
  1086. if (lease_break_time > 0) {
  1087. break_time = jiffies + lease_break_time * HZ;
  1088. if (break_time == 0)
  1089. break_time++; /* so that 0 means no break time */
  1090. }
  1091. for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
  1092. if (fl->fl_type != future) {
  1093. fl->fl_type = future;
  1094. fl->fl_break_time = break_time;
  1095. /* lease must have lmops break callback */
  1096. fl->fl_lmops->fl_break(fl);
  1097. }
  1098. }
  1099. if (i_have_this_lease || (mode & O_NONBLOCK)) {
  1100. error = -EWOULDBLOCK;
  1101. goto out;
  1102. }
  1103. restart:
  1104. break_time = flock->fl_break_time;
  1105. if (break_time != 0) {
  1106. break_time -= jiffies;
  1107. if (break_time == 0)
  1108. break_time++;
  1109. }
  1110. locks_insert_block(flock, new_fl);
  1111. error = wait_event_interruptible_timeout(new_fl->fl_wait,
  1112. !new_fl->fl_next, break_time);
  1113. __locks_delete_block(new_fl);
  1114. if (error >= 0) {
  1115. if (error == 0)
  1116. time_out_leases(inode);
  1117. /* Wait for the next lease that has not been broken yet */
  1118. for (flock = inode->i_flock; flock && IS_LEASE(flock);
  1119. flock = flock->fl_next) {
  1120. if (flock->fl_type & F_INPROGRESS)
  1121. goto restart;
  1122. }
  1123. error = 0;
  1124. }
  1125. out:
  1126. unlock_kernel();
  1127. if (!IS_ERR(new_fl))
  1128. locks_free_lock(new_fl);
  1129. return error;
  1130. }
  1131. EXPORT_SYMBOL(__break_lease);
  1132. /**
  1133. * lease_get_mtime
  1134. * @inode: the inode
  1135. * @time: pointer to a timespec which will contain the last modified time
  1136. *
  1137. * This is to force NFS clients to flush their caches for files with
  1138. * exclusive leases. The justification is that if someone has an
  1139. * exclusive lease, then they could be modifiying it.
  1140. */
  1141. void lease_get_mtime(struct inode *inode, struct timespec *time)
  1142. {
  1143. struct file_lock *flock = inode->i_flock;
  1144. if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
  1145. *time = current_fs_time(inode->i_sb);
  1146. else
  1147. *time = inode->i_mtime;
  1148. }
  1149. EXPORT_SYMBOL(lease_get_mtime);
  1150. /**
  1151. * fcntl_getlease - Enquire what lease is currently active
  1152. * @filp: the file
  1153. *
  1154. * The value returned by this function will be one of
  1155. * (if no lease break is pending):
  1156. *
  1157. * %F_RDLCK to indicate a shared lease is held.
  1158. *
  1159. * %F_WRLCK to indicate an exclusive lease is held.
  1160. *
  1161. * %F_UNLCK to indicate no lease is held.
  1162. *
  1163. * (if a lease break is pending):
  1164. *
  1165. * %F_RDLCK to indicate an exclusive lease needs to be
  1166. * changed to a shared lease (or removed).
  1167. *
  1168. * %F_UNLCK to indicate the lease needs to be removed.
  1169. *
  1170. * XXX: sfr & willy disagree over whether F_INPROGRESS
  1171. * should be returned to userspace.
  1172. */
  1173. int fcntl_getlease(struct file *filp)
  1174. {
  1175. struct file_lock *fl;
  1176. int type = F_UNLCK;
  1177. lock_kernel();
  1178. time_out_leases(filp->f_path.dentry->d_inode);
  1179. for (fl = filp->f_path.dentry->d_inode->i_flock; fl && IS_LEASE(fl);
  1180. fl = fl->fl_next) {
  1181. if (fl->fl_file == filp) {
  1182. type = fl->fl_type & ~F_INPROGRESS;
  1183. break;
  1184. }
  1185. }
  1186. unlock_kernel();
  1187. return type;
  1188. }
  1189. /**
  1190. * setlease - sets a lease on an open file
  1191. * @filp: file pointer
  1192. * @arg: type of lease to obtain
  1193. * @flp: input - file_lock to use, output - file_lock inserted
  1194. *
  1195. * The (input) flp->fl_lmops->fl_break function is required
  1196. * by break_lease().
  1197. *
  1198. * Called with kernel lock held.
  1199. */
  1200. int setlease(struct file *filp, long arg, struct file_lock **flp)
  1201. {
  1202. struct file_lock *fl, **before, **my_before = NULL, *lease;
  1203. struct file_lock *new_fl = NULL;
  1204. struct dentry *dentry = filp->f_path.dentry;
  1205. struct inode *inode = dentry->d_inode;
  1206. int error, rdlease_count = 0, wrlease_count = 0;
  1207. if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
  1208. return -EACCES;
  1209. if (!S_ISREG(inode->i_mode))
  1210. return -EINVAL;
  1211. error = security_file_lock(filp, arg);
  1212. if (error)
  1213. return error;
  1214. time_out_leases(inode);
  1215. BUG_ON(!(*flp)->fl_lmops->fl_break);
  1216. lease = *flp;
  1217. error = -EAGAIN;
  1218. if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
  1219. goto out;
  1220. if ((arg == F_WRLCK)
  1221. && ((atomic_read(&dentry->d_count) > 1)
  1222. || (atomic_read(&inode->i_count) > 1)))
  1223. goto out;
  1224. error = -ENOMEM;
  1225. new_fl = locks_alloc_lock();
  1226. if (new_fl == NULL)
  1227. goto out;
  1228. /*
  1229. * At this point, we know that if there is an exclusive
  1230. * lease on this file, then we hold it on this filp
  1231. * (otherwise our open of this file would have blocked).
  1232. * And if we are trying to acquire an exclusive lease,
  1233. * then the file is not open by anyone (including us)
  1234. * except for this filp.
  1235. */
  1236. for (before = &inode->i_flock;
  1237. ((fl = *before) != NULL) && IS_LEASE(fl);
  1238. before = &fl->fl_next) {
  1239. if (lease->fl_lmops->fl_mylease(fl, lease))
  1240. my_before = before;
  1241. else if (fl->fl_type == (F_INPROGRESS | F_UNLCK))
  1242. /*
  1243. * Someone is in the process of opening this
  1244. * file for writing so we may not take an
  1245. * exclusive lease on it.
  1246. */
  1247. wrlease_count++;
  1248. else
  1249. rdlease_count++;
  1250. }
  1251. error = -EAGAIN;
  1252. if ((arg == F_RDLCK && (wrlease_count > 0)) ||
  1253. (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
  1254. goto out;
  1255. if (my_before != NULL) {
  1256. *flp = *my_before;
  1257. error = lease->fl_lmops->fl_change(my_before, arg);
  1258. goto out;
  1259. }
  1260. error = 0;
  1261. if (arg == F_UNLCK)
  1262. goto out;
  1263. error = -EINVAL;
  1264. if (!leases_enable)
  1265. goto out;
  1266. locks_copy_lock(new_fl, lease);
  1267. locks_insert_lock(before, new_fl);
  1268. *flp = new_fl;
  1269. return 0;
  1270. out:
  1271. if (new_fl != NULL)
  1272. locks_free_lock(new_fl);
  1273. return error;
  1274. }
  1275. EXPORT_SYMBOL(setlease);
  1276. /**
  1277. * vfs_setlease - sets a lease on an open file
  1278. * @filp: file pointer
  1279. * @arg: type of lease to obtain
  1280. * @lease: file_lock to use
  1281. *
  1282. * Call this to establish a lease on the file.
  1283. * The (*lease)->fl_lmops->fl_break operation must be set; if not,
  1284. * break_lease will oops!
  1285. *
  1286. * This will call the filesystem's setlease file method, if
  1287. * defined. Note that there is no getlease method; instead, the
  1288. * filesystem setlease method should call back to setlease() to
  1289. * add a lease to the inode's lease list, where fcntl_getlease() can
  1290. * find it. Since fcntl_getlease() only reports whether the current
  1291. * task holds a lease, a cluster filesystem need only do this for
  1292. * leases held by processes on this node.
  1293. *
  1294. * There is also no break_lease method; filesystems that
  1295. * handle their own leases shoud break leases themselves from the
  1296. * filesystem's open, create, and (on truncate) setattr methods.
  1297. *
  1298. * Warning: the only current setlease methods exist only to disable
  1299. * leases in certain cases. More vfs changes may be required to
  1300. * allow a full filesystem lease implementation.
  1301. */
  1302. int vfs_setlease(struct file *filp, long arg, struct file_lock **lease)
  1303. {
  1304. int error;
  1305. lock_kernel();
  1306. if (filp->f_op && filp->f_op->setlease)
  1307. error = filp->f_op->setlease(filp, arg, lease);
  1308. else
  1309. error = setlease(filp, arg, lease);
  1310. unlock_kernel();
  1311. return error;
  1312. }
  1313. EXPORT_SYMBOL_GPL(vfs_setlease);
  1314. /**
  1315. * fcntl_setlease - sets a lease on an open file
  1316. * @fd: open file descriptor
  1317. * @filp: file pointer
  1318. * @arg: type of lease to obtain
  1319. *
  1320. * Call this fcntl to establish a lease on the file.
  1321. * Note that you also need to call %F_SETSIG to
  1322. * receive a signal when the lease is broken.
  1323. */
  1324. int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
  1325. {
  1326. struct file_lock fl, *flp = &fl;
  1327. struct dentry *dentry = filp->f_path.dentry;
  1328. struct inode *inode = dentry->d_inode;
  1329. int error;
  1330. locks_init_lock(&fl);
  1331. error = lease_init(filp, arg, &fl);
  1332. if (error)
  1333. return error;
  1334. lock_kernel();
  1335. error = vfs_setlease(filp, arg, &flp);
  1336. if (error || arg == F_UNLCK)
  1337. goto out_unlock;
  1338. error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
  1339. if (error < 0) {
  1340. /* remove lease just inserted by setlease */
  1341. flp->fl_type = F_UNLCK | F_INPROGRESS;
  1342. flp->fl_break_time = jiffies - 10;
  1343. time_out_leases(inode);
  1344. goto out_unlock;
  1345. }
  1346. error = __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  1347. out_unlock:
  1348. unlock_kernel();
  1349. return error;
  1350. }
  1351. /**
  1352. * flock_lock_file_wait - Apply a FLOCK-style lock to a file
  1353. * @filp: The file to apply the lock to
  1354. * @fl: The lock to be applied
  1355. *
  1356. * Add a FLOCK style lock to a file.
  1357. */
  1358. int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
  1359. {
  1360. int error;
  1361. might_sleep();
  1362. for (;;) {
  1363. error = flock_lock_file(filp, fl);
  1364. if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
  1365. break;
  1366. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  1367. if (!error)
  1368. continue;
  1369. locks_delete_block(fl);
  1370. break;
  1371. }
  1372. return error;
  1373. }
  1374. EXPORT_SYMBOL(flock_lock_file_wait);
  1375. /**
  1376. * sys_flock: - flock() system call.
  1377. * @fd: the file descriptor to lock.
  1378. * @cmd: the type of lock to apply.
  1379. *
  1380. * Apply a %FL_FLOCK style lock to an open file descriptor.
  1381. * The @cmd can be one of
  1382. *
  1383. * %LOCK_SH -- a shared lock.
  1384. *
  1385. * %LOCK_EX -- an exclusive lock.
  1386. *
  1387. * %LOCK_UN -- remove an existing lock.
  1388. *
  1389. * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
  1390. *
  1391. * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
  1392. * processes read and write access respectively.
  1393. */
  1394. asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
  1395. {
  1396. struct file *filp;
  1397. struct file_lock *lock;
  1398. int can_sleep, unlock;
  1399. int error;
  1400. error = -EBADF;
  1401. filp = fget(fd);
  1402. if (!filp)
  1403. goto out;
  1404. can_sleep = !(cmd & LOCK_NB);
  1405. cmd &= ~LOCK_NB;
  1406. unlock = (cmd == LOCK_UN);
  1407. if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3))
  1408. goto out_putf;
  1409. error = flock_make_lock(filp, &lock, cmd);
  1410. if (error)
  1411. goto out_putf;
  1412. if (can_sleep)
  1413. lock->fl_flags |= FL_SLEEP;
  1414. error = security_file_lock(filp, cmd);
  1415. if (error)
  1416. goto out_free;
  1417. if (filp->f_op && filp->f_op->flock)
  1418. error = filp->f_op->flock(filp,
  1419. (can_sleep) ? F_SETLKW : F_SETLK,
  1420. lock);
  1421. else
  1422. error = flock_lock_file_wait(filp, lock);
  1423. out_free:
  1424. locks_free_lock(lock);
  1425. out_putf:
  1426. fput(filp);
  1427. out:
  1428. return error;
  1429. }
  1430. /**
  1431. * vfs_test_lock - test file byte range lock
  1432. * @filp: The file to test lock for
  1433. * @fl: The lock to test; also used to hold result
  1434. *
  1435. * Returns -ERRNO on failure. Indicates presence of conflicting lock by
  1436. * setting conf->fl_type to something other than F_UNLCK.
  1437. */
  1438. int vfs_test_lock(struct file *filp, struct file_lock *fl)
  1439. {
  1440. if (filp->f_op && filp->f_op->lock)
  1441. return filp->f_op->lock(filp, F_GETLK, fl);
  1442. posix_test_lock(filp, fl);
  1443. return 0;
  1444. }
  1445. EXPORT_SYMBOL_GPL(vfs_test_lock);
  1446. static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
  1447. {
  1448. flock->l_pid = fl->fl_pid;
  1449. #if BITS_PER_LONG == 32
  1450. /*
  1451. * Make sure we can represent the posix lock via
  1452. * legacy 32bit flock.
  1453. */
  1454. if (fl->fl_start > OFFT_OFFSET_MAX)
  1455. return -EOVERFLOW;
  1456. if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
  1457. return -EOVERFLOW;
  1458. #endif
  1459. flock->l_start = fl->fl_start;
  1460. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1461. fl->fl_end - fl->fl_start + 1;
  1462. flock->l_whence = 0;
  1463. flock->l_type = fl->fl_type;
  1464. return 0;
  1465. }
  1466. #if BITS_PER_LONG == 32
  1467. static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
  1468. {
  1469. flock->l_pid = fl->fl_pid;
  1470. flock->l_start = fl->fl_start;
  1471. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1472. fl->fl_end - fl->fl_start + 1;
  1473. flock->l_whence = 0;
  1474. flock->l_type = fl->fl_type;
  1475. }
  1476. #endif
  1477. /* Report the first existing lock that would conflict with l.
  1478. * This implements the F_GETLK command of fcntl().
  1479. */
  1480. int fcntl_getlk(struct file *filp, struct flock __user *l)
  1481. {
  1482. struct file_lock file_lock;
  1483. struct flock flock;
  1484. int error;
  1485. error = -EFAULT;
  1486. if (copy_from_user(&flock, l, sizeof(flock)))
  1487. goto out;
  1488. error = -EINVAL;
  1489. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1490. goto out;
  1491. error = flock_to_posix_lock(filp, &file_lock, &flock);
  1492. if (error)
  1493. goto out;
  1494. error = vfs_test_lock(filp, &file_lock);
  1495. if (error)
  1496. goto out;
  1497. flock.l_type = file_lock.fl_type;
  1498. if (file_lock.fl_type != F_UNLCK) {
  1499. error = posix_lock_to_flock(&flock, &file_lock);
  1500. if (error)
  1501. goto out;
  1502. }
  1503. error = -EFAULT;
  1504. if (!copy_to_user(l, &flock, sizeof(flock)))
  1505. error = 0;
  1506. out:
  1507. return error;
  1508. }
  1509. /**
  1510. * vfs_lock_file - file byte range lock
  1511. * @filp: The file to apply the lock to
  1512. * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
  1513. * @fl: The lock to be applied
  1514. * @conf: Place to return a copy of the conflicting lock, if found.
  1515. *
  1516. * A caller that doesn't care about the conflicting lock may pass NULL
  1517. * as the final argument.
  1518. *
  1519. * If the filesystem defines a private ->lock() method, then @conf will
  1520. * be left unchanged; so a caller that cares should initialize it to
  1521. * some acceptable default.
  1522. *
  1523. * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
  1524. * locks, the ->lock() interface may return asynchronously, before the lock has
  1525. * been granted or denied by the underlying filesystem, if (and only if)
  1526. * fl_grant is set. Callers expecting ->lock() to return asynchronously
  1527. * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
  1528. * the request is for a blocking lock. When ->lock() does return asynchronously,
  1529. * it must return -EINPROGRESS, and call ->fl_grant() when the lock
  1530. * request completes.
  1531. * If the request is for non-blocking lock the file system should return
  1532. * -EINPROGRESS then try to get the lock and call the callback routine with
  1533. * the result. If the request timed out the callback routine will return a
  1534. * nonzero return code and the file system should release the lock. The file
  1535. * system is also responsible to keep a corresponding posix lock when it
  1536. * grants a lock so the VFS can find out which locks are locally held and do
  1537. * the correct lock cleanup when required.
  1538. * The underlying filesystem must not drop the kernel lock or call
  1539. * ->fl_grant() before returning to the caller with a -EINPROGRESS
  1540. * return code.
  1541. */
  1542. int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
  1543. {
  1544. if (filp->f_op && filp->f_op->lock)
  1545. return filp->f_op->lock(filp, cmd, fl);
  1546. else
  1547. return posix_lock_file(filp, fl, conf);
  1548. }
  1549. EXPORT_SYMBOL_GPL(vfs_lock_file);
  1550. /* Apply the lock described by l to an open file descriptor.
  1551. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1552. */
  1553. int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
  1554. struct flock __user *l)
  1555. {
  1556. struct file_lock *file_lock = locks_alloc_lock();
  1557. struct flock flock;
  1558. struct inode *inode;
  1559. int error;
  1560. if (file_lock == NULL)
  1561. return -ENOLCK;
  1562. /*
  1563. * This might block, so we do it before checking the inode.
  1564. */
  1565. error = -EFAULT;
  1566. if (copy_from_user(&flock, l, sizeof(flock)))
  1567. goto out;
  1568. inode = filp->f_path.dentry->d_inode;
  1569. /* Don't allow mandatory locks on files that may be memory mapped
  1570. * and shared.
  1571. */
  1572. if (IS_MANDLOCK(inode) &&
  1573. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1574. mapping_writably_mapped(filp->f_mapping)) {
  1575. error = -EAGAIN;
  1576. goto out;
  1577. }
  1578. again:
  1579. error = flock_to_posix_lock(filp, file_lock, &flock);
  1580. if (error)
  1581. goto out;
  1582. if (cmd == F_SETLKW) {
  1583. file_lock->fl_flags |= FL_SLEEP;
  1584. }
  1585. error = -EBADF;
  1586. switch (flock.l_type) {
  1587. case F_RDLCK:
  1588. if (!(filp->f_mode & FMODE_READ))
  1589. goto out;
  1590. break;
  1591. case F_WRLCK:
  1592. if (!(filp->f_mode & FMODE_WRITE))
  1593. goto out;
  1594. break;
  1595. case F_UNLCK:
  1596. break;
  1597. default:
  1598. error = -EINVAL;
  1599. goto out;
  1600. }
  1601. error = security_file_lock(filp, file_lock->fl_type);
  1602. if (error)
  1603. goto out;
  1604. for (;;) {
  1605. error = vfs_lock_file(filp, cmd, file_lock, NULL);
  1606. if (error != -EAGAIN || cmd == F_SETLK)
  1607. break;
  1608. error = wait_event_interruptible(file_lock->fl_wait,
  1609. !file_lock->fl_next);
  1610. if (!error)
  1611. continue;
  1612. locks_delete_block(file_lock);
  1613. break;
  1614. }
  1615. /*
  1616. * Attempt to detect a close/fcntl race and recover by
  1617. * releasing the lock that was just acquired.
  1618. */
  1619. if (!error && fcheck(fd) != filp && flock.l_type != F_UNLCK) {
  1620. flock.l_type = F_UNLCK;
  1621. goto again;
  1622. }
  1623. out:
  1624. locks_free_lock(file_lock);
  1625. return error;
  1626. }
  1627. #if BITS_PER_LONG == 32
  1628. /* Report the first existing lock that would conflict with l.
  1629. * This implements the F_GETLK command of fcntl().
  1630. */
  1631. int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
  1632. {
  1633. struct file_lock file_lock;
  1634. struct flock64 flock;
  1635. int error;
  1636. error = -EFAULT;
  1637. if (copy_from_user(&flock, l, sizeof(flock)))
  1638. goto out;
  1639. error = -EINVAL;
  1640. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1641. goto out;
  1642. error = flock64_to_posix_lock(filp, &file_lock, &flock);
  1643. if (error)
  1644. goto out;
  1645. error = vfs_test_lock(filp, &file_lock);
  1646. if (error)
  1647. goto out;
  1648. flock.l_type = file_lock.fl_type;
  1649. if (file_lock.fl_type != F_UNLCK)
  1650. posix_lock_to_flock64(&flock, &file_lock);
  1651. error = -EFAULT;
  1652. if (!copy_to_user(l, &flock, sizeof(flock)))
  1653. error = 0;
  1654. out:
  1655. return error;
  1656. }
  1657. /* Apply the lock described by l to an open file descriptor.
  1658. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1659. */
  1660. int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
  1661. struct flock64 __user *l)
  1662. {
  1663. struct file_lock *file_lock = locks_alloc_lock();
  1664. struct flock64 flock;
  1665. struct inode *inode;
  1666. int error;
  1667. if (file_lock == NULL)
  1668. return -ENOLCK;
  1669. /*
  1670. * This might block, so we do it before checking the inode.
  1671. */
  1672. error = -EFAULT;
  1673. if (copy_from_user(&flock, l, sizeof(flock)))
  1674. goto out;
  1675. inode = filp->f_path.dentry->d_inode;
  1676. /* Don't allow mandatory locks on files that may be memory mapped
  1677. * and shared.
  1678. */
  1679. if (IS_MANDLOCK(inode) &&
  1680. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1681. mapping_writably_mapped(filp->f_mapping)) {
  1682. error = -EAGAIN;
  1683. goto out;
  1684. }
  1685. again:
  1686. error = flock64_to_posix_lock(filp, file_lock, &flock);
  1687. if (error)
  1688. goto out;
  1689. if (cmd == F_SETLKW64) {
  1690. file_lock->fl_flags |= FL_SLEEP;
  1691. }
  1692. error = -EBADF;
  1693. switch (flock.l_type) {
  1694. case F_RDLCK:
  1695. if (!(filp->f_mode & FMODE_READ))
  1696. goto out;
  1697. break;
  1698. case F_WRLCK:
  1699. if (!(filp->f_mode & FMODE_WRITE))
  1700. goto out;
  1701. break;
  1702. case F_UNLCK:
  1703. break;
  1704. default:
  1705. error = -EINVAL;
  1706. goto out;
  1707. }
  1708. error = security_file_lock(filp, file_lock->fl_type);
  1709. if (error)
  1710. goto out;
  1711. for (;;) {
  1712. error = vfs_lock_file(fi

Large files files are truncated, but you can click here to view the full file