locks.c 70 KB

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