PageRenderTime 64ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/locks.c

https://bitbucket.org/evzijst/gittest
C | 2212 lines | 1447 code | 243 blank | 522 comment | 426 complexity | 9aa96df5ea77e2a68bdb135bc4754810 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  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 <asm/semaphore.h>
  127. #include <asm/uaccess.h>
  128. #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
  129. #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
  130. #define IS_LEASE(fl) (fl->fl_flags & FL_LEASE)
  131. int leases_enable = 1;
  132. int lease_break_time = 45;
  133. #define for_each_lock(inode, lockp) \
  134. for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
  135. LIST_HEAD(file_lock_list);
  136. EXPORT_SYMBOL(file_lock_list);
  137. static LIST_HEAD(blocked_list);
  138. static kmem_cache_t *filelock_cache;
  139. /* Allocate an empty lock structure. */
  140. static struct file_lock *locks_alloc_lock(void)
  141. {
  142. return kmem_cache_alloc(filelock_cache, SLAB_KERNEL);
  143. }
  144. /* Free a lock which is not in use. */
  145. static inline void locks_free_lock(struct file_lock *fl)
  146. {
  147. if (fl == NULL) {
  148. BUG();
  149. return;
  150. }
  151. if (waitqueue_active(&fl->fl_wait))
  152. panic("Attempting to free lock with active wait queue");
  153. if (!list_empty(&fl->fl_block))
  154. panic("Attempting to free lock with active block list");
  155. if (!list_empty(&fl->fl_link))
  156. panic("Attempting to free lock on active lock list");
  157. if (fl->fl_ops) {
  158. if (fl->fl_ops->fl_release_private)
  159. fl->fl_ops->fl_release_private(fl);
  160. fl->fl_ops = NULL;
  161. }
  162. if (fl->fl_lmops) {
  163. if (fl->fl_lmops->fl_release_private)
  164. fl->fl_lmops->fl_release_private(fl);
  165. fl->fl_lmops = NULL;
  166. }
  167. kmem_cache_free(filelock_cache, fl);
  168. }
  169. void locks_init_lock(struct file_lock *fl)
  170. {
  171. INIT_LIST_HEAD(&fl->fl_link);
  172. INIT_LIST_HEAD(&fl->fl_block);
  173. init_waitqueue_head(&fl->fl_wait);
  174. fl->fl_next = NULL;
  175. fl->fl_fasync = NULL;
  176. fl->fl_owner = NULL;
  177. fl->fl_pid = 0;
  178. fl->fl_file = NULL;
  179. fl->fl_flags = 0;
  180. fl->fl_type = 0;
  181. fl->fl_start = fl->fl_end = 0;
  182. fl->fl_ops = NULL;
  183. fl->fl_lmops = NULL;
  184. }
  185. EXPORT_SYMBOL(locks_init_lock);
  186. /*
  187. * Initialises the fields of the file lock which are invariant for
  188. * free file_locks.
  189. */
  190. static void init_once(void *foo, kmem_cache_t *cache, unsigned long flags)
  191. {
  192. struct file_lock *lock = (struct file_lock *) foo;
  193. if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) !=
  194. SLAB_CTOR_CONSTRUCTOR)
  195. return;
  196. locks_init_lock(lock);
  197. }
  198. /*
  199. * Initialize a new lock from an existing file_lock structure.
  200. */
  201. void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  202. {
  203. new->fl_owner = fl->fl_owner;
  204. new->fl_pid = fl->fl_pid;
  205. new->fl_file = fl->fl_file;
  206. new->fl_flags = fl->fl_flags;
  207. new->fl_type = fl->fl_type;
  208. new->fl_start = fl->fl_start;
  209. new->fl_end = fl->fl_end;
  210. new->fl_ops = fl->fl_ops;
  211. new->fl_lmops = fl->fl_lmops;
  212. if (fl->fl_ops && fl->fl_ops->fl_copy_lock)
  213. fl->fl_ops->fl_copy_lock(new, fl);
  214. if (fl->fl_lmops && fl->fl_lmops->fl_copy_lock)
  215. fl->fl_lmops->fl_copy_lock(new, fl);
  216. }
  217. EXPORT_SYMBOL(locks_copy_lock);
  218. static inline int flock_translate_cmd(int cmd) {
  219. if (cmd & LOCK_MAND)
  220. return cmd & (LOCK_MAND | LOCK_RW);
  221. switch (cmd) {
  222. case LOCK_SH:
  223. return F_RDLCK;
  224. case LOCK_EX:
  225. return F_WRLCK;
  226. case LOCK_UN:
  227. return F_UNLCK;
  228. }
  229. return -EINVAL;
  230. }
  231. /* Fill in a file_lock structure with an appropriate FLOCK lock. */
  232. static int flock_make_lock(struct file *filp, struct file_lock **lock,
  233. unsigned int cmd)
  234. {
  235. struct file_lock *fl;
  236. int type = flock_translate_cmd(cmd);
  237. if (type < 0)
  238. return type;
  239. fl = locks_alloc_lock();
  240. if (fl == NULL)
  241. return -ENOMEM;
  242. fl->fl_file = filp;
  243. fl->fl_pid = current->tgid;
  244. fl->fl_flags = FL_FLOCK;
  245. fl->fl_type = type;
  246. fl->fl_end = OFFSET_MAX;
  247. *lock = fl;
  248. return 0;
  249. }
  250. static int assign_type(struct file_lock *fl, int type)
  251. {
  252. switch (type) {
  253. case F_RDLCK:
  254. case F_WRLCK:
  255. case F_UNLCK:
  256. fl->fl_type = type;
  257. break;
  258. default:
  259. return -EINVAL;
  260. }
  261. return 0;
  262. }
  263. /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  264. * style lock.
  265. */
  266. static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
  267. struct flock *l)
  268. {
  269. off_t start, end;
  270. switch (l->l_whence) {
  271. case 0: /*SEEK_SET*/
  272. start = 0;
  273. break;
  274. case 1: /*SEEK_CUR*/
  275. start = filp->f_pos;
  276. break;
  277. case 2: /*SEEK_END*/
  278. start = i_size_read(filp->f_dentry->d_inode);
  279. break;
  280. default:
  281. return -EINVAL;
  282. }
  283. /* POSIX-1996 leaves the case l->l_len < 0 undefined;
  284. POSIX-2001 defines it. */
  285. start += l->l_start;
  286. end = start + l->l_len - 1;
  287. if (l->l_len < 0) {
  288. end = start - 1;
  289. start += l->l_len;
  290. }
  291. if (start < 0)
  292. return -EINVAL;
  293. if (l->l_len > 0 && end < 0)
  294. return -EOVERFLOW;
  295. fl->fl_start = start; /* we record the absolute position */
  296. fl->fl_end = end;
  297. if (l->l_len == 0)
  298. fl->fl_end = OFFSET_MAX;
  299. fl->fl_owner = current->files;
  300. fl->fl_pid = current->tgid;
  301. fl->fl_file = filp;
  302. fl->fl_flags = FL_POSIX;
  303. fl->fl_ops = NULL;
  304. fl->fl_lmops = NULL;
  305. return assign_type(fl, l->l_type);
  306. }
  307. #if BITS_PER_LONG == 32
  308. static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
  309. struct flock64 *l)
  310. {
  311. loff_t start;
  312. switch (l->l_whence) {
  313. case 0: /*SEEK_SET*/
  314. start = 0;
  315. break;
  316. case 1: /*SEEK_CUR*/
  317. start = filp->f_pos;
  318. break;
  319. case 2: /*SEEK_END*/
  320. start = i_size_read(filp->f_dentry->d_inode);
  321. break;
  322. default:
  323. return -EINVAL;
  324. }
  325. if (((start += l->l_start) < 0) || (l->l_len < 0))
  326. return -EINVAL;
  327. fl->fl_end = start + l->l_len - 1;
  328. if (l->l_len > 0 && fl->fl_end < 0)
  329. return -EOVERFLOW;
  330. fl->fl_start = start; /* we record the absolute position */
  331. if (l->l_len == 0)
  332. fl->fl_end = OFFSET_MAX;
  333. fl->fl_owner = current->files;
  334. fl->fl_pid = current->tgid;
  335. fl->fl_file = filp;
  336. fl->fl_flags = FL_POSIX;
  337. fl->fl_ops = NULL;
  338. fl->fl_lmops = NULL;
  339. switch (l->l_type) {
  340. case F_RDLCK:
  341. case F_WRLCK:
  342. case F_UNLCK:
  343. fl->fl_type = l->l_type;
  344. break;
  345. default:
  346. return -EINVAL;
  347. }
  348. return (0);
  349. }
  350. #endif
  351. /* default lease lock manager operations */
  352. static void lease_break_callback(struct file_lock *fl)
  353. {
  354. kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
  355. }
  356. static void lease_release_private_callback(struct file_lock *fl)
  357. {
  358. if (!fl->fl_file)
  359. return;
  360. f_delown(fl->fl_file);
  361. fl->fl_file->f_owner.signum = 0;
  362. }
  363. int lease_mylease_callback(struct file_lock *fl, struct file_lock *try)
  364. {
  365. return fl->fl_file == try->fl_file;
  366. }
  367. struct lock_manager_operations lease_manager_ops = {
  368. .fl_break = lease_break_callback,
  369. .fl_release_private = lease_release_private_callback,
  370. .fl_mylease = lease_mylease_callback,
  371. .fl_change = lease_modify,
  372. };
  373. /*
  374. * Initialize a lease, use the default lock manager operations
  375. */
  376. static int lease_init(struct file *filp, int type, struct file_lock *fl)
  377. {
  378. fl->fl_owner = current->files;
  379. fl->fl_pid = current->tgid;
  380. fl->fl_file = filp;
  381. fl->fl_flags = FL_LEASE;
  382. if (assign_type(fl, type) != 0) {
  383. locks_free_lock(fl);
  384. return -EINVAL;
  385. }
  386. fl->fl_start = 0;
  387. fl->fl_end = OFFSET_MAX;
  388. fl->fl_ops = NULL;
  389. fl->fl_lmops = &lease_manager_ops;
  390. return 0;
  391. }
  392. /* Allocate a file_lock initialised to this type of lease */
  393. static int lease_alloc(struct file *filp, int type, struct file_lock **flp)
  394. {
  395. struct file_lock *fl = locks_alloc_lock();
  396. int error;
  397. if (fl == NULL)
  398. return -ENOMEM;
  399. error = lease_init(filp, type, fl);
  400. if (error)
  401. return error;
  402. *flp = fl;
  403. return 0;
  404. }
  405. /* Check if two locks overlap each other.
  406. */
  407. static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
  408. {
  409. return ((fl1->fl_end >= fl2->fl_start) &&
  410. (fl2->fl_end >= fl1->fl_start));
  411. }
  412. /*
  413. * Check whether two locks have the same owner.
  414. */
  415. static inline int
  416. posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  417. {
  418. if (fl1->fl_lmops && fl1->fl_lmops->fl_compare_owner)
  419. return fl2->fl_lmops == fl1->fl_lmops &&
  420. fl1->fl_lmops->fl_compare_owner(fl1, fl2);
  421. return fl1->fl_owner == fl2->fl_owner;
  422. }
  423. /* Remove waiter from blocker's block list.
  424. * When blocker ends up pointing to itself then the list is empty.
  425. */
  426. static inline void __locks_delete_block(struct file_lock *waiter)
  427. {
  428. list_del_init(&waiter->fl_block);
  429. list_del_init(&waiter->fl_link);
  430. waiter->fl_next = NULL;
  431. }
  432. /*
  433. */
  434. static void locks_delete_block(struct file_lock *waiter)
  435. {
  436. lock_kernel();
  437. __locks_delete_block(waiter);
  438. unlock_kernel();
  439. }
  440. /* Insert waiter into blocker's block list.
  441. * We use a circular list so that processes can be easily woken up in
  442. * the order they blocked. The documentation doesn't require this but
  443. * it seems like the reasonable thing to do.
  444. */
  445. static void locks_insert_block(struct file_lock *blocker,
  446. struct file_lock *waiter)
  447. {
  448. if (!list_empty(&waiter->fl_block)) {
  449. printk(KERN_ERR "locks_insert_block: removing duplicated lock "
  450. "(pid=%d %Ld-%Ld type=%d)\n", waiter->fl_pid,
  451. waiter->fl_start, waiter->fl_end, waiter->fl_type);
  452. __locks_delete_block(waiter);
  453. }
  454. list_add_tail(&waiter->fl_block, &blocker->fl_block);
  455. waiter->fl_next = blocker;
  456. if (IS_POSIX(blocker))
  457. list_add(&waiter->fl_link, &blocked_list);
  458. }
  459. /* Wake up processes blocked waiting for blocker.
  460. * If told to wait then schedule the processes until the block list
  461. * is empty, otherwise empty the block list ourselves.
  462. */
  463. static void locks_wake_up_blocks(struct file_lock *blocker)
  464. {
  465. while (!list_empty(&blocker->fl_block)) {
  466. struct file_lock *waiter = list_entry(blocker->fl_block.next,
  467. struct file_lock, fl_block);
  468. __locks_delete_block(waiter);
  469. if (waiter->fl_lmops && waiter->fl_lmops->fl_notify)
  470. waiter->fl_lmops->fl_notify(waiter);
  471. else
  472. wake_up(&waiter->fl_wait);
  473. }
  474. }
  475. /* Insert file lock fl into an inode's lock list at the position indicated
  476. * by pos. At the same time add the lock to the global file lock list.
  477. */
  478. static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
  479. {
  480. list_add(&fl->fl_link, &file_lock_list);
  481. /* insert into file's list */
  482. fl->fl_next = *pos;
  483. *pos = fl;
  484. if (fl->fl_ops && fl->fl_ops->fl_insert)
  485. fl->fl_ops->fl_insert(fl);
  486. }
  487. /*
  488. * Delete a lock and then free it.
  489. * Wake up processes that are blocked waiting for this lock,
  490. * notify the FS that the lock has been cleared and
  491. * finally free the lock.
  492. */
  493. static void locks_delete_lock(struct file_lock **thisfl_p)
  494. {
  495. struct file_lock *fl = *thisfl_p;
  496. *thisfl_p = fl->fl_next;
  497. fl->fl_next = NULL;
  498. list_del_init(&fl->fl_link);
  499. fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
  500. if (fl->fl_fasync != NULL) {
  501. printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
  502. fl->fl_fasync = NULL;
  503. }
  504. if (fl->fl_ops && fl->fl_ops->fl_remove)
  505. fl->fl_ops->fl_remove(fl);
  506. locks_wake_up_blocks(fl);
  507. locks_free_lock(fl);
  508. }
  509. /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
  510. * checks for shared/exclusive status of overlapping locks.
  511. */
  512. static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  513. {
  514. if (sys_fl->fl_type == F_WRLCK)
  515. return 1;
  516. if (caller_fl->fl_type == F_WRLCK)
  517. return 1;
  518. return 0;
  519. }
  520. /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
  521. * checking before calling the locks_conflict().
  522. */
  523. static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  524. {
  525. /* POSIX locks owned by the same process do not conflict with
  526. * each other.
  527. */
  528. if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
  529. return (0);
  530. /* Check whether they overlap */
  531. if (!locks_overlap(caller_fl, sys_fl))
  532. return 0;
  533. return (locks_conflict(caller_fl, sys_fl));
  534. }
  535. /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  536. * checking before calling the locks_conflict().
  537. */
  538. static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  539. {
  540. /* FLOCK locks referring to the same filp do not conflict with
  541. * each other.
  542. */
  543. if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
  544. return (0);
  545. if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
  546. return 0;
  547. return (locks_conflict(caller_fl, sys_fl));
  548. }
  549. static int interruptible_sleep_on_locked(wait_queue_head_t *fl_wait, int timeout)
  550. {
  551. int result = 0;
  552. DECLARE_WAITQUEUE(wait, current);
  553. __set_current_state(TASK_INTERRUPTIBLE);
  554. add_wait_queue(fl_wait, &wait);
  555. if (timeout == 0)
  556. schedule();
  557. else
  558. result = schedule_timeout(timeout);
  559. if (signal_pending(current))
  560. result = -ERESTARTSYS;
  561. remove_wait_queue(fl_wait, &wait);
  562. __set_current_state(TASK_RUNNING);
  563. return result;
  564. }
  565. static int locks_block_on_timeout(struct file_lock *blocker, struct file_lock *waiter, int time)
  566. {
  567. int result;
  568. locks_insert_block(blocker, waiter);
  569. result = interruptible_sleep_on_locked(&waiter->fl_wait, time);
  570. __locks_delete_block(waiter);
  571. return result;
  572. }
  573. struct file_lock *
  574. posix_test_lock(struct file *filp, struct file_lock *fl)
  575. {
  576. struct file_lock *cfl;
  577. lock_kernel();
  578. for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
  579. if (!IS_POSIX(cfl))
  580. continue;
  581. if (posix_locks_conflict(cfl, fl))
  582. break;
  583. }
  584. unlock_kernel();
  585. return (cfl);
  586. }
  587. EXPORT_SYMBOL(posix_test_lock);
  588. /* This function tests for deadlock condition before putting a process to
  589. * sleep. The detection scheme is no longer recursive. Recursive was neat,
  590. * but dangerous - we risked stack corruption if the lock data was bad, or
  591. * if the recursion was too deep for any other reason.
  592. *
  593. * We rely on the fact that a task can only be on one lock's wait queue
  594. * at a time. When we find blocked_task on a wait queue we can re-search
  595. * with blocked_task equal to that queue's owner, until either blocked_task
  596. * isn't found, or blocked_task is found on a queue owned by my_task.
  597. *
  598. * Note: the above assumption may not be true when handling lock requests
  599. * from a broken NFS client. But broken NFS clients have a lot more to
  600. * worry about than proper deadlock detection anyway... --okir
  601. */
  602. int posix_locks_deadlock(struct file_lock *caller_fl,
  603. struct file_lock *block_fl)
  604. {
  605. struct list_head *tmp;
  606. next_task:
  607. if (posix_same_owner(caller_fl, block_fl))
  608. return 1;
  609. list_for_each(tmp, &blocked_list) {
  610. struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
  611. if (posix_same_owner(fl, block_fl)) {
  612. fl = fl->fl_next;
  613. block_fl = fl;
  614. goto next_task;
  615. }
  616. }
  617. return 0;
  618. }
  619. EXPORT_SYMBOL(posix_locks_deadlock);
  620. /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
  621. * at the head of the list, but that's secret knowledge known only to
  622. * flock_lock_file and posix_lock_file.
  623. */
  624. static int flock_lock_file(struct file *filp, struct file_lock *new_fl)
  625. {
  626. struct file_lock **before;
  627. struct inode * inode = filp->f_dentry->d_inode;
  628. int error = 0;
  629. int found = 0;
  630. lock_kernel();
  631. for_each_lock(inode, before) {
  632. struct file_lock *fl = *before;
  633. if (IS_POSIX(fl))
  634. break;
  635. if (IS_LEASE(fl))
  636. continue;
  637. if (filp != fl->fl_file)
  638. continue;
  639. if (new_fl->fl_type == fl->fl_type)
  640. goto out;
  641. found = 1;
  642. locks_delete_lock(before);
  643. break;
  644. }
  645. unlock_kernel();
  646. if (new_fl->fl_type == F_UNLCK)
  647. return 0;
  648. /*
  649. * If a higher-priority process was blocked on the old file lock,
  650. * give it the opportunity to lock the file.
  651. */
  652. if (found)
  653. cond_resched();
  654. lock_kernel();
  655. for_each_lock(inode, before) {
  656. struct file_lock *fl = *before;
  657. if (IS_POSIX(fl))
  658. break;
  659. if (IS_LEASE(fl))
  660. continue;
  661. if (!flock_locks_conflict(new_fl, fl))
  662. continue;
  663. error = -EAGAIN;
  664. if (new_fl->fl_flags & FL_SLEEP) {
  665. locks_insert_block(fl, new_fl);
  666. }
  667. goto out;
  668. }
  669. locks_insert_lock(&inode->i_flock, new_fl);
  670. error = 0;
  671. out:
  672. unlock_kernel();
  673. return error;
  674. }
  675. EXPORT_SYMBOL(posix_lock_file);
  676. static int __posix_lock_file(struct inode *inode, struct file_lock *request)
  677. {
  678. struct file_lock *fl;
  679. struct file_lock *new_fl, *new_fl2;
  680. struct file_lock *left = NULL;
  681. struct file_lock *right = NULL;
  682. struct file_lock **before;
  683. int error, added = 0;
  684. /*
  685. * We may need two file_lock structures for this operation,
  686. * so we get them in advance to avoid races.
  687. */
  688. new_fl = locks_alloc_lock();
  689. new_fl2 = locks_alloc_lock();
  690. lock_kernel();
  691. if (request->fl_type != F_UNLCK) {
  692. for_each_lock(inode, before) {
  693. struct file_lock *fl = *before;
  694. if (!IS_POSIX(fl))
  695. continue;
  696. if (!posix_locks_conflict(request, fl))
  697. continue;
  698. error = -EAGAIN;
  699. if (!(request->fl_flags & FL_SLEEP))
  700. goto out;
  701. error = -EDEADLK;
  702. if (posix_locks_deadlock(request, fl))
  703. goto out;
  704. error = -EAGAIN;
  705. locks_insert_block(fl, request);
  706. goto out;
  707. }
  708. }
  709. /* If we're just looking for a conflict, we're done. */
  710. error = 0;
  711. if (request->fl_flags & FL_ACCESS)
  712. goto out;
  713. error = -ENOLCK; /* "no luck" */
  714. if (!(new_fl && new_fl2))
  715. goto out;
  716. /*
  717. * We've allocated the new locks in advance, so there are no
  718. * errors possible (and no blocking operations) from here on.
  719. *
  720. * Find the first old lock with the same owner as the new lock.
  721. */
  722. before = &inode->i_flock;
  723. /* First skip locks owned by other processes. */
  724. while ((fl = *before) && (!IS_POSIX(fl) ||
  725. !posix_same_owner(request, fl))) {
  726. before = &fl->fl_next;
  727. }
  728. /* Process locks with this owner. */
  729. while ((fl = *before) && posix_same_owner(request, fl)) {
  730. /* Detect adjacent or overlapping regions (if same lock type)
  731. */
  732. if (request->fl_type == fl->fl_type) {
  733. if (fl->fl_end < request->fl_start - 1)
  734. goto next_lock;
  735. /* If the next lock in the list has entirely bigger
  736. * addresses than the new one, insert the lock here.
  737. */
  738. if (fl->fl_start > request->fl_end + 1)
  739. break;
  740. /* If we come here, the new and old lock are of the
  741. * same type and adjacent or overlapping. Make one
  742. * lock yielding from the lower start address of both
  743. * locks to the higher end address.
  744. */
  745. if (fl->fl_start > request->fl_start)
  746. fl->fl_start = request->fl_start;
  747. else
  748. request->fl_start = fl->fl_start;
  749. if (fl->fl_end < request->fl_end)
  750. fl->fl_end = request->fl_end;
  751. else
  752. request->fl_end = fl->fl_end;
  753. if (added) {
  754. locks_delete_lock(before);
  755. continue;
  756. }
  757. request = fl;
  758. added = 1;
  759. }
  760. else {
  761. /* Processing for different lock types is a bit
  762. * more complex.
  763. */
  764. if (fl->fl_end < request->fl_start)
  765. goto next_lock;
  766. if (fl->fl_start > request->fl_end)
  767. break;
  768. if (request->fl_type == F_UNLCK)
  769. added = 1;
  770. if (fl->fl_start < request->fl_start)
  771. left = fl;
  772. /* If the next lock in the list has a higher end
  773. * address than the new one, insert the new one here.
  774. */
  775. if (fl->fl_end > request->fl_end) {
  776. right = fl;
  777. break;
  778. }
  779. if (fl->fl_start >= request->fl_start) {
  780. /* The new lock completely replaces an old
  781. * one (This may happen several times).
  782. */
  783. if (added) {
  784. locks_delete_lock(before);
  785. continue;
  786. }
  787. /* Replace the old lock with the new one.
  788. * Wake up anybody waiting for the old one,
  789. * as the change in lock type might satisfy
  790. * their needs.
  791. */
  792. locks_wake_up_blocks(fl);
  793. fl->fl_start = request->fl_start;
  794. fl->fl_end = request->fl_end;
  795. fl->fl_type = request->fl_type;
  796. fl->fl_u = request->fl_u;
  797. request = fl;
  798. added = 1;
  799. }
  800. }
  801. /* Go on to next lock.
  802. */
  803. next_lock:
  804. before = &fl->fl_next;
  805. }
  806. error = 0;
  807. if (!added) {
  808. if (request->fl_type == F_UNLCK)
  809. goto out;
  810. locks_copy_lock(new_fl, request);
  811. locks_insert_lock(before, new_fl);
  812. new_fl = NULL;
  813. }
  814. if (right) {
  815. if (left == right) {
  816. /* The new lock breaks the old one in two pieces,
  817. * so we have to use the second new lock.
  818. */
  819. left = new_fl2;
  820. new_fl2 = NULL;
  821. locks_copy_lock(left, right);
  822. locks_insert_lock(before, left);
  823. }
  824. right->fl_start = request->fl_end + 1;
  825. locks_wake_up_blocks(right);
  826. }
  827. if (left) {
  828. left->fl_end = request->fl_start - 1;
  829. locks_wake_up_blocks(left);
  830. }
  831. out:
  832. unlock_kernel();
  833. /*
  834. * Free any unused locks.
  835. */
  836. if (new_fl)
  837. locks_free_lock(new_fl);
  838. if (new_fl2)
  839. locks_free_lock(new_fl2);
  840. return error;
  841. }
  842. /**
  843. * posix_lock_file - Apply a POSIX-style lock to a file
  844. * @filp: The file to apply the lock to
  845. * @fl: The lock to be applied
  846. *
  847. * Add a POSIX style lock to a file.
  848. * We merge adjacent & overlapping locks whenever possible.
  849. * POSIX locks are sorted by owner task, then by starting address
  850. */
  851. int posix_lock_file(struct file *filp, struct file_lock *fl)
  852. {
  853. return __posix_lock_file(filp->f_dentry->d_inode, fl);
  854. }
  855. /**
  856. * posix_lock_file_wait - Apply a POSIX-style lock to a file
  857. * @filp: The file to apply the lock to
  858. * @fl: The lock to be applied
  859. *
  860. * Add a POSIX style lock to a file.
  861. * We merge adjacent & overlapping locks whenever possible.
  862. * POSIX locks are sorted by owner task, then by starting address
  863. */
  864. int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
  865. {
  866. int error;
  867. might_sleep ();
  868. for (;;) {
  869. error = __posix_lock_file(filp->f_dentry->d_inode, fl);
  870. if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
  871. break;
  872. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  873. if (!error)
  874. continue;
  875. locks_delete_block(fl);
  876. break;
  877. }
  878. return error;
  879. }
  880. EXPORT_SYMBOL(posix_lock_file_wait);
  881. /**
  882. * locks_mandatory_locked - Check for an active lock
  883. * @inode: the file to check
  884. *
  885. * Searches the inode's list of locks to find any POSIX locks which conflict.
  886. * This function is called from locks_verify_locked() only.
  887. */
  888. int locks_mandatory_locked(struct inode *inode)
  889. {
  890. fl_owner_t owner = current->files;
  891. struct file_lock *fl;
  892. /*
  893. * Search the lock list for this inode for any POSIX locks.
  894. */
  895. lock_kernel();
  896. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  897. if (!IS_POSIX(fl))
  898. continue;
  899. if (fl->fl_owner != owner)
  900. break;
  901. }
  902. unlock_kernel();
  903. return fl ? -EAGAIN : 0;
  904. }
  905. /**
  906. * locks_mandatory_area - Check for a conflicting lock
  907. * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
  908. * for shared
  909. * @inode: the file to check
  910. * @filp: how the file was opened (if it was)
  911. * @offset: start of area to check
  912. * @count: length of area to check
  913. *
  914. * Searches the inode's list of locks to find any POSIX locks which conflict.
  915. * This function is called from rw_verify_area() and
  916. * locks_verify_truncate().
  917. */
  918. int locks_mandatory_area(int read_write, struct inode *inode,
  919. struct file *filp, loff_t offset,
  920. size_t count)
  921. {
  922. struct file_lock fl;
  923. int error;
  924. locks_init_lock(&fl);
  925. fl.fl_owner = current->files;
  926. fl.fl_pid = current->tgid;
  927. fl.fl_file = filp;
  928. fl.fl_flags = FL_POSIX | FL_ACCESS;
  929. if (filp && !(filp->f_flags & O_NONBLOCK))
  930. fl.fl_flags |= FL_SLEEP;
  931. fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
  932. fl.fl_start = offset;
  933. fl.fl_end = offset + count - 1;
  934. for (;;) {
  935. error = __posix_lock_file(inode, &fl);
  936. if (error != -EAGAIN)
  937. break;
  938. if (!(fl.fl_flags & FL_SLEEP))
  939. break;
  940. error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
  941. if (!error) {
  942. /*
  943. * If we've been sleeping someone might have
  944. * changed the permissions behind our back.
  945. */
  946. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  947. continue;
  948. }
  949. locks_delete_block(&fl);
  950. break;
  951. }
  952. return error;
  953. }
  954. EXPORT_SYMBOL(locks_mandatory_area);
  955. /* We already had a lease on this file; just change its type */
  956. int lease_modify(struct file_lock **before, int arg)
  957. {
  958. struct file_lock *fl = *before;
  959. int error = assign_type(fl, arg);
  960. if (error)
  961. return error;
  962. locks_wake_up_blocks(fl);
  963. if (arg == F_UNLCK)
  964. locks_delete_lock(before);
  965. return 0;
  966. }
  967. EXPORT_SYMBOL(lease_modify);
  968. static void time_out_leases(struct inode *inode)
  969. {
  970. struct file_lock **before;
  971. struct file_lock *fl;
  972. before = &inode->i_flock;
  973. while ((fl = *before) && IS_LEASE(fl) && (fl->fl_type & F_INPROGRESS)) {
  974. if ((fl->fl_break_time == 0)
  975. || time_before(jiffies, fl->fl_break_time)) {
  976. before = &fl->fl_next;
  977. continue;
  978. }
  979. printk(KERN_INFO "lease broken - owner pid = %d\n", fl->fl_pid);
  980. lease_modify(before, fl->fl_type & ~F_INPROGRESS);
  981. if (fl == *before) /* lease_modify may have freed fl */
  982. before = &fl->fl_next;
  983. }
  984. }
  985. /**
  986. * __break_lease - revoke all outstanding leases on file
  987. * @inode: the inode of the file to return
  988. * @mode: the open mode (read or write)
  989. *
  990. * break_lease (inlined for speed) has checked there already
  991. * is a lease on this file. Leases are broken on a call to open()
  992. * or truncate(). This function can sleep unless you
  993. * specified %O_NONBLOCK to your open().
  994. */
  995. int __break_lease(struct inode *inode, unsigned int mode)
  996. {
  997. int error = 0, future;
  998. struct file_lock *new_fl, *flock;
  999. struct file_lock *fl;
  1000. int alloc_err;
  1001. unsigned long break_time;
  1002. int i_have_this_lease = 0;
  1003. alloc_err = lease_alloc(NULL, mode & FMODE_WRITE ? F_WRLCK : F_RDLCK,
  1004. &new_fl);
  1005. lock_kernel();
  1006. time_out_leases(inode);
  1007. flock = inode->i_flock;
  1008. if ((flock == NULL) || !IS_LEASE(flock))
  1009. goto out;
  1010. for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next)
  1011. if (fl->fl_owner == current->files)
  1012. i_have_this_lease = 1;
  1013. if (mode & FMODE_WRITE) {
  1014. /* If we want write access, we have to revoke any lease. */
  1015. future = F_UNLCK | F_INPROGRESS;
  1016. } else if (flock->fl_type & F_INPROGRESS) {
  1017. /* If the lease is already being broken, we just leave it */
  1018. future = flock->fl_type;
  1019. } else if (flock->fl_type & F_WRLCK) {
  1020. /* Downgrade the exclusive lease to a read-only lease. */
  1021. future = F_RDLCK | F_INPROGRESS;
  1022. } else {
  1023. /* the existing lease was read-only, so we can read too. */
  1024. goto out;
  1025. }
  1026. if (alloc_err && !i_have_this_lease && ((mode & O_NONBLOCK) == 0)) {
  1027. error = alloc_err;
  1028. goto out;
  1029. }
  1030. break_time = 0;
  1031. if (lease_break_time > 0) {
  1032. break_time = jiffies + lease_break_time * HZ;
  1033. if (break_time == 0)
  1034. break_time++; /* so that 0 means no break time */
  1035. }
  1036. for (fl = flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
  1037. if (fl->fl_type != future) {
  1038. fl->fl_type = future;
  1039. fl->fl_break_time = break_time;
  1040. /* lease must have lmops break callback */
  1041. fl->fl_lmops->fl_break(fl);
  1042. }
  1043. }
  1044. if (i_have_this_lease || (mode & O_NONBLOCK)) {
  1045. error = -EWOULDBLOCK;
  1046. goto out;
  1047. }
  1048. restart:
  1049. break_time = flock->fl_break_time;
  1050. if (break_time != 0) {
  1051. break_time -= jiffies;
  1052. if (break_time == 0)
  1053. break_time++;
  1054. }
  1055. error = locks_block_on_timeout(flock, new_fl, break_time);
  1056. if (error >= 0) {
  1057. if (error == 0)
  1058. time_out_leases(inode);
  1059. /* Wait for the next lease that has not been broken yet */
  1060. for (flock = inode->i_flock; flock && IS_LEASE(flock);
  1061. flock = flock->fl_next) {
  1062. if (flock->fl_type & F_INPROGRESS)
  1063. goto restart;
  1064. }
  1065. error = 0;
  1066. }
  1067. out:
  1068. unlock_kernel();
  1069. if (!alloc_err)
  1070. locks_free_lock(new_fl);
  1071. return error;
  1072. }
  1073. EXPORT_SYMBOL(__break_lease);
  1074. /**
  1075. * lease_get_mtime
  1076. * @inode: the inode
  1077. * @time: pointer to a timespec which will contain the last modified time
  1078. *
  1079. * This is to force NFS clients to flush their caches for files with
  1080. * exclusive leases. The justification is that if someone has an
  1081. * exclusive lease, then they could be modifiying it.
  1082. */
  1083. void lease_get_mtime(struct inode *inode, struct timespec *time)
  1084. {
  1085. struct file_lock *flock = inode->i_flock;
  1086. if (flock && IS_LEASE(flock) && (flock->fl_type & F_WRLCK))
  1087. *time = current_fs_time(inode->i_sb);
  1088. else
  1089. *time = inode->i_mtime;
  1090. }
  1091. EXPORT_SYMBOL(lease_get_mtime);
  1092. /**
  1093. * fcntl_getlease - Enquire what lease is currently active
  1094. * @filp: the file
  1095. *
  1096. * The value returned by this function will be one of
  1097. * (if no lease break is pending):
  1098. *
  1099. * %F_RDLCK to indicate a shared lease is held.
  1100. *
  1101. * %F_WRLCK to indicate an exclusive lease is held.
  1102. *
  1103. * %F_UNLCK to indicate no lease is held.
  1104. *
  1105. * (if a lease break is pending):
  1106. *
  1107. * %F_RDLCK to indicate an exclusive lease needs to be
  1108. * changed to a shared lease (or removed).
  1109. *
  1110. * %F_UNLCK to indicate the lease needs to be removed.
  1111. *
  1112. * XXX: sfr & willy disagree over whether F_INPROGRESS
  1113. * should be returned to userspace.
  1114. */
  1115. int fcntl_getlease(struct file *filp)
  1116. {
  1117. struct file_lock *fl;
  1118. int type = F_UNLCK;
  1119. lock_kernel();
  1120. time_out_leases(filp->f_dentry->d_inode);
  1121. for (fl = filp->f_dentry->d_inode->i_flock; fl && IS_LEASE(fl);
  1122. fl = fl->fl_next) {
  1123. if (fl->fl_file == filp) {
  1124. type = fl->fl_type & ~F_INPROGRESS;
  1125. break;
  1126. }
  1127. }
  1128. unlock_kernel();
  1129. return type;
  1130. }
  1131. /**
  1132. * __setlease - sets a lease on an open file
  1133. * @filp: file pointer
  1134. * @arg: type of lease to obtain
  1135. * @flp: input - file_lock to use, output - file_lock inserted
  1136. *
  1137. * The (input) flp->fl_lmops->fl_break function is required
  1138. * by break_lease().
  1139. *
  1140. * Called with kernel lock held.
  1141. */
  1142. int __setlease(struct file *filp, long arg, struct file_lock **flp)
  1143. {
  1144. struct file_lock *fl, **before, **my_before = NULL, *lease = *flp;
  1145. struct dentry *dentry = filp->f_dentry;
  1146. struct inode *inode = dentry->d_inode;
  1147. int error, rdlease_count = 0, wrlease_count = 0;
  1148. time_out_leases(inode);
  1149. error = -EINVAL;
  1150. if (!flp || !(*flp) || !(*flp)->fl_lmops || !(*flp)->fl_lmops->fl_break)
  1151. goto out;
  1152. error = -EAGAIN;
  1153. if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
  1154. goto out;
  1155. if ((arg == F_WRLCK)
  1156. && ((atomic_read(&dentry->d_count) > 1)
  1157. || (atomic_read(&inode->i_count) > 1)))
  1158. goto out;
  1159. /*
  1160. * At this point, we know that if there is an exclusive
  1161. * lease on this file, then we hold it on this filp
  1162. * (otherwise our open of this file would have blocked).
  1163. * And if we are trying to acquire an exclusive lease,
  1164. * then the file is not open by anyone (including us)
  1165. * except for this filp.
  1166. */
  1167. for (before = &inode->i_flock;
  1168. ((fl = *before) != NULL) && IS_LEASE(fl);
  1169. before = &fl->fl_next) {
  1170. if (lease->fl_lmops->fl_mylease(fl, lease))
  1171. my_before = before;
  1172. else if (fl->fl_type == (F_INPROGRESS | F_UNLCK))
  1173. /*
  1174. * Someone is in the process of opening this
  1175. * file for writing so we may not take an
  1176. * exclusive lease on it.
  1177. */
  1178. wrlease_count++;
  1179. else
  1180. rdlease_count++;
  1181. }
  1182. if ((arg == F_RDLCK && (wrlease_count > 0)) ||
  1183. (arg == F_WRLCK && ((rdlease_count + wrlease_count) > 0)))
  1184. goto out;
  1185. if (my_before != NULL) {
  1186. error = lease->fl_lmops->fl_change(my_before, arg);
  1187. goto out;
  1188. }
  1189. error = 0;
  1190. if (arg == F_UNLCK)
  1191. goto out;
  1192. error = -EINVAL;
  1193. if (!leases_enable)
  1194. goto out;
  1195. error = lease_alloc(filp, arg, &fl);
  1196. if (error)
  1197. goto out;
  1198. locks_copy_lock(fl, lease);
  1199. locks_insert_lock(before, fl);
  1200. *flp = fl;
  1201. out:
  1202. return error;
  1203. }
  1204. /**
  1205. * setlease - sets a lease on an open file
  1206. * @filp: file pointer
  1207. * @arg: type of lease to obtain
  1208. * @lease: file_lock to use
  1209. *
  1210. * Call this to establish a lease on the file.
  1211. * The fl_lmops fl_break function is required by break_lease
  1212. */
  1213. int setlease(struct file *filp, long arg, struct file_lock **lease)
  1214. {
  1215. struct dentry *dentry = filp->f_dentry;
  1216. struct inode *inode = dentry->d_inode;
  1217. int error;
  1218. if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
  1219. return -EACCES;
  1220. if (!S_ISREG(inode->i_mode))
  1221. return -EINVAL;
  1222. error = security_file_lock(filp, arg);
  1223. if (error)
  1224. return error;
  1225. lock_kernel();
  1226. error = __setlease(filp, arg, lease);
  1227. unlock_kernel();
  1228. return error;
  1229. }
  1230. EXPORT_SYMBOL(setlease);
  1231. /**
  1232. * fcntl_setlease - sets a lease on an open file
  1233. * @fd: open file descriptor
  1234. * @filp: file pointer
  1235. * @arg: type of lease to obtain
  1236. *
  1237. * Call this fcntl to establish a lease on the file.
  1238. * Note that you also need to call %F_SETSIG to
  1239. * receive a signal when the lease is broken.
  1240. */
  1241. int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
  1242. {
  1243. struct file_lock fl, *flp = &fl;
  1244. struct dentry *dentry = filp->f_dentry;
  1245. struct inode *inode = dentry->d_inode;
  1246. int error;
  1247. if ((current->fsuid != inode->i_uid) && !capable(CAP_LEASE))
  1248. return -EACCES;
  1249. if (!S_ISREG(inode->i_mode))
  1250. return -EINVAL;
  1251. error = security_file_lock(filp, arg);
  1252. if (error)
  1253. return error;
  1254. locks_init_lock(&fl);
  1255. error = lease_init(filp, arg, &fl);
  1256. if (error)
  1257. return error;
  1258. lock_kernel();
  1259. error = __setlease(filp, arg, &flp);
  1260. if (error)
  1261. goto out_unlock;
  1262. error = fasync_helper(fd, filp, 1, &flp->fl_fasync);
  1263. if (error < 0) {
  1264. /* remove lease just inserted by __setlease */
  1265. flp->fl_type = F_UNLCK | F_INPROGRESS;
  1266. flp->fl_break_time = jiffies- 10;
  1267. time_out_leases(inode);
  1268. goto out_unlock;
  1269. }
  1270. error = f_setown(filp, current->pid, 0);
  1271. out_unlock:
  1272. unlock_kernel();
  1273. return error;
  1274. }
  1275. /**
  1276. * flock_lock_file_wait - Apply a FLOCK-style lock to a file
  1277. * @filp: The file to apply the lock to
  1278. * @fl: The lock to be applied
  1279. *
  1280. * Add a FLOCK style lock to a file.
  1281. */
  1282. int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
  1283. {
  1284. int error;
  1285. might_sleep();
  1286. for (;;) {
  1287. error = flock_lock_file(filp, fl);
  1288. if ((error != -EAGAIN) || !(fl->fl_flags & FL_SLEEP))
  1289. break;
  1290. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  1291. if (!error)
  1292. continue;
  1293. locks_delete_block(fl);
  1294. break;
  1295. }
  1296. return error;
  1297. }
  1298. EXPORT_SYMBOL(flock_lock_file_wait);
  1299. /**
  1300. * sys_flock: - flock() system call.
  1301. * @fd: the file descriptor to lock.
  1302. * @cmd: the type of lock to apply.
  1303. *
  1304. * Apply a %FL_FLOCK style lock to an open file descriptor.
  1305. * The @cmd can be one of
  1306. *
  1307. * %LOCK_SH -- a shared lock.
  1308. *
  1309. * %LOCK_EX -- an exclusive lock.
  1310. *
  1311. * %LOCK_UN -- remove an existing lock.
  1312. *
  1313. * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
  1314. *
  1315. * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
  1316. * processes read and write access respectively.
  1317. */
  1318. asmlinkage long sys_flock(unsigned int fd, unsigned int cmd)
  1319. {
  1320. struct file *filp;
  1321. struct file_lock *lock;
  1322. int can_sleep, unlock;
  1323. int error;
  1324. error = -EBADF;
  1325. filp = fget(fd);
  1326. if (!filp)
  1327. goto out;
  1328. can_sleep = !(cmd & LOCK_NB);
  1329. cmd &= ~LOCK_NB;
  1330. unlock = (cmd == LOCK_UN);
  1331. if (!unlock && !(cmd & LOCK_MAND) && !(filp->f_mode & 3))
  1332. goto out_putf;
  1333. error = flock_make_lock(filp, &lock, cmd);
  1334. if (error)
  1335. goto out_putf;
  1336. if (can_sleep)
  1337. lock->fl_flags |= FL_SLEEP;
  1338. error = security_file_lock(filp, cmd);
  1339. if (error)
  1340. goto out_free;
  1341. if (filp->f_op && filp->f_op->flock)
  1342. error = filp->f_op->flock(filp,
  1343. (can_sleep) ? F_SETLKW : F_SETLK,
  1344. lock);
  1345. else
  1346. error = flock_lock_file_wait(filp, lock);
  1347. out_free:
  1348. if (list_empty(&lock->fl_link)) {
  1349. locks_free_lock(lock);
  1350. }
  1351. out_putf:
  1352. fput(filp);
  1353. out:
  1354. return error;
  1355. }
  1356. /* Report the first existing lock that would conflict with l.
  1357. * This implements the F_GETLK command of fcntl().
  1358. */
  1359. int fcntl_getlk(struct file *filp, struct flock __user *l)
  1360. {
  1361. struct file_lock *fl, file_lock;
  1362. struct flock flock;
  1363. int error;
  1364. error = -EFAULT;
  1365. if (copy_from_user(&flock, l, sizeof(flock)))
  1366. goto out;
  1367. error = -EINVAL;
  1368. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1369. goto out;
  1370. error = flock_to_posix_lock(filp, &file_lock, &flock);
  1371. if (error)
  1372. goto out;
  1373. if (filp->f_op && filp->f_op->lock) {
  1374. error = filp->f_op->lock(filp, F_GETLK, &file_lock);
  1375. if (error < 0)
  1376. goto out;
  1377. else
  1378. fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
  1379. } else {
  1380. fl = posix_test_lock(filp, &file_lock);
  1381. }
  1382. flock.l_type = F_UNLCK;
  1383. if (fl != NULL) {
  1384. flock.l_pid = fl->fl_pid;
  1385. #if BITS_PER_LONG == 32
  1386. /*
  1387. * Make sure we can represent the posix lock via
  1388. * legacy 32bit flock.
  1389. */
  1390. error = -EOVERFLOW;
  1391. if (fl->fl_start > OFFT_OFFSET_MAX)
  1392. goto out;
  1393. if ((fl->fl_end != OFFSET_MAX)
  1394. && (fl->fl_end > OFFT_OFFSET_MAX))
  1395. goto out;
  1396. #endif
  1397. flock.l_start = fl->fl_start;
  1398. flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1399. fl->fl_end - fl->fl_start + 1;
  1400. flock.l_whence = 0;
  1401. flock.l_type = fl->fl_type;
  1402. }
  1403. error = -EFAULT;
  1404. if (!copy_to_user(l, &flock, sizeof(flock)))
  1405. error = 0;
  1406. out:
  1407. return error;
  1408. }
  1409. /* Apply the lock described by l to an open file descriptor.
  1410. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1411. */
  1412. int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
  1413. {
  1414. struct file_lock *file_lock = locks_alloc_lock();
  1415. struct flock flock;
  1416. struct inode *inode;
  1417. int error;
  1418. if (file_lock == NULL)
  1419. return -ENOLCK;
  1420. /*
  1421. * This might block, so we do it before checking the inode.
  1422. */
  1423. error = -EFAULT;
  1424. if (copy_from_user(&flock, l, sizeof(flock)))
  1425. goto out;
  1426. inode = filp->f_dentry->d_inode;
  1427. /* Don't allow mandatory locks on files that may be memory mapped
  1428. * and shared.
  1429. */
  1430. if (IS_MANDLOCK(inode) &&
  1431. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1432. mapping_writably_mapped(filp->f_mapping)) {
  1433. error = -EAGAIN;
  1434. goto out;
  1435. }
  1436. error = flock_to_posix_lock(filp, file_lock, &flock);
  1437. if (error)
  1438. goto out;
  1439. if (cmd == F_SETLKW) {
  1440. file_lock->fl_flags |= FL_SLEEP;
  1441. }
  1442. error = -EBADF;
  1443. switch (flock.l_type) {
  1444. case F_RDLCK:
  1445. if (!(filp->f_mode & FMODE_READ))
  1446. goto out;
  1447. break;
  1448. case F_WRLCK:
  1449. if (!(filp->f_mode & FMODE_WRITE))
  1450. goto out;
  1451. break;
  1452. case F_UNLCK:
  1453. break;
  1454. default:
  1455. error = -EINVAL;
  1456. goto out;
  1457. }
  1458. error = security_file_lock(filp, file_lock->fl_type);
  1459. if (error)
  1460. goto out;
  1461. if (filp->f_op && filp->f_op->lock != NULL) {
  1462. error = filp->f_op->lock(filp, cmd, file_lock);
  1463. goto out;
  1464. }
  1465. for (;;) {
  1466. error = __posix_lock_file(inode, file_lock);
  1467. if ((error != -EAGAIN) || (cmd == F_SETLK))
  1468. break;
  1469. error = wait_event_interruptible(file_lock->fl_wait,
  1470. !file_lock->fl_next);
  1471. if (!error)
  1472. continue;
  1473. locks_delete_block(file_lock);
  1474. break;
  1475. }
  1476. out:
  1477. locks_free_lock(file_lock);
  1478. return error;
  1479. }
  1480. #if BITS_PER_LONG == 32
  1481. /* Report the first existing lock that would conflict with l.
  1482. * This implements the F_GETLK command of fcntl().
  1483. */
  1484. int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
  1485. {
  1486. struct file_lock *fl, file_lock;
  1487. struct flock64 flock;
  1488. int error;
  1489. error = -EFAULT;
  1490. if (copy_from_user(&flock, l, sizeof(flock)))
  1491. goto out;
  1492. error = -EINVAL;
  1493. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1494. goto out;
  1495. error = flock64_to_posix_lock(filp, &file_lock, &flock);
  1496. if (error)
  1497. goto out;
  1498. if (filp->f_op && filp->f_op->lock) {
  1499. error = filp->f_op->lock(filp, F_GETLK, &file_lock);
  1500. if (error < 0)
  1501. goto out;
  1502. else
  1503. fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
  1504. } else {
  1505. fl = posix_test_lock(filp, &file_lock);
  1506. }
  1507. flock.l_type = F_UNLCK;
  1508. if (fl != NULL) {
  1509. flock.l_pid = fl->fl_pid;
  1510. flock.l_start = fl->fl_start;
  1511. flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1512. fl->fl_end - fl->fl_start + 1;
  1513. flock.l_whence = 0;
  1514. flock.l_type = fl->fl_type;
  1515. }
  1516. error = -EFAULT;
  1517. if (!copy_to_user(l, &flock, sizeof(flock)))
  1518. error = 0;
  1519. out:
  1520. return error;
  1521. }
  1522. /* Apply the lock described by l to an open file descriptor.
  1523. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1524. */
  1525. int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
  1526. {
  1527. struct file_lock *file_lock = locks_alloc_lock();
  1528. struct flock64 flock;
  1529. struct inode *inode;
  1530. int error;
  1531. if (file_lock == NULL)
  1532. return -ENOLCK;
  1533. /*
  1534. * This might block, so we do it before checking the inode.
  1535. */
  1536. error = -EFAULT;
  1537. if (copy_from_user(&flock, l, sizeof(flock)))
  1538. goto out;
  1539. inode = filp->f_dentry->d_inode;
  1540. /* Don't allow mandatory locks on files that may be memory mapped
  1541. * and shared.
  1542. */
  1543. if (IS_MANDLOCK(inode) &&
  1544. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1545. mapping_writably_mapped(filp->f_mapping)) {
  1546. error = -EAGAIN;
  1547. goto out;
  1548. }
  1549. error = flock64_to_posix_lock(filp, file_lock, &flock);
  1550. if (error)
  1551. goto out;
  1552. if (cmd == F_SETLKW64) {
  1553. file_lock->fl_flags |= FL_SLEEP;
  1554. }
  1555. error = -EBADF;
  1556. switch (flock.l_type) {
  1557. case F_RDLCK:
  1558. if (!(filp->f_mode & FMODE_READ))
  1559. goto out;
  1560. break;
  1561. case F_WRLCK:
  1562. if (!(filp->f_mode & FMODE_WRITE))
  1563. goto out;
  1564. break;
  1565. case F_UNLCK:
  1566. break;
  1567. default:
  1568. error = -EINVAL;
  1569. goto out;
  1570. }
  1571. error = security_file_lock(filp, file_lock->fl_type);
  1572. if (error)
  1573. goto out;
  1574. if (filp->f_op && filp->f_op->lock != NULL) {
  1575. error = filp->f_op->lock(filp, cmd, file_lock);
  1576. goto out;
  1577. }
  1578. for (;;) {
  1579. error = __posix_lock_file(inode, file_lock);
  1580. if ((error != -EAGAIN) || (cmd == F_SETLK64))
  1581. break;
  1582. error = wait_event_interruptible(file_lock->fl_wait,
  1583. !file_lock->fl_next);
  1584. if (!error)
  1585. continue;
  1586. locks_delete_block(file_lock);
  1587. break;
  1588. }
  1589. out:
  1590. locks_free_lock(file_lock);
  1591. return error;
  1592. }
  1593. #endif /* BITS_PER_LONG == 32 */
  1594. /*
  1595. * This function is called when the file is being removed
  1596. * from the task's fd array. POSIX locks belonging to this task
  1597. * are deleted at this time.
  1598. */
  1599. void locks_remove_posix(struct file *filp, fl_owner_t owner)
  1600. {
  1601. struct file_lock lock, **before;
  1602. /*
  1603. * If there are no locks held on this file, we don't need to call
  1604. * posix_lock_file(). Another process could be setting a lock on this
  1605. * file at the same time, but we wouldn't remove that lock anyway.
  1606. */
  1607. before = &filp->f_dentry->d_inode->i_flock;
  1608. if (*before == NULL)
  1609. return;
  1610. lock.fl_type = F_UNLCK;
  1611. lock.fl_flags = FL_POSIX;
  1612. lock.fl_start = 0;
  1613. lock.fl_end = OFFSET_MAX;
  1614. lock.fl_owner = owner;
  1615. lock.fl_pid = current->tgid;
  1616. lock.fl_file = filp;
  1617. lock.fl_ops = NULL;
  1618. lock.fl_lmops = NULL;
  1619. if (filp->f_op && filp->f_op->lock != NULL) {
  1620. filp->f_op->lock(filp, F_SETLK, &lock);
  1621. goto out;
  1622. }
  1623. /* Can't use posix_lock_file here; we need to remove it no matter
  1624. * which pid we have.
  1625. */
  1626. lock_kernel();
  1627. while (*before != NULL) {
  1628. struct file_lock *fl = *before;
  1629. if (IS_POSIX(fl) && posix_same_owner(fl, &lock)) {
  1630. locks_delete_lock(before);
  1631. continue;
  1632. }
  1633. before = &fl->fl_next;
  1634. }
  1635. unlock_kernel();
  1636. out:
  1637. if (lock.fl_ops && lock.fl_ops->fl_release_private)
  1638. lock.fl_ops->fl_release_private(&lock);
  1639. }
  1640. EXPORT_SYMBOL(locks_remove_posix);
  1641. /*
  1642. * This function is called on the last close of an open file.
  1643. */
  1644. void locks_remove_flock(struct file *filp)
  1645. {
  1646. struct inode * inode = filp->f_dentry->d_inode;
  1647. struct file_lock *fl;
  1648. struct file_lock **before;
  1649. if (!inode->i_flock)
  1650. return;
  1651. if (filp->f_op && filp->f_op->flock) {
  1652. struct file_lock fl = {
  1653. .fl_pid = current->tgid,
  1654. .fl_file = filp,
  1655. .fl_flags = FL_FLOCK,
  1656. .fl_type = F_UNLCK,
  1657. .fl_end = OFFSET_MAX,
  1658. };
  1659. filp->f_op->flock(filp, F_SETLKW, &fl);
  1660. }
  1661. lock_kernel();
  1662. before = &inode->i_flock;
  1663. while ((fl = *before) != NULL) {
  1664. if (fl->fl_file == filp) {
  1665. /*
  1666. * We might have a POSIX lock that was created at the same time
  1667. * the filp was closed for the last time. Just remove that too,
  1668. * regardless of ownership, since nobody can own it.
  1669. */
  1670. if (IS_FLOCK(fl) || IS_POSIX(fl)) {
  1671. locks_delete_lock(before);
  1672. continue;
  1673. }
  1674. if (IS_LEASE(fl)) {
  1675. lease_modify(before, F_UNLCK);
  1676. continue;
  1677. }
  1678. /* What? */
  1679. BUG();
  1680. }
  1681. before = &fl->fl_next;
  1682. }
  1683. unlock_kernel();
  1684. }
  1685. /**
  1686. * posix_block_lock - blocks waiting for a file lock
  1687. * @blocker: the lock which is blocking
  1688. * @waiter: the lock which conflicts and has to wait
  1689. *
  1690. * lockd needs to block waiting for locks.
  1691. */
  1692. void
  1693. posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
  1694. {
  1695. locks_insert_block(blocker, waiter);
  1696. }
  1697. EXPORT_SYMBOL(posix_block_lock);
  1698. /**
  1699. * posix_unblock_lock - stop waiting for a file lock
  1700. * @filp: how the file was opened
  1701. * @waiter: the lock which was waiting
  1702. *
  1703. * lockd needs to block waiting for locks.
  1704. */
  1705. void
  1706. posix_unblock_lock(struct file *filp, struct file_lock *waiter)
  1707. {
  1708. /*
  1709. * A remote machine may cancel the lock request after it's been
  1710. * granted locally. If that happens, we need to delete the lock.
  1711. */
  1712. lock_kernel();
  1713. if (waiter->fl_next) {
  1714. __locks_delete_block(waiter);
  1715. unlock_kernel();
  1716. } else {
  1717. unlock_kernel();
  1718. waiter->fl_type = F_UNLCK;
  1719. posix_lock_file(filp, waiter);
  1720. }
  1721. }
  1722. EXPORT_SYMBOL(posix_unblock_lock);
  1723. static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
  1724. {
  1725. struct inode *inode = NULL;
  1726. if (fl->fl_file != NULL)
  1727. inode = fl->fl_file->f_dentry->d_inode;
  1728. out += sprintf(out, "%d:%s ", id, pfx);
  1729. if (IS_POSIX(fl)) {
  1730. out += sprintf(out, "%6s %s ",
  1731. (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
  1732. (inode == NULL) ? "*NOINODE*" :
  1733. (IS_MANDLOCK(inode) &&
  1734. (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
  1735. "MANDATORY" : "ADVISORY ");
  1736. } else if (IS_FLOCK(fl)) {
  1737. if (fl->fl_type & LOCK_MAND) {
  1738. out += sprintf(out, "FLOCK MSNFS ");
  1739. } else {
  1740. out += sprintf(out, "FLOCK ADVISORY ");
  1741. }
  1742. } else if (IS_LEASE(fl)) {
  1743. out += sprintf(out, "LEASE ");
  1744. if (fl->fl_type & F_INPROGRESS)
  1745. out += sprintf(out, "BREAKING ");
  1746. else if (fl->fl_file)
  1747. out += sprintf(out, "ACTIVE ");
  1748. else
  1749. out += sprintf(out, "BREAKER ");
  1750. } else {
  1751. out += sprintf(out, "UNKNOWN UNKNOWN ");
  1752. }
  1753. if (fl->fl_type & LOCK_MAND) {
  1754. out += sprintf(out, "%s ",
  1755. (fl->fl_type & LOCK_READ)
  1756. ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
  1757. : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
  1758. } else {
  1759. out += sprintf(out, "%s ",
  1760. (fl->fl_type & F_INPROGRESS)
  1761. ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
  1762. : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
  1763. }
  1764. if (inode) {
  1765. #ifdef WE_CAN_BREAK_LSLK_NOW
  1766. out += sprintf(out, "%d %s:%ld ", fl->fl_pid,
  1767. inode->i_sb->s_id, inode->i_ino);
  1768. #else
  1769. /* userspace relies on this representation of dev_t ;-( */
  1770. out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid,
  1771. MAJOR(inode->i_sb->s_dev),
  1772. MINOR(inode->i_sb->s_dev), inode->i_ino);
  1773. #endif
  1774. } else {
  1775. out += sprintf(out, "%d <none>:0 ", fl->fl_pid);
  1776. }
  1777. if (IS_POSIX(fl)) {
  1778. if (fl->fl_end == OFFSET_MAX)
  1779. out += sprintf(out, "%Ld EOF\n", fl->fl_start);
  1780. else
  1781. out += sprintf(out, "%Ld %Ld\n", fl->fl_start,
  1782. fl->fl_end);
  1783. } else {
  1784. out += sprintf(out, "0 EOF\n");
  1785. }
  1786. }
  1787. static void move_lock_status(char **p, off_t* pos, off_t offset)
  1788. {
  1789. int len;
  1790. len = strlen(*p);
  1791. if(*pos >= offset) {
  1792. /* the complete line is valid */
  1793. *p += len;
  1794. *pos += len;
  1795. return;
  1796. }
  1797. if(*pos+len > offset) {
  1798. /* use the second part of the line */
  1799. int i = offset-*pos;
  1800. memmove(*p,*p+i,len-i);
  1801. *p += len-i;
  1802. *pos += len;
  1803. return;
  1804. }
  1805. /* discard the complete line */
  1806. *pos += len;
  1807. }
  1808. /**
  1809. * get_locks_status - reports lock usage in /proc/locks
  1810. * @buffer: address in userspace to write into
  1811. * @start: ?
  1812. * @offset: how far we are through the buffer
  1813. * @length: how much to read
  1814. */
  1815. int get_locks_status(char *buffer, char **start, off_t offset, int length)
  1816. {
  1817. struct list_head *tmp;
  1818. char *q = buffer;
  1819. off_t pos = 0;
  1820. int i = 0;
  1821. lock_kernel();
  1822. list_for_each(tmp, &file_lock_list) {
  1823. struct list_head *btmp;
  1824. struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
  1825. lock_get_status(q, fl, ++i, "");
  1826. move_lock_status(&q, &pos, offset);
  1827. if(pos >= offset+length)
  1828. goto done;
  1829. list_for_each(btmp, &fl->fl_block) {
  1830. struct file_lock *bfl = list_entry(btmp,
  1831. struct file_lock, fl_block);
  1832. lock_get_status(q, bfl, i, " ->");
  1833. move_lock_status(&q, &pos, offset);
  1834. if(pos >= offset+length)
  1835. goto done;
  1836. }
  1837. }
  1838. done:
  1839. unlock_kernel();
  1840. *start = buffer;
  1841. if(q-buffer < length)
  1842. return (q-buffer);
  1843. return length;
  1844. }
  1845. /**
  1846. * lock_may_read - checks that the region is free of locks
  1847. * @inode: the inode that is being read
  1848. * @start: the first byte to read
  1849. * @len: the number of bytes to read
  1850. *
  1851. * Emulates Windows locking requirements. Whole-file
  1852. * mandatory locks (share modes) can prohibit a read and
  1853. * byte-range POSIX locks can prohibit a read if they overlap.
  1854. *
  1855. * N.B. this function is only ever called
  1856. * from knfsd and ownership of locks is never checked.
  1857. */
  1858. int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
  1859. {
  1860. struct file_lock *fl;
  1861. int result = 1;
  1862. lock_kernel();
  1863. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1864. if (IS_POSIX(fl)) {
  1865. if (fl->fl_type == F_RDLCK)
  1866. continue;
  1867. if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
  1868. continue;
  1869. } else if (IS_FLOCK(fl)) {
  1870. if (!(fl->fl_type & LOCK_MAND))
  1871. continue;
  1872. if (fl->fl_type & LOCK_READ)
  1873. continue;
  1874. } else
  1875. continue;
  1876. result = 0;
  1877. break;
  1878. }
  1879. unlock_kernel();
  1880. return result;
  1881. }
  1882. EXPORT_SYMBOL(lock_may_read);
  1883. /**
  1884. * lock_may_write - checks that the region is free of locks
  1885. * @inode: the inode that is being written
  1886. * @start: the first byte to write
  1887. * @len: the number of bytes to write
  1888. *
  1889. * Emulates Windows locking requirements. Whole-file
  1890. * mandatory locks (share modes) can prohibit a write and
  1891. * byte-range POSIX locks can prohibit a write if they overlap.
  1892. *
  1893. * N.B. this function is only ever called
  1894. * from knfsd and ownership of locks is never checked.
  1895. */
  1896. int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
  1897. {
  1898. struct file_lock *fl;
  1899. int result = 1;
  1900. lock_kernel();
  1901. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1902. if (IS_POSIX(fl)) {
  1903. if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
  1904. continue;
  1905. } else if (IS_FLOCK(fl)) {
  1906. if (!(fl->fl_type & LOCK_MAND))
  1907. continue;
  1908. if (fl->fl_type & LOCK_WRITE)
  1909. continue;
  1910. } else
  1911. continue;
  1912. result = 0;
  1913. break;
  1914. }
  1915. unlock_kernel();
  1916. return result;
  1917. }
  1918. EXPORT_SYMBOL(lock_may_write);
  1919. static inline void __steal_locks(struct file *file, fl_owner_t from)
  1920. {
  1921. struct inode *inode = file->f_dentry->d_inode;
  1922. struct file_lock *fl = inode->i_flock;
  1923. while (fl) {
  1924. if (fl->fl_file == file && fl->fl_owner == from)
  1925. fl->fl_owner = current->files;
  1926. fl = fl->fl_next;
  1927. }
  1928. }
  1929. /* When getting ready for executing a binary, we make sure that current
  1930. * has a files_struct on its own. Before dropping the old files_struct,
  1931. * we take over ownership of all locks for all file descriptors we own.
  1932. * Note that we may accidentally steal a lock for a file that a sibling
  1933. * has created since the unshare_files() call.
  1934. */
  1935. void steal_locks(fl_owner_t from)
  1936. {
  1937. struct files_struct *files = current->files;
  1938. int i, j;
  1939. if (from == files)
  1940. return;
  1941. lock_kernel();
  1942. j = 0;
  1943. for (;;) {
  1944. unsigned long set;
  1945. i = j * __NFDBITS;
  1946. if (i >= files->max_fdset || i >= files->max_fds)
  1947. break;
  1948. set = files->open_fds->fds_bits[j++];
  1949. while (set) {
  1950. if (set & 1) {
  1951. struct file *file = files->fd[i];
  1952. if (file)
  1953. __steal_locks(file, from);
  1954. }
  1955. i++;
  1956. set >>= 1;
  1957. }
  1958. }
  1959. unlock_kernel();
  1960. }
  1961. EXPORT_SYMBOL(steal_locks);
  1962. static int __init filelock_init(void)
  1963. {
  1964. filelock_cache = kmem_cache_create("file_lock_cache",
  1965. sizeof(struct file_lock), 0, SLAB_PANIC,
  1966. init_once, NULL);
  1967. return 0;
  1968. }
  1969. core_initcall(filelock_init);