locks.c 55 KB

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