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