locks.c 59 KB

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