locks.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218
  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. static int lease_mylease_callback(struct file_lock *fl, struct file_lock *try)
  364. {
  365. return fl->fl_file == try->fl_file;
  366. }
  367. static 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. static 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 (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
  1376. file_lock.fl_ops->fl_release_private(&file_lock);
  1377. if (error < 0)
  1378. goto out;
  1379. else
  1380. fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
  1381. } else {
  1382. fl = posix_test_lock(filp, &file_lock);
  1383. }
  1384. flock.l_type = F_UNLCK;
  1385. if (fl != NULL) {
  1386. flock.l_pid = fl->fl_pid;
  1387. #if BITS_PER_LONG == 32
  1388. /*
  1389. * Make sure we can represent the posix lock via
  1390. * legacy 32bit flock.
  1391. */
  1392. error = -EOVERFLOW;
  1393. if (fl->fl_start > OFFT_OFFSET_MAX)
  1394. goto out;
  1395. if ((fl->fl_end != OFFSET_MAX)
  1396. && (fl->fl_end > OFFT_OFFSET_MAX))
  1397. goto out;
  1398. #endif
  1399. flock.l_start = fl->fl_start;
  1400. flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1401. fl->fl_end - fl->fl_start + 1;
  1402. flock.l_whence = 0;
  1403. flock.l_type = fl->fl_type;
  1404. }
  1405. error = -EFAULT;
  1406. if (!copy_to_user(l, &flock, sizeof(flock)))
  1407. error = 0;
  1408. out:
  1409. return error;
  1410. }
  1411. /* Apply the lock described by l to an open file descriptor.
  1412. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1413. */
  1414. int fcntl_setlk(struct file *filp, unsigned int cmd, struct flock __user *l)
  1415. {
  1416. struct file_lock *file_lock = locks_alloc_lock();
  1417. struct flock flock;
  1418. struct inode *inode;
  1419. int error;
  1420. if (file_lock == NULL)
  1421. return -ENOLCK;
  1422. /*
  1423. * This might block, so we do it before checking the inode.
  1424. */
  1425. error = -EFAULT;
  1426. if (copy_from_user(&flock, l, sizeof(flock)))
  1427. goto out;
  1428. inode = filp->f_dentry->d_inode;
  1429. /* Don't allow mandatory locks on files that may be memory mapped
  1430. * and shared.
  1431. */
  1432. if (IS_MANDLOCK(inode) &&
  1433. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1434. mapping_writably_mapped(filp->f_mapping)) {
  1435. error = -EAGAIN;
  1436. goto out;
  1437. }
  1438. error = flock_to_posix_lock(filp, file_lock, &flock);
  1439. if (error)
  1440. goto out;
  1441. if (cmd == F_SETLKW) {
  1442. file_lock->fl_flags |= FL_SLEEP;
  1443. }
  1444. error = -EBADF;
  1445. switch (flock.l_type) {
  1446. case F_RDLCK:
  1447. if (!(filp->f_mode & FMODE_READ))
  1448. goto out;
  1449. break;
  1450. case F_WRLCK:
  1451. if (!(filp->f_mode & FMODE_WRITE))
  1452. goto out;
  1453. break;
  1454. case F_UNLCK:
  1455. break;
  1456. default:
  1457. error = -EINVAL;
  1458. goto out;
  1459. }
  1460. error = security_file_lock(filp, file_lock->fl_type);
  1461. if (error)
  1462. goto out;
  1463. if (filp->f_op && filp->f_op->lock != NULL) {
  1464. error = filp->f_op->lock(filp, cmd, file_lock);
  1465. goto out;
  1466. }
  1467. for (;;) {
  1468. error = __posix_lock_file(inode, file_lock);
  1469. if ((error != -EAGAIN) || (cmd == F_SETLK))
  1470. break;
  1471. error = wait_event_interruptible(file_lock->fl_wait,
  1472. !file_lock->fl_next);
  1473. if (!error)
  1474. continue;
  1475. locks_delete_block(file_lock);
  1476. break;
  1477. }
  1478. out:
  1479. locks_free_lock(file_lock);
  1480. return error;
  1481. }
  1482. #if BITS_PER_LONG == 32
  1483. /* Report the first existing lock that would conflict with l.
  1484. * This implements the F_GETLK command of fcntl().
  1485. */
  1486. int fcntl_getlk64(struct file *filp, struct flock64 __user *l)
  1487. {
  1488. struct file_lock *fl, file_lock;
  1489. struct flock64 flock;
  1490. int error;
  1491. error = -EFAULT;
  1492. if (copy_from_user(&flock, l, sizeof(flock)))
  1493. goto out;
  1494. error = -EINVAL;
  1495. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1496. goto out;
  1497. error = flock64_to_posix_lock(filp, &file_lock, &flock);
  1498. if (error)
  1499. goto out;
  1500. if (filp->f_op && filp->f_op->lock) {
  1501. error = filp->f_op->lock(filp, F_GETLK, &file_lock);
  1502. if (file_lock.fl_ops && file_lock.fl_ops->fl_release_private)
  1503. file_lock.fl_ops->fl_release_private(&file_lock);
  1504. if (error < 0)
  1505. goto out;
  1506. else
  1507. fl = (file_lock.fl_type == F_UNLCK ? NULL : &file_lock);
  1508. } else {
  1509. fl = posix_test_lock(filp, &file_lock);
  1510. }
  1511. flock.l_type = F_UNLCK;
  1512. if (fl != NULL) {
  1513. flock.l_pid = fl->fl_pid;
  1514. flock.l_start = fl->fl_start;
  1515. flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1516. fl->fl_end - fl->fl_start + 1;
  1517. flock.l_whence = 0;
  1518. flock.l_type = fl->fl_type;
  1519. }
  1520. error = -EFAULT;
  1521. if (!copy_to_user(l, &flock, sizeof(flock)))
  1522. error = 0;
  1523. out:
  1524. return error;
  1525. }
  1526. /* Apply the lock described by l to an open file descriptor.
  1527. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1528. */
  1529. int fcntl_setlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
  1530. {
  1531. struct file_lock *file_lock = locks_alloc_lock();
  1532. struct flock64 flock;
  1533. struct inode *inode;
  1534. int error;
  1535. if (file_lock == NULL)
  1536. return -ENOLCK;
  1537. /*
  1538. * This might block, so we do it before checking the inode.
  1539. */
  1540. error = -EFAULT;
  1541. if (copy_from_user(&flock, l, sizeof(flock)))
  1542. goto out;
  1543. inode = filp->f_dentry->d_inode;
  1544. /* Don't allow mandatory locks on files that may be memory mapped
  1545. * and shared.
  1546. */
  1547. if (IS_MANDLOCK(inode) &&
  1548. (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  1549. mapping_writably_mapped(filp->f_mapping)) {
  1550. error = -EAGAIN;
  1551. goto out;
  1552. }
  1553. error = flock64_to_posix_lock(filp, file_lock, &flock);
  1554. if (error)
  1555. goto out;
  1556. if (cmd == F_SETLKW64) {
  1557. file_lock->fl_flags |= FL_SLEEP;
  1558. }
  1559. error = -EBADF;
  1560. switch (flock.l_type) {
  1561. case F_RDLCK:
  1562. if (!(filp->f_mode & FMODE_READ))
  1563. goto out;
  1564. break;
  1565. case F_WRLCK:
  1566. if (!(filp->f_mode & FMODE_WRITE))
  1567. goto out;
  1568. break;
  1569. case F_UNLCK:
  1570. break;
  1571. default:
  1572. error = -EINVAL;
  1573. goto out;
  1574. }
  1575. error = security_file_lock(filp, file_lock->fl_type);
  1576. if (error)
  1577. goto out;
  1578. if (filp->f_op && filp->f_op->lock != NULL) {
  1579. error = filp->f_op->lock(filp, cmd, file_lock);
  1580. goto out;
  1581. }
  1582. for (;;) {
  1583. error = __posix_lock_file(inode, file_lock);
  1584. if ((error != -EAGAIN) || (cmd == F_SETLK64))
  1585. break;
  1586. error = wait_event_interruptible(file_lock->fl_wait,
  1587. !file_lock->fl_next);
  1588. if (!error)
  1589. continue;
  1590. locks_delete_block(file_lock);
  1591. break;
  1592. }
  1593. out:
  1594. locks_free_lock(file_lock);
  1595. return error;
  1596. }
  1597. #endif /* BITS_PER_LONG == 32 */
  1598. /*
  1599. * This function is called when the file is being removed
  1600. * from the task's fd array. POSIX locks belonging to this task
  1601. * are deleted at this time.
  1602. */
  1603. void locks_remove_posix(struct file *filp, fl_owner_t owner)
  1604. {
  1605. struct file_lock lock, **before;
  1606. /*
  1607. * If there are no locks held on this file, we don't need to call
  1608. * posix_lock_file(). Another process could be setting a lock on this
  1609. * file at the same time, but we wouldn't remove that lock anyway.
  1610. */
  1611. before = &filp->f_dentry->d_inode->i_flock;
  1612. if (*before == NULL)
  1613. return;
  1614. lock.fl_type = F_UNLCK;
  1615. lock.fl_flags = FL_POSIX;
  1616. lock.fl_start = 0;
  1617. lock.fl_end = OFFSET_MAX;
  1618. lock.fl_owner = owner;
  1619. lock.fl_pid = current->tgid;
  1620. lock.fl_file = filp;
  1621. lock.fl_ops = NULL;
  1622. lock.fl_lmops = NULL;
  1623. if (filp->f_op && filp->f_op->lock != NULL) {
  1624. filp->f_op->lock(filp, F_SETLK, &lock);
  1625. goto out;
  1626. }
  1627. /* Can't use posix_lock_file here; we need to remove it no matter
  1628. * which pid we have.
  1629. */
  1630. lock_kernel();
  1631. while (*before != NULL) {
  1632. struct file_lock *fl = *before;
  1633. if (IS_POSIX(fl) && posix_same_owner(fl, &lock)) {
  1634. locks_delete_lock(before);
  1635. continue;
  1636. }
  1637. before = &fl->fl_next;
  1638. }
  1639. unlock_kernel();
  1640. out:
  1641. if (lock.fl_ops && lock.fl_ops->fl_release_private)
  1642. lock.fl_ops->fl_release_private(&lock);
  1643. }
  1644. EXPORT_SYMBOL(locks_remove_posix);
  1645. /*
  1646. * This function is called on the last close of an open file.
  1647. */
  1648. void locks_remove_flock(struct file *filp)
  1649. {
  1650. struct inode * inode = filp->f_dentry->d_inode;
  1651. struct file_lock *fl;
  1652. struct file_lock **before;
  1653. if (!inode->i_flock)
  1654. return;
  1655. if (filp->f_op && filp->f_op->flock) {
  1656. struct file_lock fl = {
  1657. .fl_pid = current->tgid,
  1658. .fl_file = filp,
  1659. .fl_flags = FL_FLOCK,
  1660. .fl_type = F_UNLCK,
  1661. .fl_end = OFFSET_MAX,
  1662. };
  1663. filp->f_op->flock(filp, F_SETLKW, &fl);
  1664. if (fl.fl_ops && fl.fl_ops->fl_release_private)
  1665. fl.fl_ops->fl_release_private(&fl);
  1666. }
  1667. lock_kernel();
  1668. before = &inode->i_flock;
  1669. while ((fl = *before) != NULL) {
  1670. if (fl->fl_file == filp) {
  1671. /*
  1672. * We might have a POSIX lock that was created at the same time
  1673. * the filp was closed for the last time. Just remove that too,
  1674. * regardless of ownership, since nobody can own it.
  1675. */
  1676. if (IS_FLOCK(fl) || IS_POSIX(fl)) {
  1677. locks_delete_lock(before);
  1678. continue;
  1679. }
  1680. if (IS_LEASE(fl)) {
  1681. lease_modify(before, F_UNLCK);
  1682. continue;
  1683. }
  1684. /* What? */
  1685. BUG();
  1686. }
  1687. before = &fl->fl_next;
  1688. }
  1689. unlock_kernel();
  1690. }
  1691. /**
  1692. * posix_block_lock - blocks waiting for a file lock
  1693. * @blocker: the lock which is blocking
  1694. * @waiter: the lock which conflicts and has to wait
  1695. *
  1696. * lockd needs to block waiting for locks.
  1697. */
  1698. void
  1699. posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
  1700. {
  1701. locks_insert_block(blocker, waiter);
  1702. }
  1703. EXPORT_SYMBOL(posix_block_lock);
  1704. /**
  1705. * posix_unblock_lock - stop waiting for a file lock
  1706. * @filp: how the file was opened
  1707. * @waiter: the lock which was waiting
  1708. *
  1709. * lockd needs to block waiting for locks.
  1710. */
  1711. void
  1712. posix_unblock_lock(struct file *filp, struct file_lock *waiter)
  1713. {
  1714. /*
  1715. * A remote machine may cancel the lock request after it's been
  1716. * granted locally. If that happens, we need to delete the lock.
  1717. */
  1718. lock_kernel();
  1719. if (waiter->fl_next) {
  1720. __locks_delete_block(waiter);
  1721. unlock_kernel();
  1722. } else {
  1723. unlock_kernel();
  1724. waiter->fl_type = F_UNLCK;
  1725. posix_lock_file(filp, waiter);
  1726. }
  1727. }
  1728. EXPORT_SYMBOL(posix_unblock_lock);
  1729. static void lock_get_status(char* out, struct file_lock *fl, int id, char *pfx)
  1730. {
  1731. struct inode *inode = NULL;
  1732. if (fl->fl_file != NULL)
  1733. inode = fl->fl_file->f_dentry->d_inode;
  1734. out += sprintf(out, "%d:%s ", id, pfx);
  1735. if (IS_POSIX(fl)) {
  1736. out += sprintf(out, "%6s %s ",
  1737. (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
  1738. (inode == NULL) ? "*NOINODE*" :
  1739. (IS_MANDLOCK(inode) &&
  1740. (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
  1741. "MANDATORY" : "ADVISORY ");
  1742. } else if (IS_FLOCK(fl)) {
  1743. if (fl->fl_type & LOCK_MAND) {
  1744. out += sprintf(out, "FLOCK MSNFS ");
  1745. } else {
  1746. out += sprintf(out, "FLOCK ADVISORY ");
  1747. }
  1748. } else if (IS_LEASE(fl)) {
  1749. out += sprintf(out, "LEASE ");
  1750. if (fl->fl_type & F_INPROGRESS)
  1751. out += sprintf(out, "BREAKING ");
  1752. else if (fl->fl_file)
  1753. out += sprintf(out, "ACTIVE ");
  1754. else
  1755. out += sprintf(out, "BREAKER ");
  1756. } else {
  1757. out += sprintf(out, "UNKNOWN UNKNOWN ");
  1758. }
  1759. if (fl->fl_type & LOCK_MAND) {
  1760. out += sprintf(out, "%s ",
  1761. (fl->fl_type & LOCK_READ)
  1762. ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
  1763. : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
  1764. } else {
  1765. out += sprintf(out, "%s ",
  1766. (fl->fl_type & F_INPROGRESS)
  1767. ? (fl->fl_type & F_UNLCK) ? "UNLCK" : "READ "
  1768. : (fl->fl_type & F_WRLCK) ? "WRITE" : "READ ");
  1769. }
  1770. if (inode) {
  1771. #ifdef WE_CAN_BREAK_LSLK_NOW
  1772. out += sprintf(out, "%d %s:%ld ", fl->fl_pid,
  1773. inode->i_sb->s_id, inode->i_ino);
  1774. #else
  1775. /* userspace relies on this representation of dev_t ;-( */
  1776. out += sprintf(out, "%d %02x:%02x:%ld ", fl->fl_pid,
  1777. MAJOR(inode->i_sb->s_dev),
  1778. MINOR(inode->i_sb->s_dev), inode->i_ino);
  1779. #endif
  1780. } else {
  1781. out += sprintf(out, "%d <none>:0 ", fl->fl_pid);
  1782. }
  1783. if (IS_POSIX(fl)) {
  1784. if (fl->fl_end == OFFSET_MAX)
  1785. out += sprintf(out, "%Ld EOF\n", fl->fl_start);
  1786. else
  1787. out += sprintf(out, "%Ld %Ld\n", fl->fl_start,
  1788. fl->fl_end);
  1789. } else {
  1790. out += sprintf(out, "0 EOF\n");
  1791. }
  1792. }
  1793. static void move_lock_status(char **p, off_t* pos, off_t offset)
  1794. {
  1795. int len;
  1796. len = strlen(*p);
  1797. if(*pos >= offset) {
  1798. /* the complete line is valid */
  1799. *p += len;
  1800. *pos += len;
  1801. return;
  1802. }
  1803. if(*pos+len > offset) {
  1804. /* use the second part of the line */
  1805. int i = offset-*pos;
  1806. memmove(*p,*p+i,len-i);
  1807. *p += len-i;
  1808. *pos += len;
  1809. return;
  1810. }
  1811. /* discard the complete line */
  1812. *pos += len;
  1813. }
  1814. /**
  1815. * get_locks_status - reports lock usage in /proc/locks
  1816. * @buffer: address in userspace to write into
  1817. * @start: ?
  1818. * @offset: how far we are through the buffer
  1819. * @length: how much to read
  1820. */
  1821. int get_locks_status(char *buffer, char **start, off_t offset, int length)
  1822. {
  1823. struct list_head *tmp;
  1824. char *q = buffer;
  1825. off_t pos = 0;
  1826. int i = 0;
  1827. lock_kernel();
  1828. list_for_each(tmp, &file_lock_list) {
  1829. struct list_head *btmp;
  1830. struct file_lock *fl = list_entry(tmp, struct file_lock, fl_link);
  1831. lock_get_status(q, fl, ++i, "");
  1832. move_lock_status(&q, &pos, offset);
  1833. if(pos >= offset+length)
  1834. goto done;
  1835. list_for_each(btmp, &fl->fl_block) {
  1836. struct file_lock *bfl = list_entry(btmp,
  1837. struct file_lock, fl_block);
  1838. lock_get_status(q, bfl, i, " ->");
  1839. move_lock_status(&q, &pos, offset);
  1840. if(pos >= offset+length)
  1841. goto done;
  1842. }
  1843. }
  1844. done:
  1845. unlock_kernel();
  1846. *start = buffer;
  1847. if(q-buffer < length)
  1848. return (q-buffer);
  1849. return length;
  1850. }
  1851. /**
  1852. * lock_may_read - checks that the region is free of locks
  1853. * @inode: the inode that is being read
  1854. * @start: the first byte to read
  1855. * @len: the number of bytes to read
  1856. *
  1857. * Emulates Windows locking requirements. Whole-file
  1858. * mandatory locks (share modes) can prohibit a read and
  1859. * byte-range POSIX locks can prohibit a read if they overlap.
  1860. *
  1861. * N.B. this function is only ever called
  1862. * from knfsd and ownership of locks is never checked.
  1863. */
  1864. int lock_may_read(struct inode *inode, loff_t start, unsigned long len)
  1865. {
  1866. struct file_lock *fl;
  1867. int result = 1;
  1868. lock_kernel();
  1869. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1870. if (IS_POSIX(fl)) {
  1871. if (fl->fl_type == F_RDLCK)
  1872. continue;
  1873. if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
  1874. continue;
  1875. } else if (IS_FLOCK(fl)) {
  1876. if (!(fl->fl_type & LOCK_MAND))
  1877. continue;
  1878. if (fl->fl_type & LOCK_READ)
  1879. continue;
  1880. } else
  1881. continue;
  1882. result = 0;
  1883. break;
  1884. }
  1885. unlock_kernel();
  1886. return result;
  1887. }
  1888. EXPORT_SYMBOL(lock_may_read);
  1889. /**
  1890. * lock_may_write - checks that the region is free of locks
  1891. * @inode: the inode that is being written
  1892. * @start: the first byte to write
  1893. * @len: the number of bytes to write
  1894. *
  1895. * Emulates Windows locking requirements. Whole-file
  1896. * mandatory locks (share modes) can prohibit a write and
  1897. * byte-range POSIX locks can prohibit a write if they overlap.
  1898. *
  1899. * N.B. this function is only ever called
  1900. * from knfsd and ownership of locks is never checked.
  1901. */
  1902. int lock_may_write(struct inode *inode, loff_t start, unsigned long len)
  1903. {
  1904. struct file_lock *fl;
  1905. int result = 1;
  1906. lock_kernel();
  1907. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1908. if (IS_POSIX(fl)) {
  1909. if ((fl->fl_end < start) || (fl->fl_start > (start + len)))
  1910. continue;
  1911. } else if (IS_FLOCK(fl)) {
  1912. if (!(fl->fl_type & LOCK_MAND))
  1913. continue;
  1914. if (fl->fl_type & LOCK_WRITE)
  1915. continue;
  1916. } else
  1917. continue;
  1918. result = 0;
  1919. break;
  1920. }
  1921. unlock_kernel();
  1922. return result;
  1923. }
  1924. EXPORT_SYMBOL(lock_may_write);
  1925. static inline void __steal_locks(struct file *file, fl_owner_t from)
  1926. {
  1927. struct inode *inode = file->f_dentry->d_inode;
  1928. struct file_lock *fl = inode->i_flock;
  1929. while (fl) {
  1930. if (fl->fl_file == file && fl->fl_owner == from)
  1931. fl->fl_owner = current->files;
  1932. fl = fl->fl_next;
  1933. }
  1934. }
  1935. /* When getting ready for executing a binary, we make sure that current
  1936. * has a files_struct on its own. Before dropping the old files_struct,
  1937. * we take over ownership of all locks for all file descriptors we own.
  1938. * Note that we may accidentally steal a lock for a file that a sibling
  1939. * has created since the unshare_files() call.
  1940. */
  1941. void steal_locks(fl_owner_t from)
  1942. {
  1943. struct files_struct *files = current->files;
  1944. int i, j;
  1945. if (from == files)
  1946. return;
  1947. lock_kernel();
  1948. j = 0;
  1949. for (;;) {
  1950. unsigned long set;
  1951. i = j * __NFDBITS;
  1952. if (i >= files->max_fdset || i >= files->max_fds)
  1953. break;
  1954. set = files->open_fds->fds_bits[j++];
  1955. while (set) {
  1956. if (set & 1) {
  1957. struct file *file = files->fd[i];
  1958. if (file)
  1959. __steal_locks(file, from);
  1960. }
  1961. i++;
  1962. set >>= 1;
  1963. }
  1964. }
  1965. unlock_kernel();
  1966. }
  1967. EXPORT_SYMBOL(steal_locks);
  1968. static int __init filelock_init(void)
  1969. {
  1970. filelock_cache = kmem_cache_create("file_lock_cache",
  1971. sizeof(struct file_lock), 0, SLAB_PANIC,
  1972. init_once, NULL);
  1973. return 0;
  1974. }
  1975. core_initcall(filelock_init);