locks.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/fs/locks.c
  4. *
  5. * Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
  6. * Doug Evans (dje@spiff.uucp), August 07, 1992
  7. *
  8. * Deadlock detection added.
  9. * FIXME: one thing isn't handled yet:
  10. * - mandatory locks (requires lots of changes elsewhere)
  11. * Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
  12. *
  13. * Miscellaneous edits, and a total rewrite of posix_lock_file() code.
  14. * Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
  15. *
  16. * Converted file_lock_table to a linked list from an array, which eliminates
  17. * the limits on how many active file locks are open.
  18. * Chad Page (pageone@netcom.com), November 27, 1994
  19. *
  20. * Removed dependency on file descriptors. dup()'ed file descriptors now
  21. * get the same locks as the original file descriptors, and a close() on
  22. * any file descriptor removes ALL the locks on the file for the current
  23. * process. Since locks still depend on the process id, locks are inherited
  24. * after an exec() but not after a fork(). This agrees with POSIX, and both
  25. * BSD and SVR4 practice.
  26. * Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
  27. *
  28. * Scrapped free list which is redundant now that we allocate locks
  29. * dynamically with kmalloc()/kfree().
  30. * Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
  31. *
  32. * Implemented two lock personalities - FL_FLOCK and FL_POSIX.
  33. *
  34. * FL_POSIX locks are created with calls to fcntl() and lockf() through the
  35. * fcntl() system call. They have the semantics described above.
  36. *
  37. * FL_FLOCK locks are created with calls to flock(), through the flock()
  38. * system call, which is new. Old C libraries implement flock() via fcntl()
  39. * and will continue to use the old, broken implementation.
  40. *
  41. * FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
  42. * with a file pointer (filp). As a result they can be shared by a parent
  43. * process and its children after a fork(). They are removed when the last
  44. * file descriptor referring to the file pointer is closed (unless explicitly
  45. * unlocked).
  46. *
  47. * FL_FLOCK locks never deadlock, an existing lock is always removed before
  48. * upgrading from shared to exclusive (or vice versa). When this happens
  49. * any processes blocked by the current lock are woken up and allowed to
  50. * run before the new lock is applied.
  51. * Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
  52. *
  53. * Removed some race conditions in flock_lock_file(), marked other possible
  54. * races. Just grep for FIXME to see them.
  55. * Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
  56. *
  57. * Addressed Dmitry's concerns. Deadlock checking no longer recursive.
  58. * Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
  59. * once we've checked for blocking and deadlocking.
  60. * Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
  61. *
  62. * Initial implementation of mandatory locks. SunOS turned out to be
  63. * a rotten model, so I implemented the "obvious" semantics.
  64. * See 'Documentation/filesystems/mandatory-locking.rst' for details.
  65. * Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
  66. *
  67. * Don't allow mandatory locks on mmap()'ed files. Added simple functions to
  68. * check if a file has mandatory locks, used by mmap(), open() and creat() to
  69. * see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
  70. * Manual, Section 2.
  71. * Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
  72. *
  73. * Tidied up block list handling. Added '/proc/locks' interface.
  74. * Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
  75. *
  76. * Fixed deadlock condition for pathological code that mixes calls to
  77. * flock() and fcntl().
  78. * Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
  79. *
  80. * Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
  81. * for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
  82. * guarantee sensible behaviour in the case where file system modules might
  83. * be compiled with different options than the kernel itself.
  84. * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  85. *
  86. * Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
  87. * (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
  88. * Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  89. *
  90. * Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
  91. * locks. Changed process synchronisation to avoid dereferencing locks that
  92. * have already been freed.
  93. * Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
  94. *
  95. * Made the block list a circular list to minimise searching in the list.
  96. * Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
  97. *
  98. * Made mandatory locking a mount option. Default is not to allow mandatory
  99. * locking.
  100. * Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
  101. *
  102. * Some adaptations for NFS support.
  103. * Olaf Kirch (okir@monad.swb.de), Dec 1996,
  104. *
  105. * Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
  106. * Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
  107. *
  108. * Use slab allocator instead of kmalloc/kfree.
  109. * Use generic list implementation from <linux/list.h>.
  110. * Sped up posix_locks_deadlock by only considering blocked locks.
  111. * Matthew Wilcox <willy@debian.org>, March, 2000.
  112. *
  113. * Leases and LOCK_MAND
  114. * Matthew Wilcox <willy@debian.org>, June, 2000.
  115. * Stephen Rothwell <sfr@canb.auug.org.au>, June, 2000.
  116. *
  117. * Locking conflicts and dependencies:
  118. * If multiple threads attempt to lock the same byte (or flock the same file)
  119. * only one can be granted the lock, and other must wait their turn.
  120. * The first lock has been "applied" or "granted", the others are "waiting"
  121. * and are "blocked" by the "applied" lock..
  122. *
  123. * Waiting and applied locks are all kept in trees whose properties are:
  124. *
  125. * - the root of a tree may be an applied or waiting lock.
  126. * - every other node in the tree is a waiting lock that
  127. * conflicts with every ancestor of that node.
  128. *
  129. * Every such tree begins life as a waiting singleton which obviously
  130. * satisfies the above properties.
  131. *
  132. * The only ways we modify trees preserve these properties:
  133. *
  134. * 1. We may add a new leaf node, but only after first verifying that it
  135. * conflicts with all of its ancestors.
  136. * 2. We may remove the root of a tree, creating a new singleton
  137. * tree from the root and N new trees rooted in the immediate
  138. * children.
  139. * 3. If the root of a tree is not currently an applied lock, we may
  140. * apply it (if possible).
  141. * 4. We may upgrade the root of the tree (either extend its range,
  142. * or upgrade its entire range from read to write).
  143. *
  144. * When an applied lock is modified in a way that reduces or downgrades any
  145. * part of its range, we remove all its children (2 above). This particularly
  146. * happens when a lock is unlocked.
  147. *
  148. * For each of those child trees we "wake up" the thread which is
  149. * waiting for the lock so it can continue handling as follows: if the
  150. * root of the tree applies, we do so (3). If it doesn't, it must
  151. * conflict with some applied lock. We remove (wake up) all of its children
  152. * (2), and add it is a new leaf to the tree rooted in the applied
  153. * lock (1). We then repeat the process recursively with those
  154. * children.
  155. *
  156. */
  157. #include <linux/capability.h>
  158. #include <linux/file.h>
  159. #include <linux/fdtable.h>
  160. #include <linux/fs.h>
  161. #include <linux/init.h>
  162. #include <linux/security.h>
  163. #include <linux/slab.h>
  164. #include <linux/syscalls.h>
  165. #include <linux/time.h>
  166. #include <linux/rcupdate.h>
  167. #include <linux/pid_namespace.h>
  168. #include <linux/hashtable.h>
  169. #include <linux/percpu.h>
  170. #define CREATE_TRACE_POINTS
  171. #include <trace/events/filelock.h>
  172. #include <linux/uaccess.h>
  173. #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
  174. #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
  175. #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG|FL_LAYOUT))
  176. #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
  177. #define IS_REMOTELCK(fl) (fl->fl_pid <= 0)
  178. static bool lease_breaking(struct file_lock *fl)
  179. {
  180. return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
  181. }
  182. static int target_leasetype(struct file_lock *fl)
  183. {
  184. if (fl->fl_flags & FL_UNLOCK_PENDING)
  185. return F_UNLCK;
  186. if (fl->fl_flags & FL_DOWNGRADE_PENDING)
  187. return F_RDLCK;
  188. return fl->fl_type;
  189. }
  190. int leases_enable = 1;
  191. int lease_break_time = 45;
  192. /*
  193. * The global file_lock_list is only used for displaying /proc/locks, so we
  194. * keep a list on each CPU, with each list protected by its own spinlock.
  195. * Global serialization is done using file_rwsem.
  196. *
  197. * Note that alterations to the list also require that the relevant flc_lock is
  198. * held.
  199. */
  200. struct file_lock_list_struct {
  201. spinlock_t lock;
  202. struct hlist_head hlist;
  203. };
  204. static DEFINE_PER_CPU(struct file_lock_list_struct, file_lock_list);
  205. DEFINE_STATIC_PERCPU_RWSEM(file_rwsem);
  206. /*
  207. * The blocked_hash is used to find POSIX lock loops for deadlock detection.
  208. * It is protected by blocked_lock_lock.
  209. *
  210. * We hash locks by lockowner in order to optimize searching for the lock a
  211. * particular lockowner is waiting on.
  212. *
  213. * FIXME: make this value scale via some heuristic? We generally will want more
  214. * buckets when we have more lockowners holding locks, but that's a little
  215. * difficult to determine without knowing what the workload will look like.
  216. */
  217. #define BLOCKED_HASH_BITS 7
  218. static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
  219. /*
  220. * This lock protects the blocked_hash. Generally, if you're accessing it, you
  221. * want to be holding this lock.
  222. *
  223. * In addition, it also protects the fl->fl_blocked_requests list, and the
  224. * fl->fl_blocker pointer for file_lock structures that are acting as lock
  225. * requests (in contrast to those that are acting as records of acquired locks).
  226. *
  227. * Note that when we acquire this lock in order to change the above fields,
  228. * we often hold the flc_lock as well. In certain cases, when reading the fields
  229. * protected by this lock, we can skip acquiring it iff we already hold the
  230. * flc_lock.
  231. */
  232. static DEFINE_SPINLOCK(blocked_lock_lock);
  233. static struct kmem_cache *flctx_cache __read_mostly;
  234. static struct kmem_cache *filelock_cache __read_mostly;
  235. static struct file_lock_context *
  236. locks_get_lock_context(struct inode *inode, int type)
  237. {
  238. struct file_lock_context *ctx;
  239. /* paired with cmpxchg() below */
  240. ctx = smp_load_acquire(&inode->i_flctx);
  241. if (likely(ctx) || type == F_UNLCK)
  242. goto out;
  243. ctx = kmem_cache_alloc(flctx_cache, GFP_KERNEL);
  244. if (!ctx)
  245. goto out;
  246. spin_lock_init(&ctx->flc_lock);
  247. INIT_LIST_HEAD(&ctx->flc_flock);
  248. INIT_LIST_HEAD(&ctx->flc_posix);
  249. INIT_LIST_HEAD(&ctx->flc_lease);
  250. /*
  251. * Assign the pointer if it's not already assigned. If it is, then
  252. * free the context we just allocated.
  253. */
  254. if (cmpxchg(&inode->i_flctx, NULL, ctx)) {
  255. kmem_cache_free(flctx_cache, ctx);
  256. ctx = smp_load_acquire(&inode->i_flctx);
  257. }
  258. out:
  259. trace_locks_get_lock_context(inode, type, ctx);
  260. return ctx;
  261. }
  262. static void
  263. locks_dump_ctx_list(struct list_head *list, char *list_type)
  264. {
  265. struct file_lock *fl;
  266. list_for_each_entry(fl, list, fl_list) {
  267. pr_warn("%s: fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n", list_type, fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
  268. }
  269. }
  270. static void
  271. locks_check_ctx_lists(struct inode *inode)
  272. {
  273. struct file_lock_context *ctx = inode->i_flctx;
  274. if (unlikely(!list_empty(&ctx->flc_flock) ||
  275. !list_empty(&ctx->flc_posix) ||
  276. !list_empty(&ctx->flc_lease))) {
  277. pr_warn("Leaked locks on dev=0x%x:0x%x ino=0x%lx:\n",
  278. MAJOR(inode->i_sb->s_dev), MINOR(inode->i_sb->s_dev),
  279. inode->i_ino);
  280. locks_dump_ctx_list(&ctx->flc_flock, "FLOCK");
  281. locks_dump_ctx_list(&ctx->flc_posix, "POSIX");
  282. locks_dump_ctx_list(&ctx->flc_lease, "LEASE");
  283. }
  284. }
  285. static void
  286. locks_check_ctx_file_list(struct file *filp, struct list_head *list,
  287. char *list_type)
  288. {
  289. struct file_lock *fl;
  290. struct inode *inode = locks_inode(filp);
  291. list_for_each_entry(fl, list, fl_list)
  292. if (fl->fl_file == filp)
  293. pr_warn("Leaked %s lock on dev=0x%x:0x%x ino=0x%lx "
  294. " fl_owner=%p fl_flags=0x%x fl_type=0x%x fl_pid=%u\n",
  295. list_type, MAJOR(inode->i_sb->s_dev),
  296. MINOR(inode->i_sb->s_dev), inode->i_ino,
  297. fl->fl_owner, fl->fl_flags, fl->fl_type, fl->fl_pid);
  298. }
  299. void
  300. locks_free_lock_context(struct inode *inode)
  301. {
  302. struct file_lock_context *ctx = inode->i_flctx;
  303. if (unlikely(ctx)) {
  304. locks_check_ctx_lists(inode);
  305. kmem_cache_free(flctx_cache, ctx);
  306. }
  307. }
  308. static void locks_init_lock_heads(struct file_lock *fl)
  309. {
  310. INIT_HLIST_NODE(&fl->fl_link);
  311. INIT_LIST_HEAD(&fl->fl_list);
  312. INIT_LIST_HEAD(&fl->fl_blocked_requests);
  313. INIT_LIST_HEAD(&fl->fl_blocked_member);
  314. init_waitqueue_head(&fl->fl_wait);
  315. }
  316. /* Allocate an empty lock structure. */
  317. struct file_lock *locks_alloc_lock(void)
  318. {
  319. struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
  320. if (fl)
  321. locks_init_lock_heads(fl);
  322. return fl;
  323. }
  324. EXPORT_SYMBOL_GPL(locks_alloc_lock);
  325. void locks_release_private(struct file_lock *fl)
  326. {
  327. BUG_ON(waitqueue_active(&fl->fl_wait));
  328. BUG_ON(!list_empty(&fl->fl_list));
  329. BUG_ON(!list_empty(&fl->fl_blocked_requests));
  330. BUG_ON(!list_empty(&fl->fl_blocked_member));
  331. BUG_ON(!hlist_unhashed(&fl->fl_link));
  332. if (fl->fl_ops) {
  333. if (fl->fl_ops->fl_release_private)
  334. fl->fl_ops->fl_release_private(fl);
  335. fl->fl_ops = NULL;
  336. }
  337. if (fl->fl_lmops) {
  338. if (fl->fl_lmops->lm_put_owner) {
  339. fl->fl_lmops->lm_put_owner(fl->fl_owner);
  340. fl->fl_owner = NULL;
  341. }
  342. fl->fl_lmops = NULL;
  343. }
  344. }
  345. EXPORT_SYMBOL_GPL(locks_release_private);
  346. /* Free a lock which is not in use. */
  347. void locks_free_lock(struct file_lock *fl)
  348. {
  349. locks_release_private(fl);
  350. kmem_cache_free(filelock_cache, fl);
  351. }
  352. EXPORT_SYMBOL(locks_free_lock);
  353. static void
  354. locks_dispose_list(struct list_head *dispose)
  355. {
  356. struct file_lock *fl;
  357. while (!list_empty(dispose)) {
  358. fl = list_first_entry(dispose, struct file_lock, fl_list);
  359. list_del_init(&fl->fl_list);
  360. locks_free_lock(fl);
  361. }
  362. }
  363. void locks_init_lock(struct file_lock *fl)
  364. {
  365. memset(fl, 0, sizeof(struct file_lock));
  366. locks_init_lock_heads(fl);
  367. }
  368. EXPORT_SYMBOL(locks_init_lock);
  369. /*
  370. * Initialize a new lock from an existing file_lock structure.
  371. */
  372. void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
  373. {
  374. new->fl_owner = fl->fl_owner;
  375. new->fl_pid = fl->fl_pid;
  376. new->fl_file = NULL;
  377. new->fl_flags = fl->fl_flags;
  378. new->fl_type = fl->fl_type;
  379. new->fl_start = fl->fl_start;
  380. new->fl_end = fl->fl_end;
  381. new->fl_lmops = fl->fl_lmops;
  382. new->fl_ops = NULL;
  383. if (fl->fl_lmops) {
  384. if (fl->fl_lmops->lm_get_owner)
  385. fl->fl_lmops->lm_get_owner(fl->fl_owner);
  386. }
  387. }
  388. EXPORT_SYMBOL(locks_copy_conflock);
  389. void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  390. {
  391. /* "new" must be a freshly-initialized lock */
  392. WARN_ON_ONCE(new->fl_ops);
  393. locks_copy_conflock(new, fl);
  394. new->fl_file = fl->fl_file;
  395. new->fl_ops = fl->fl_ops;
  396. if (fl->fl_ops) {
  397. if (fl->fl_ops->fl_copy_lock)
  398. fl->fl_ops->fl_copy_lock(new, fl);
  399. }
  400. }
  401. EXPORT_SYMBOL(locks_copy_lock);
  402. static void locks_move_blocks(struct file_lock *new, struct file_lock *fl)
  403. {
  404. struct file_lock *f;
  405. /*
  406. * As ctx->flc_lock is held, new requests cannot be added to
  407. * ->fl_blocked_requests, so we don't need a lock to check if it
  408. * is empty.
  409. */
  410. if (list_empty(&fl->fl_blocked_requests))
  411. return;
  412. spin_lock(&blocked_lock_lock);
  413. list_splice_init(&fl->fl_blocked_requests, &new->fl_blocked_requests);
  414. list_for_each_entry(f, &new->fl_blocked_requests, fl_blocked_member)
  415. f->fl_blocker = new;
  416. spin_unlock(&blocked_lock_lock);
  417. }
  418. static inline int flock_translate_cmd(int cmd) {
  419. if (cmd & LOCK_MAND)
  420. return cmd & (LOCK_MAND | LOCK_RW);
  421. switch (cmd) {
  422. case LOCK_SH:
  423. return F_RDLCK;
  424. case LOCK_EX:
  425. return F_WRLCK;
  426. case LOCK_UN:
  427. return F_UNLCK;
  428. }
  429. return -EINVAL;
  430. }
  431. /* Fill in a file_lock structure with an appropriate FLOCK lock. */
  432. static struct file_lock *
  433. flock_make_lock(struct file *filp, unsigned int cmd, struct file_lock *fl)
  434. {
  435. int type = flock_translate_cmd(cmd);
  436. if (type < 0)
  437. return ERR_PTR(type);
  438. if (fl == NULL) {
  439. fl = locks_alloc_lock();
  440. if (fl == NULL)
  441. return ERR_PTR(-ENOMEM);
  442. } else {
  443. locks_init_lock(fl);
  444. }
  445. fl->fl_file = filp;
  446. fl->fl_owner = filp;
  447. fl->fl_pid = current->tgid;
  448. fl->fl_flags = FL_FLOCK;
  449. fl->fl_type = type;
  450. fl->fl_end = OFFSET_MAX;
  451. return fl;
  452. }
  453. static int assign_type(struct file_lock *fl, long type)
  454. {
  455. switch (type) {
  456. case F_RDLCK:
  457. case F_WRLCK:
  458. case F_UNLCK:
  459. fl->fl_type = type;
  460. break;
  461. default:
  462. return -EINVAL;
  463. }
  464. return 0;
  465. }
  466. static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
  467. struct flock64 *l)
  468. {
  469. switch (l->l_whence) {
  470. case SEEK_SET:
  471. fl->fl_start = 0;
  472. break;
  473. case SEEK_CUR:
  474. fl->fl_start = filp->f_pos;
  475. break;
  476. case SEEK_END:
  477. fl->fl_start = i_size_read(file_inode(filp));
  478. break;
  479. default:
  480. return -EINVAL;
  481. }
  482. if (l->l_start > OFFSET_MAX - fl->fl_start)
  483. return -EOVERFLOW;
  484. fl->fl_start += l->l_start;
  485. if (fl->fl_start < 0)
  486. return -EINVAL;
  487. /* POSIX-1996 leaves the case l->l_len < 0 undefined;
  488. POSIX-2001 defines it. */
  489. if (l->l_len > 0) {
  490. if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
  491. return -EOVERFLOW;
  492. fl->fl_end = fl->fl_start + l->l_len - 1;
  493. } else if (l->l_len < 0) {
  494. if (fl->fl_start + l->l_len < 0)
  495. return -EINVAL;
  496. fl->fl_end = fl->fl_start - 1;
  497. fl->fl_start += l->l_len;
  498. } else
  499. fl->fl_end = OFFSET_MAX;
  500. fl->fl_owner = current->files;
  501. fl->fl_pid = current->tgid;
  502. fl->fl_file = filp;
  503. fl->fl_flags = FL_POSIX;
  504. fl->fl_ops = NULL;
  505. fl->fl_lmops = NULL;
  506. return assign_type(fl, l->l_type);
  507. }
  508. /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  509. * style lock.
  510. */
  511. static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
  512. struct flock *l)
  513. {
  514. struct flock64 ll = {
  515. .l_type = l->l_type,
  516. .l_whence = l->l_whence,
  517. .l_start = l->l_start,
  518. .l_len = l->l_len,
  519. };
  520. return flock64_to_posix_lock(filp, fl, &ll);
  521. }
  522. /* default lease lock manager operations */
  523. static bool
  524. lease_break_callback(struct file_lock *fl)
  525. {
  526. kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
  527. return false;
  528. }
  529. static void
  530. lease_setup(struct file_lock *fl, void **priv)
  531. {
  532. struct file *filp = fl->fl_file;
  533. struct fasync_struct *fa = *priv;
  534. /*
  535. * fasync_insert_entry() returns the old entry if any. If there was no
  536. * old entry, then it used "priv" and inserted it into the fasync list.
  537. * Clear the pointer to indicate that it shouldn't be freed.
  538. */
  539. if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
  540. *priv = NULL;
  541. __f_setown(filp, task_pid(current), PIDTYPE_TGID, 0);
  542. }
  543. static const struct lock_manager_operations lease_manager_ops = {
  544. .lm_break = lease_break_callback,
  545. .lm_change = lease_modify,
  546. .lm_setup = lease_setup,
  547. };
  548. /*
  549. * Initialize a lease, use the default lock manager operations
  550. */
  551. static int lease_init(struct file *filp, long type, struct file_lock *fl)
  552. {
  553. if (assign_type(fl, type) != 0)
  554. return -EINVAL;
  555. fl->fl_owner = filp;
  556. fl->fl_pid = current->tgid;
  557. fl->fl_file = filp;
  558. fl->fl_flags = FL_LEASE;
  559. fl->fl_start = 0;
  560. fl->fl_end = OFFSET_MAX;
  561. fl->fl_ops = NULL;
  562. fl->fl_lmops = &lease_manager_ops;
  563. return 0;
  564. }
  565. /* Allocate a file_lock initialised to this type of lease */
  566. static struct file_lock *lease_alloc(struct file *filp, long type)
  567. {
  568. struct file_lock *fl = locks_alloc_lock();
  569. int error = -ENOMEM;
  570. if (fl == NULL)
  571. return ERR_PTR(error);
  572. error = lease_init(filp, type, fl);
  573. if (error) {
  574. locks_free_lock(fl);
  575. return ERR_PTR(error);
  576. }
  577. return fl;
  578. }
  579. /* Check if two locks overlap each other.
  580. */
  581. static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
  582. {
  583. return ((fl1->fl_end >= fl2->fl_start) &&
  584. (fl2->fl_end >= fl1->fl_start));
  585. }
  586. /*
  587. * Check whether two locks have the same owner.
  588. */
  589. static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  590. {
  591. return fl1->fl_owner == fl2->fl_owner;
  592. }
  593. /* Must be called with the flc_lock held! */
  594. static void locks_insert_global_locks(struct file_lock *fl)
  595. {
  596. struct file_lock_list_struct *fll = this_cpu_ptr(&file_lock_list);
  597. percpu_rwsem_assert_held(&file_rwsem);
  598. spin_lock(&fll->lock);
  599. fl->fl_link_cpu = smp_processor_id();
  600. hlist_add_head(&fl->fl_link, &fll->hlist);
  601. spin_unlock(&fll->lock);
  602. }
  603. /* Must be called with the flc_lock held! */
  604. static void locks_delete_global_locks(struct file_lock *fl)
  605. {
  606. struct file_lock_list_struct *fll;
  607. percpu_rwsem_assert_held(&file_rwsem);
  608. /*
  609. * Avoid taking lock if already unhashed. This is safe since this check
  610. * is done while holding the flc_lock, and new insertions into the list
  611. * also require that it be held.
  612. */
  613. if (hlist_unhashed(&fl->fl_link))
  614. return;
  615. fll = per_cpu_ptr(&file_lock_list, fl->fl_link_cpu);
  616. spin_lock(&fll->lock);
  617. hlist_del_init(&fl->fl_link);
  618. spin_unlock(&fll->lock);
  619. }
  620. static unsigned long
  621. posix_owner_key(struct file_lock *fl)
  622. {
  623. return (unsigned long)fl->fl_owner;
  624. }
  625. static void locks_insert_global_blocked(struct file_lock *waiter)
  626. {
  627. lockdep_assert_held(&blocked_lock_lock);
  628. hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
  629. }
  630. static void locks_delete_global_blocked(struct file_lock *waiter)
  631. {
  632. lockdep_assert_held(&blocked_lock_lock);
  633. hash_del(&waiter->fl_link);
  634. }
  635. /* Remove waiter from blocker's block list.
  636. * When blocker ends up pointing to itself then the list is empty.
  637. *
  638. * Must be called with blocked_lock_lock held.
  639. */
  640. static void __locks_delete_block(struct file_lock *waiter)
  641. {
  642. locks_delete_global_blocked(waiter);
  643. list_del_init(&waiter->fl_blocked_member);
  644. }
  645. static void __locks_wake_up_blocks(struct file_lock *blocker)
  646. {
  647. while (!list_empty(&blocker->fl_blocked_requests)) {
  648. struct file_lock *waiter;
  649. waiter = list_first_entry(&blocker->fl_blocked_requests,
  650. struct file_lock, fl_blocked_member);
  651. __locks_delete_block(waiter);
  652. if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
  653. waiter->fl_lmops->lm_notify(waiter);
  654. else
  655. wake_up(&waiter->fl_wait);
  656. /*
  657. * The setting of fl_blocker to NULL marks the "done"
  658. * point in deleting a block. Paired with acquire at the top
  659. * of locks_delete_block().
  660. */
  661. smp_store_release(&waiter->fl_blocker, NULL);
  662. }
  663. }
  664. /**
  665. * locks_delete_lock - stop waiting for a file lock
  666. * @waiter: the lock which was waiting
  667. *
  668. * lockd/nfsd need to disconnect the lock while working on it.
  669. */
  670. int locks_delete_block(struct file_lock *waiter)
  671. {
  672. int status = -ENOENT;
  673. /*
  674. * If fl_blocker is NULL, it won't be set again as this thread "owns"
  675. * the lock and is the only one that might try to claim the lock.
  676. *
  677. * We use acquire/release to manage fl_blocker so that we can
  678. * optimize away taking the blocked_lock_lock in many cases.
  679. *
  680. * The smp_load_acquire guarantees two things:
  681. *
  682. * 1/ that fl_blocked_requests can be tested locklessly. If something
  683. * was recently added to that list it must have been in a locked region
  684. * *before* the locked region when fl_blocker was set to NULL.
  685. *
  686. * 2/ that no other thread is accessing 'waiter', so it is safe to free
  687. * it. __locks_wake_up_blocks is careful not to touch waiter after
  688. * fl_blocker is released.
  689. *
  690. * If a lockless check of fl_blocker shows it to be NULL, we know that
  691. * no new locks can be inserted into its fl_blocked_requests list, and
  692. * can avoid doing anything further if the list is empty.
  693. */
  694. if (!smp_load_acquire(&waiter->fl_blocker) &&
  695. list_empty(&waiter->fl_blocked_requests))
  696. return status;
  697. spin_lock(&blocked_lock_lock);
  698. if (waiter->fl_blocker)
  699. status = 0;
  700. __locks_wake_up_blocks(waiter);
  701. __locks_delete_block(waiter);
  702. /*
  703. * The setting of fl_blocker to NULL marks the "done" point in deleting
  704. * a block. Paired with acquire at the top of this function.
  705. */
  706. smp_store_release(&waiter->fl_blocker, NULL);
  707. spin_unlock(&blocked_lock_lock);
  708. return status;
  709. }
  710. EXPORT_SYMBOL(locks_delete_block);
  711. /* Insert waiter into blocker's block list.
  712. * We use a circular list so that processes can be easily woken up in
  713. * the order they blocked. The documentation doesn't require this but
  714. * it seems like the reasonable thing to do.
  715. *
  716. * Must be called with both the flc_lock and blocked_lock_lock held. The
  717. * fl_blocked_requests list itself is protected by the blocked_lock_lock,
  718. * but by ensuring that the flc_lock is also held on insertions we can avoid
  719. * taking the blocked_lock_lock in some cases when we see that the
  720. * fl_blocked_requests list is empty.
  721. *
  722. * Rather than just adding to the list, we check for conflicts with any existing
  723. * waiters, and add beneath any waiter that blocks the new waiter.
  724. * Thus wakeups don't happen until needed.
  725. */
  726. static void __locks_insert_block(struct file_lock *blocker,
  727. struct file_lock *waiter,
  728. bool conflict(struct file_lock *,
  729. struct file_lock *))
  730. {
  731. struct file_lock *fl;
  732. BUG_ON(!list_empty(&waiter->fl_blocked_member));
  733. new_blocker:
  734. list_for_each_entry(fl, &blocker->fl_blocked_requests, fl_blocked_member)
  735. if (conflict(fl, waiter)) {
  736. blocker = fl;
  737. goto new_blocker;
  738. }
  739. waiter->fl_blocker = blocker;
  740. list_add_tail(&waiter->fl_blocked_member, &blocker->fl_blocked_requests);
  741. if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
  742. locks_insert_global_blocked(waiter);
  743. /* The requests in waiter->fl_blocked are known to conflict with
  744. * waiter, but might not conflict with blocker, or the requests
  745. * and lock which block it. So they all need to be woken.
  746. */
  747. __locks_wake_up_blocks(waiter);
  748. }
  749. /* Must be called with flc_lock held. */
  750. static void locks_insert_block(struct file_lock *blocker,
  751. struct file_lock *waiter,
  752. bool conflict(struct file_lock *,
  753. struct file_lock *))
  754. {
  755. spin_lock(&blocked_lock_lock);
  756. __locks_insert_block(blocker, waiter, conflict);
  757. spin_unlock(&blocked_lock_lock);
  758. }
  759. /*
  760. * Wake up processes blocked waiting for blocker.
  761. *
  762. * Must be called with the inode->flc_lock held!
  763. */
  764. static void locks_wake_up_blocks(struct file_lock *blocker)
  765. {
  766. /*
  767. * Avoid taking global lock if list is empty. This is safe since new
  768. * blocked requests are only added to the list under the flc_lock, and
  769. * the flc_lock is always held here. Note that removal from the
  770. * fl_blocked_requests list does not require the flc_lock, so we must
  771. * recheck list_empty() after acquiring the blocked_lock_lock.
  772. */
  773. if (list_empty(&blocker->fl_blocked_requests))
  774. return;
  775. spin_lock(&blocked_lock_lock);
  776. __locks_wake_up_blocks(blocker);
  777. spin_unlock(&blocked_lock_lock);
  778. }
  779. static void
  780. locks_insert_lock_ctx(struct file_lock *fl, struct list_head *before)
  781. {
  782. list_add_tail(&fl->fl_list, before);
  783. locks_insert_global_locks(fl);
  784. }
  785. static void
  786. locks_unlink_lock_ctx(struct file_lock *fl)
  787. {
  788. locks_delete_global_locks(fl);
  789. list_del_init(&fl->fl_list);
  790. locks_wake_up_blocks(fl);
  791. }
  792. static void
  793. locks_delete_lock_ctx(struct file_lock *fl, struct list_head *dispose)
  794. {
  795. locks_unlink_lock_ctx(fl);
  796. if (dispose)
  797. list_add(&fl->fl_list, dispose);
  798. else
  799. locks_free_lock(fl);
  800. }
  801. /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
  802. * checks for shared/exclusive status of overlapping locks.
  803. */
  804. static bool locks_conflict(struct file_lock *caller_fl,
  805. struct file_lock *sys_fl)
  806. {
  807. if (sys_fl->fl_type == F_WRLCK)
  808. return true;
  809. if (caller_fl->fl_type == F_WRLCK)
  810. return true;
  811. return false;
  812. }
  813. /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
  814. * checking before calling the locks_conflict().
  815. */
  816. static bool posix_locks_conflict(struct file_lock *caller_fl,
  817. struct file_lock *sys_fl)
  818. {
  819. /* POSIX locks owned by the same process do not conflict with
  820. * each other.
  821. */
  822. if (posix_same_owner(caller_fl, sys_fl))
  823. return false;
  824. /* Check whether they overlap */
  825. if (!locks_overlap(caller_fl, sys_fl))
  826. return false;
  827. return locks_conflict(caller_fl, sys_fl);
  828. }
  829. /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  830. * checking before calling the locks_conflict().
  831. */
  832. static bool flock_locks_conflict(struct file_lock *caller_fl,
  833. struct file_lock *sys_fl)
  834. {
  835. /* FLOCK locks referring to the same filp do not conflict with
  836. * each other.
  837. */
  838. if (caller_fl->fl_file == sys_fl->fl_file)
  839. return false;
  840. if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
  841. return false;
  842. return locks_conflict(caller_fl, sys_fl);
  843. }
  844. void
  845. posix_test_lock(struct file *filp, struct file_lock *fl)
  846. {
  847. struct file_lock *cfl;
  848. struct file_lock_context *ctx;
  849. struct inode *inode = locks_inode(filp);
  850. ctx = smp_load_acquire(&inode->i_flctx);
  851. if (!ctx || list_empty_careful(&ctx->flc_posix)) {
  852. fl->fl_type = F_UNLCK;
  853. return;
  854. }
  855. spin_lock(&ctx->flc_lock);
  856. list_for_each_entry(cfl, &ctx->flc_posix, fl_list) {
  857. if (posix_locks_conflict(fl, cfl)) {
  858. locks_copy_conflock(fl, cfl);
  859. goto out;
  860. }
  861. }
  862. fl->fl_type = F_UNLCK;
  863. out:
  864. spin_unlock(&ctx->flc_lock);
  865. return;
  866. }
  867. EXPORT_SYMBOL(posix_test_lock);
  868. /*
  869. * Deadlock detection:
  870. *
  871. * We attempt to detect deadlocks that are due purely to posix file
  872. * locks.
  873. *
  874. * We assume that a task can be waiting for at most one lock at a time.
  875. * So for any acquired lock, the process holding that lock may be
  876. * waiting on at most one other lock. That lock in turns may be held by
  877. * someone waiting for at most one other lock. Given a requested lock
  878. * caller_fl which is about to wait for a conflicting lock block_fl, we
  879. * follow this chain of waiters to ensure we are not about to create a
  880. * cycle.
  881. *
  882. * Since we do this before we ever put a process to sleep on a lock, we
  883. * are ensured that there is never a cycle; that is what guarantees that
  884. * the while() loop in posix_locks_deadlock() eventually completes.
  885. *
  886. * Note: the above assumption may not be true when handling lock
  887. * requests from a broken NFS client. It may also fail in the presence
  888. * of tasks (such as posix threads) sharing the same open file table.
  889. * To handle those cases, we just bail out after a few iterations.
  890. *
  891. * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
  892. * Because the owner is not even nominally tied to a thread of
  893. * execution, the deadlock detection below can't reasonably work well. Just
  894. * skip it for those.
  895. *
  896. * In principle, we could do a more limited deadlock detection on FL_OFDLCK
  897. * locks that just checks for the case where two tasks are attempting to
  898. * upgrade from read to write locks on the same inode.
  899. */
  900. #define MAX_DEADLK_ITERATIONS 10
  901. /* Find a lock that the owner of the given block_fl is blocking on. */
  902. static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
  903. {
  904. struct file_lock *fl;
  905. hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
  906. if (posix_same_owner(fl, block_fl)) {
  907. while (fl->fl_blocker)
  908. fl = fl->fl_blocker;
  909. return fl;
  910. }
  911. }
  912. return NULL;
  913. }
  914. /* Must be called with the blocked_lock_lock held! */
  915. static int posix_locks_deadlock(struct file_lock *caller_fl,
  916. struct file_lock *block_fl)
  917. {
  918. int i = 0;
  919. lockdep_assert_held(&blocked_lock_lock);
  920. /*
  921. * This deadlock detector can't reasonably detect deadlocks with
  922. * FL_OFDLCK locks, since they aren't owned by a process, per-se.
  923. */
  924. if (IS_OFDLCK(caller_fl))
  925. return 0;
  926. while ((block_fl = what_owner_is_waiting_for(block_fl))) {
  927. if (i++ > MAX_DEADLK_ITERATIONS)
  928. return 0;
  929. if (posix_same_owner(caller_fl, block_fl))
  930. return 1;
  931. }
  932. return 0;
  933. }
  934. /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
  935. * after any leases, but before any posix locks.
  936. *
  937. * Note that if called with an FL_EXISTS argument, the caller may determine
  938. * whether or not a lock was successfully freed by testing the return
  939. * value for -ENOENT.
  940. */
  941. static int flock_lock_inode(struct inode *inode, struct file_lock *request)
  942. {
  943. struct file_lock *new_fl = NULL;
  944. struct file_lock *fl;
  945. struct file_lock_context *ctx;
  946. int error = 0;
  947. bool found = false;
  948. LIST_HEAD(dispose);
  949. ctx = locks_get_lock_context(inode, request->fl_type);
  950. if (!ctx) {
  951. if (request->fl_type != F_UNLCK)
  952. return -ENOMEM;
  953. return (request->fl_flags & FL_EXISTS) ? -ENOENT : 0;
  954. }
  955. if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
  956. new_fl = locks_alloc_lock();
  957. if (!new_fl)
  958. return -ENOMEM;
  959. }
  960. percpu_down_read(&file_rwsem);
  961. spin_lock(&ctx->flc_lock);
  962. if (request->fl_flags & FL_ACCESS)
  963. goto find_conflict;
  964. list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
  965. if (request->fl_file != fl->fl_file)
  966. continue;
  967. if (request->fl_type == fl->fl_type)
  968. goto out;
  969. found = true;
  970. locks_delete_lock_ctx(fl, &dispose);
  971. break;
  972. }
  973. if (request->fl_type == F_UNLCK) {
  974. if ((request->fl_flags & FL_EXISTS) && !found)
  975. error = -ENOENT;
  976. goto out;
  977. }
  978. find_conflict:
  979. list_for_each_entry(fl, &ctx->flc_flock, fl_list) {
  980. if (!flock_locks_conflict(request, fl))
  981. continue;
  982. error = -EAGAIN;
  983. if (!(request->fl_flags & FL_SLEEP))
  984. goto out;
  985. error = FILE_LOCK_DEFERRED;
  986. locks_insert_block(fl, request, flock_locks_conflict);
  987. goto out;
  988. }
  989. if (request->fl_flags & FL_ACCESS)
  990. goto out;
  991. locks_copy_lock(new_fl, request);
  992. locks_move_blocks(new_fl, request);
  993. locks_insert_lock_ctx(new_fl, &ctx->flc_flock);
  994. new_fl = NULL;
  995. error = 0;
  996. out:
  997. spin_unlock(&ctx->flc_lock);
  998. percpu_up_read(&file_rwsem);
  999. if (new_fl)
  1000. locks_free_lock(new_fl);
  1001. locks_dispose_list(&dispose);
  1002. trace_flock_lock_inode(inode, request, error);
  1003. return error;
  1004. }
  1005. static int posix_lock_inode(struct inode *inode, struct file_lock *request,
  1006. struct file_lock *conflock)
  1007. {
  1008. struct file_lock *fl, *tmp;
  1009. struct file_lock *new_fl = NULL;
  1010. struct file_lock *new_fl2 = NULL;
  1011. struct file_lock *left = NULL;
  1012. struct file_lock *right = NULL;
  1013. struct file_lock_context *ctx;
  1014. int error;
  1015. bool added = false;
  1016. LIST_HEAD(dispose);
  1017. ctx = locks_get_lock_context(inode, request->fl_type);
  1018. if (!ctx)
  1019. return (request->fl_type == F_UNLCK) ? 0 : -ENOMEM;
  1020. /*
  1021. * We may need two file_lock structures for this operation,
  1022. * so we get them in advance to avoid races.
  1023. *
  1024. * In some cases we can be sure, that no new locks will be needed
  1025. */
  1026. if (!(request->fl_flags & FL_ACCESS) &&
  1027. (request->fl_type != F_UNLCK ||
  1028. request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
  1029. new_fl = locks_alloc_lock();
  1030. new_fl2 = locks_alloc_lock();
  1031. }
  1032. percpu_down_read(&file_rwsem);
  1033. spin_lock(&ctx->flc_lock);
  1034. /*
  1035. * New lock request. Walk all POSIX locks and look for conflicts. If
  1036. * there are any, either return error or put the request on the
  1037. * blocker's list of waiters and the global blocked_hash.
  1038. */
  1039. if (request->fl_type != F_UNLCK) {
  1040. list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
  1041. if (!posix_locks_conflict(request, fl))
  1042. continue;
  1043. if (conflock)
  1044. locks_copy_conflock(conflock, fl);
  1045. error = -EAGAIN;
  1046. if (!(request->fl_flags & FL_SLEEP))
  1047. goto out;
  1048. /*
  1049. * Deadlock detection and insertion into the blocked
  1050. * locks list must be done while holding the same lock!
  1051. */
  1052. error = -EDEADLK;
  1053. spin_lock(&blocked_lock_lock);
  1054. /*
  1055. * Ensure that we don't find any locks blocked on this
  1056. * request during deadlock detection.
  1057. */
  1058. __locks_wake_up_blocks(request);
  1059. if (likely(!posix_locks_deadlock(request, fl))) {
  1060. error = FILE_LOCK_DEFERRED;
  1061. __locks_insert_block(fl, request,
  1062. posix_locks_conflict);
  1063. }
  1064. spin_unlock(&blocked_lock_lock);
  1065. goto out;
  1066. }
  1067. }
  1068. /* If we're just looking for a conflict, we're done. */
  1069. error = 0;
  1070. if (request->fl_flags & FL_ACCESS)
  1071. goto out;
  1072. /* Find the first old lock with the same owner as the new lock */
  1073. list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
  1074. if (posix_same_owner(request, fl))
  1075. break;
  1076. }
  1077. /* Process locks with this owner. */
  1078. list_for_each_entry_safe_from(fl, tmp, &ctx->flc_posix, fl_list) {
  1079. if (!posix_same_owner(request, fl))
  1080. break;
  1081. /* Detect adjacent or overlapping regions (if same lock type) */
  1082. if (request->fl_type == fl->fl_type) {
  1083. /* In all comparisons of start vs end, use
  1084. * "start - 1" rather than "end + 1". If end
  1085. * is OFFSET_MAX, end + 1 will become negative.
  1086. */
  1087. if (fl->fl_end < request->fl_start - 1)
  1088. continue;
  1089. /* If the next lock in the list has entirely bigger
  1090. * addresses than the new one, insert the lock here.
  1091. */
  1092. if (fl->fl_start - 1 > request->fl_end)
  1093. break;
  1094. /* If we come here, the new and old lock are of the
  1095. * same type and adjacent or overlapping. Make one
  1096. * lock yielding from the lower start address of both
  1097. * locks to the higher end address.
  1098. */
  1099. if (fl->fl_start > request->fl_start)
  1100. fl->fl_start = request->fl_start;
  1101. else
  1102. request->fl_start = fl->fl_start;
  1103. if (fl->fl_end < request->fl_end)
  1104. fl->fl_end = request->fl_end;
  1105. else
  1106. request->fl_end = fl->fl_end;
  1107. if (added) {
  1108. locks_delete_lock_ctx(fl, &dispose);
  1109. continue;
  1110. }
  1111. request = fl;
  1112. added = true;
  1113. } else {
  1114. /* Processing for different lock types is a bit
  1115. * more complex.
  1116. */
  1117. if (fl->fl_end < request->fl_start)
  1118. continue;
  1119. if (fl->fl_start > request->fl_end)
  1120. break;
  1121. if (request->fl_type == F_UNLCK)
  1122. added = true;
  1123. if (fl->fl_start < request->fl_start)
  1124. left = fl;
  1125. /* If the next lock in the list has a higher end
  1126. * address than the new one, insert the new one here.
  1127. */
  1128. if (fl->fl_end > request->fl_end) {
  1129. right = fl;
  1130. break;
  1131. }
  1132. if (fl->fl_start >= request->fl_start) {
  1133. /* The new lock completely replaces an old
  1134. * one (This may happen several times).
  1135. */
  1136. if (added) {
  1137. locks_delete_lock_ctx(fl, &dispose);
  1138. continue;
  1139. }
  1140. /*
  1141. * Replace the old lock with new_fl, and
  1142. * remove the old one. It's safe to do the
  1143. * insert here since we know that we won't be
  1144. * using new_fl later, and that the lock is
  1145. * just replacing an existing lock.
  1146. */
  1147. error = -ENOLCK;
  1148. if (!new_fl)
  1149. goto out;
  1150. locks_copy_lock(new_fl, request);
  1151. locks_move_blocks(new_fl, request);
  1152. request = new_fl;
  1153. new_fl = NULL;
  1154. locks_insert_lock_ctx(request, &fl->fl_list);
  1155. locks_delete_lock_ctx(fl, &dispose);
  1156. added = true;
  1157. }
  1158. }
  1159. }
  1160. /*
  1161. * The above code only modifies existing locks in case of merging or
  1162. * replacing. If new lock(s) need to be inserted all modifications are
  1163. * done below this, so it's safe yet to bail out.
  1164. */
  1165. error = -ENOLCK; /* "no luck" */
  1166. if (right && left == right && !new_fl2)
  1167. goto out;
  1168. error = 0;
  1169. if (!added) {
  1170. if (request->fl_type == F_UNLCK) {
  1171. if (request->fl_flags & FL_EXISTS)
  1172. error = -ENOENT;
  1173. goto out;
  1174. }
  1175. if (!new_fl) {
  1176. error = -ENOLCK;
  1177. goto out;
  1178. }
  1179. locks_copy_lock(new_fl, request);
  1180. locks_move_blocks(new_fl, request);
  1181. locks_insert_lock_ctx(new_fl, &fl->fl_list);
  1182. fl = new_fl;
  1183. new_fl = NULL;
  1184. }
  1185. if (right) {
  1186. if (left == right) {
  1187. /* The new lock breaks the old one in two pieces,
  1188. * so we have to use the second new lock.
  1189. */
  1190. left = new_fl2;
  1191. new_fl2 = NULL;
  1192. locks_copy_lock(left, right);
  1193. locks_insert_lock_ctx(left, &fl->fl_list);
  1194. }
  1195. right->fl_start = request->fl_end + 1;
  1196. locks_wake_up_blocks(right);
  1197. }
  1198. if (left) {
  1199. left->fl_end = request->fl_start - 1;
  1200. locks_wake_up_blocks(left);
  1201. }
  1202. out:
  1203. spin_unlock(&ctx->flc_lock);
  1204. percpu_up_read(&file_rwsem);
  1205. /*
  1206. * Free any unused locks.
  1207. */
  1208. if (new_fl)
  1209. locks_free_lock(new_fl);
  1210. if (new_fl2)
  1211. locks_free_lock(new_fl2);
  1212. locks_dispose_list(&dispose);
  1213. trace_posix_lock_inode(inode, request, error);
  1214. return error;
  1215. }
  1216. /**
  1217. * posix_lock_file - Apply a POSIX-style lock to a file
  1218. * @filp: The file to apply the lock to
  1219. * @fl: The lock to be applied
  1220. * @conflock: Place to return a copy of the conflicting lock, if found.
  1221. *
  1222. * Add a POSIX style lock to a file.
  1223. * We merge adjacent & overlapping locks whenever possible.
  1224. * POSIX locks are sorted by owner task, then by starting address
  1225. *
  1226. * Note that if called with an FL_EXISTS argument, the caller may determine
  1227. * whether or not a lock was successfully freed by testing the return
  1228. * value for -ENOENT.
  1229. */
  1230. int posix_lock_file(struct file *filp, struct file_lock *fl,
  1231. struct file_lock *conflock)
  1232. {
  1233. return posix_lock_inode(locks_inode(filp), fl, conflock);
  1234. }
  1235. EXPORT_SYMBOL(posix_lock_file);
  1236. /**
  1237. * posix_lock_inode_wait - Apply a POSIX-style lock to a file
  1238. * @inode: inode of file to which lock request should be applied
  1239. * @fl: The lock to be applied
  1240. *
  1241. * Apply a POSIX style lock request to an inode.
  1242. */
  1243. static int posix_lock_inode_wait(struct inode *inode, struct file_lock *fl)
  1244. {
  1245. int error;
  1246. might_sleep ();
  1247. for (;;) {
  1248. error = posix_lock_inode(inode, fl, NULL);
  1249. if (error != FILE_LOCK_DEFERRED)
  1250. break;
  1251. error = wait_event_interruptible(fl->fl_wait,
  1252. list_empty(&fl->fl_blocked_member));
  1253. if (error)
  1254. break;
  1255. }
  1256. locks_delete_block(fl);
  1257. return error;
  1258. }
  1259. #ifdef CONFIG_MANDATORY_FILE_LOCKING
  1260. /**
  1261. * locks_mandatory_locked - Check for an active lock
  1262. * @file: the file to check
  1263. *
  1264. * Searches the inode's list of locks to find any POSIX locks which conflict.
  1265. * This function is called from locks_verify_locked() only.
  1266. */
  1267. int locks_mandatory_locked(struct file *file)
  1268. {
  1269. int ret;
  1270. struct inode *inode = locks_inode(file);
  1271. struct file_lock_context *ctx;
  1272. struct file_lock *fl;
  1273. ctx = smp_load_acquire(&inode->i_flctx);
  1274. if (!ctx || list_empty_careful(&ctx->flc_posix))
  1275. return 0;
  1276. /*
  1277. * Search the lock list for this inode for any POSIX locks.
  1278. */
  1279. spin_lock(&ctx->flc_lock);
  1280. ret = 0;
  1281. list_for_each_entry(fl, &ctx->flc_posix, fl_list) {
  1282. if (fl->fl_owner != current->files &&
  1283. fl->fl_owner != file) {
  1284. ret = -EAGAIN;
  1285. break;
  1286. }
  1287. }
  1288. spin_unlock(&ctx->flc_lock);
  1289. return ret;
  1290. }
  1291. /**
  1292. * locks_mandatory_area - Check for a conflicting lock
  1293. * @inode: the file to check
  1294. * @filp: how the file was opened (if it was)
  1295. * @start: first byte in the file to check
  1296. * @end: lastbyte in the file to check
  1297. * @type: %F_WRLCK for a write lock, else %F_RDLCK
  1298. *
  1299. * Searches the inode's list of locks to find any POSIX locks which conflict.
  1300. */
  1301. int locks_mandatory_area(struct inode *inode, struct file *filp, loff_t start,
  1302. loff_t end, unsigned char type)
  1303. {
  1304. struct file_lock fl;
  1305. int error;
  1306. bool sleep = false;
  1307. locks_init_lock(&fl);
  1308. fl.fl_pid = current->tgid;
  1309. fl.fl_file = filp;
  1310. fl.fl_flags = FL_POSIX | FL_ACCESS;
  1311. if (filp && !(filp->f_flags & O_NONBLOCK))
  1312. sleep = true;
  1313. fl.fl_type = type;
  1314. fl.fl_start = start;
  1315. fl.fl_end = end;
  1316. for (;;) {
  1317. if (filp) {
  1318. fl.fl_owner = filp;
  1319. fl.fl_flags &= ~FL_SLEEP;
  1320. error = posix_lock_inode(inode, &fl, NULL);
  1321. if (!error)
  1322. break;
  1323. }
  1324. if (sleep)
  1325. fl.fl_flags |= FL_SLEEP;
  1326. fl.fl_owner = current->files;
  1327. error = posix_lock_inode(inode, &fl, NULL);
  1328. if (error != FILE_LOCK_DEFERRED)
  1329. break;
  1330. error = wait_event_interruptible(fl.fl_wait,
  1331. list_empty(&fl.fl_blocked_member));
  1332. if (!error) {
  1333. /*
  1334. * If we've been sleeping someone might have
  1335. * changed the permissions behind our back.
  1336. */
  1337. if (__mandatory_lock(inode))
  1338. continue;
  1339. }
  1340. break;
  1341. }
  1342. locks_delete_block(&fl);
  1343. return error;
  1344. }
  1345. EXPORT_SYMBOL(locks_mandatory_area);
  1346. #endif /* CONFIG_MANDATORY_FILE_LOCKING */
  1347. static void lease_clear_pending(struct file_lock *fl, int arg)
  1348. {
  1349. switch (arg) {
  1350. case F_UNLCK:
  1351. fl->fl_flags &= ~FL_UNLOCK_PENDING;
  1352. fallthrough;
  1353. case F_RDLCK:
  1354. fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
  1355. }
  1356. }
  1357. /* We already had a lease on this file; just change its type */
  1358. int lease_modify(struct file_lock *fl, int arg, struct list_head *dispose)
  1359. {
  1360. int error = assign_type(fl, arg);
  1361. if (error)
  1362. return error;
  1363. lease_clear_pending(fl, arg);
  1364. locks_wake_up_blocks(fl);
  1365. if (arg == F_UNLCK) {
  1366. struct file *filp = fl->fl_file;
  1367. f_delown(filp);
  1368. filp->f_owner.signum = 0;
  1369. fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
  1370. if (fl->fl_fasync != NULL) {
  1371. printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
  1372. fl->fl_fasync = NULL;
  1373. }
  1374. locks_delete_lock_ctx(fl, dispose);
  1375. }
  1376. return 0;
  1377. }
  1378. EXPORT_SYMBOL(lease_modify);
  1379. static bool past_time(unsigned long then)
  1380. {
  1381. if (!then)
  1382. /* 0 is a special value meaning "this never expires": */
  1383. return false;
  1384. return time_after(jiffies, then);
  1385. }
  1386. static void time_out_leases(struct inode *inode, struct list_head *dispose)
  1387. {
  1388. struct file_lock_context *ctx = inode->i_flctx;
  1389. struct file_lock *fl, *tmp;
  1390. lockdep_assert_held(&ctx->flc_lock);
  1391. list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
  1392. trace_time_out_leases(inode, fl);
  1393. if (past_time(fl->fl_downgrade_time))
  1394. lease_modify(fl, F_RDLCK, dispose);
  1395. if (past_time(fl->fl_break_time))
  1396. lease_modify(fl, F_UNLCK, dispose);
  1397. }
  1398. }
  1399. static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
  1400. {
  1401. bool rc;
  1402. if (lease->fl_lmops->lm_breaker_owns_lease
  1403. && lease->fl_lmops->lm_breaker_owns_lease(lease))
  1404. return false;
  1405. if ((breaker->fl_flags & FL_LAYOUT) != (lease->fl_flags & FL_LAYOUT)) {
  1406. rc = false;
  1407. goto trace;
  1408. }
  1409. if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE)) {
  1410. rc = false;
  1411. goto trace;
  1412. }
  1413. rc = locks_conflict(breaker, lease);
  1414. trace:
  1415. trace_leases_conflict(rc, lease, breaker);
  1416. return rc;
  1417. }
  1418. static bool
  1419. any_leases_conflict(struct inode *inode, struct file_lock *breaker)
  1420. {
  1421. struct file_lock_context *ctx = inode->i_flctx;
  1422. struct file_lock *fl;
  1423. lockdep_assert_held(&ctx->flc_lock);
  1424. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  1425. if (leases_conflict(fl, breaker))
  1426. return true;
  1427. }
  1428. return false;
  1429. }
  1430. /**
  1431. * __break_lease - revoke all outstanding leases on file
  1432. * @inode: the inode of the file to return
  1433. * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
  1434. * break all leases
  1435. * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
  1436. * only delegations
  1437. *
  1438. * break_lease (inlined for speed) has checked there already is at least
  1439. * some kind of lock (maybe a lease) on this file. Leases are broken on
  1440. * a call to open() or truncate(). This function can sleep unless you
  1441. * specified %O_NONBLOCK to your open().
  1442. */
  1443. int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
  1444. {
  1445. int error = 0;
  1446. struct file_lock_context *ctx;
  1447. struct file_lock *new_fl, *fl, *tmp;
  1448. unsigned long break_time;
  1449. int want_write = (mode & O_ACCMODE) != O_RDONLY;
  1450. LIST_HEAD(dispose);
  1451. new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
  1452. if (IS_ERR(new_fl))
  1453. return PTR_ERR(new_fl);
  1454. new_fl->fl_flags = type;
  1455. /* typically we will check that ctx is non-NULL before calling */
  1456. ctx = smp_load_acquire(&inode->i_flctx);
  1457. if (!ctx) {
  1458. WARN_ON_ONCE(1);
  1459. goto free_lock;
  1460. }
  1461. percpu_down_read(&file_rwsem);
  1462. spin_lock(&ctx->flc_lock);
  1463. time_out_leases(inode, &dispose);
  1464. if (!any_leases_conflict(inode, new_fl))
  1465. goto out;
  1466. break_time = 0;
  1467. if (lease_break_time > 0) {
  1468. break_time = jiffies + lease_break_time * HZ;
  1469. if (break_time == 0)
  1470. break_time++; /* so that 0 means no break time */
  1471. }
  1472. list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list) {
  1473. if (!leases_conflict(fl, new_fl))
  1474. continue;
  1475. if (want_write) {
  1476. if (fl->fl_flags & FL_UNLOCK_PENDING)
  1477. continue;
  1478. fl->fl_flags |= FL_UNLOCK_PENDING;
  1479. fl->fl_break_time = break_time;
  1480. } else {
  1481. if (lease_breaking(fl))
  1482. continue;
  1483. fl->fl_flags |= FL_DOWNGRADE_PENDING;
  1484. fl->fl_downgrade_time = break_time;
  1485. }
  1486. if (fl->fl_lmops->lm_break(fl))
  1487. locks_delete_lock_ctx(fl, &dispose);
  1488. }
  1489. if (list_empty(&ctx->flc_lease))
  1490. goto out;
  1491. if (mode & O_NONBLOCK) {
  1492. trace_break_lease_noblock(inode, new_fl);
  1493. error = -EWOULDBLOCK;
  1494. goto out;
  1495. }
  1496. restart:
  1497. fl = list_first_entry(&ctx->flc_lease, struct file_lock, fl_list);
  1498. break_time = fl->fl_break_time;
  1499. if (break_time != 0)
  1500. break_time -= jiffies;
  1501. if (break_time == 0)
  1502. break_time++;
  1503. locks_insert_block(fl, new_fl, leases_conflict);
  1504. trace_break_lease_block(inode, new_fl);
  1505. spin_unlock(&ctx->flc_lock);
  1506. percpu_up_read(&file_rwsem);
  1507. locks_dispose_list(&dispose);
  1508. error = wait_event_interruptible_timeout(new_fl->fl_wait,
  1509. list_empty(&new_fl->fl_blocked_member),
  1510. break_time);
  1511. percpu_down_read(&file_rwsem);
  1512. spin_lock(&ctx->flc_lock);
  1513. trace_break_lease_unblock(inode, new_fl);
  1514. locks_delete_block(new_fl);
  1515. if (error >= 0) {
  1516. /*
  1517. * Wait for the next conflicting lease that has not been
  1518. * broken yet
  1519. */
  1520. if (error == 0)
  1521. time_out_leases(inode, &dispose);
  1522. if (any_leases_conflict(inode, new_fl))
  1523. goto restart;
  1524. error = 0;
  1525. }
  1526. out:
  1527. spin_unlock(&ctx->flc_lock);
  1528. percpu_up_read(&file_rwsem);
  1529. locks_dispose_list(&dispose);
  1530. free_lock:
  1531. locks_free_lock(new_fl);
  1532. return error;
  1533. }
  1534. EXPORT_SYMBOL(__break_lease);
  1535. /**
  1536. * lease_get_mtime - update modified time of an inode with exclusive lease
  1537. * @inode: the inode
  1538. * @time: pointer to a timespec which contains the last modified time
  1539. *
  1540. * This is to force NFS clients to flush their caches for files with
  1541. * exclusive leases. The justification is that if someone has an
  1542. * exclusive lease, then they could be modifying it.
  1543. */
  1544. void lease_get_mtime(struct inode *inode, struct timespec64 *time)
  1545. {
  1546. bool has_lease = false;
  1547. struct file_lock_context *ctx;
  1548. struct file_lock *fl;
  1549. ctx = smp_load_acquire(&inode->i_flctx);
  1550. if (ctx && !list_empty_careful(&ctx->flc_lease)) {
  1551. spin_lock(&ctx->flc_lock);
  1552. fl = list_first_entry_or_null(&ctx->flc_lease,
  1553. struct file_lock, fl_list);
  1554. if (fl && (fl->fl_type == F_WRLCK))
  1555. has_lease = true;
  1556. spin_unlock(&ctx->flc_lock);
  1557. }
  1558. if (has_lease)
  1559. *time = current_time(inode);
  1560. }
  1561. EXPORT_SYMBOL(lease_get_mtime);
  1562. /**
  1563. * fcntl_getlease - Enquire what lease is currently active
  1564. * @filp: the file
  1565. *
  1566. * The value returned by this function will be one of
  1567. * (if no lease break is pending):
  1568. *
  1569. * %F_RDLCK to indicate a shared lease is held.
  1570. *
  1571. * %F_WRLCK to indicate an exclusive lease is held.
  1572. *
  1573. * %F_UNLCK to indicate no lease is held.
  1574. *
  1575. * (if a lease break is pending):
  1576. *
  1577. * %F_RDLCK to indicate an exclusive lease needs to be
  1578. * changed to a shared lease (or removed).
  1579. *
  1580. * %F_UNLCK to indicate the lease needs to be removed.
  1581. *
  1582. * XXX: sfr & willy disagree over whether F_INPROGRESS
  1583. * should be returned to userspace.
  1584. */
  1585. int fcntl_getlease(struct file *filp)
  1586. {
  1587. struct file_lock *fl;
  1588. struct inode *inode = locks_inode(filp);
  1589. struct file_lock_context *ctx;
  1590. int type = F_UNLCK;
  1591. LIST_HEAD(dispose);
  1592. ctx = smp_load_acquire(&inode->i_flctx);
  1593. if (ctx && !list_empty_careful(&ctx->flc_lease)) {
  1594. percpu_down_read(&file_rwsem);
  1595. spin_lock(&ctx->flc_lock);
  1596. time_out_leases(inode, &dispose);
  1597. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  1598. if (fl->fl_file != filp)
  1599. continue;
  1600. type = target_leasetype(fl);
  1601. break;
  1602. }
  1603. spin_unlock(&ctx->flc_lock);
  1604. percpu_up_read(&file_rwsem);
  1605. locks_dispose_list(&dispose);
  1606. }
  1607. return type;
  1608. }
  1609. /**
  1610. * check_conflicting_open - see if the given file points to an inode that has
  1611. * an existing open that would conflict with the
  1612. * desired lease.
  1613. * @filp: file to check
  1614. * @arg: type of lease that we're trying to acquire
  1615. * @flags: current lock flags
  1616. *
  1617. * Check to see if there's an existing open fd on this file that would
  1618. * conflict with the lease we're trying to set.
  1619. */
  1620. static int
  1621. check_conflicting_open(struct file *filp, const long arg, int flags)
  1622. {
  1623. struct inode *inode = locks_inode(filp);
  1624. int self_wcount = 0, self_rcount = 0;
  1625. if (flags & FL_LAYOUT)
  1626. return 0;
  1627. if (arg == F_RDLCK)
  1628. return inode_is_open_for_write(inode) ? -EAGAIN : 0;
  1629. else if (arg != F_WRLCK)
  1630. return 0;
  1631. /*
  1632. * Make sure that only read/write count is from lease requestor.
  1633. * Note that this will result in denying write leases when i_writecount
  1634. * is negative, which is what we want. (We shouldn't grant write leases
  1635. * on files open for execution.)
  1636. */
  1637. if (filp->f_mode & FMODE_WRITE)
  1638. self_wcount = 1;
  1639. else if (filp->f_mode & FMODE_READ)
  1640. self_rcount = 1;
  1641. if (atomic_read(&inode->i_writecount) != self_wcount ||
  1642. atomic_read(&inode->i_readcount) != self_rcount)
  1643. return -EAGAIN;
  1644. return 0;
  1645. }
  1646. static int
  1647. generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
  1648. {
  1649. struct file_lock *fl, *my_fl = NULL, *lease;
  1650. struct inode *inode = locks_inode(filp);
  1651. struct file_lock_context *ctx;
  1652. bool is_deleg = (*flp)->fl_flags & FL_DELEG;
  1653. int error;
  1654. LIST_HEAD(dispose);
  1655. lease = *flp;
  1656. trace_generic_add_lease(inode, lease);
  1657. /* Note that arg is never F_UNLCK here */
  1658. ctx = locks_get_lock_context(inode, arg);
  1659. if (!ctx)
  1660. return -ENOMEM;
  1661. /*
  1662. * In the delegation case we need mutual exclusion with
  1663. * a number of operations that take the i_mutex. We trylock
  1664. * because delegations are an optional optimization, and if
  1665. * there's some chance of a conflict--we'd rather not
  1666. * bother, maybe that's a sign this just isn't a good file to
  1667. * hand out a delegation on.
  1668. */
  1669. if (is_deleg && !inode_trylock(inode))
  1670. return -EAGAIN;
  1671. if (is_deleg && arg == F_WRLCK) {
  1672. /* Write delegations are not currently supported: */
  1673. inode_unlock(inode);
  1674. WARN_ON_ONCE(1);
  1675. return -EINVAL;
  1676. }
  1677. percpu_down_read(&file_rwsem);
  1678. spin_lock(&ctx->flc_lock);
  1679. time_out_leases(inode, &dispose);
  1680. error = check_conflicting_open(filp, arg, lease->fl_flags);
  1681. if (error)
  1682. goto out;
  1683. /*
  1684. * At this point, we know that if there is an exclusive
  1685. * lease on this file, then we hold it on this filp
  1686. * (otherwise our open of this file would have blocked).
  1687. * And if we are trying to acquire an exclusive lease,
  1688. * then the file is not open by anyone (including us)
  1689. * except for this filp.
  1690. */
  1691. error = -EAGAIN;
  1692. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  1693. if (fl->fl_file == filp &&
  1694. fl->fl_owner == lease->fl_owner) {
  1695. my_fl = fl;
  1696. continue;
  1697. }
  1698. /*
  1699. * No exclusive leases if someone else has a lease on
  1700. * this file:
  1701. */
  1702. if (arg == F_WRLCK)
  1703. goto out;
  1704. /*
  1705. * Modifying our existing lease is OK, but no getting a
  1706. * new lease if someone else is opening for write:
  1707. */
  1708. if (fl->fl_flags & FL_UNLOCK_PENDING)
  1709. goto out;
  1710. }
  1711. if (my_fl != NULL) {
  1712. lease = my_fl;
  1713. error = lease->fl_lmops->lm_change(lease, arg, &dispose);
  1714. if (error)
  1715. goto out;
  1716. goto out_setup;
  1717. }
  1718. error = -EINVAL;
  1719. if (!leases_enable)
  1720. goto out;
  1721. locks_insert_lock_ctx(lease, &ctx->flc_lease);
  1722. /*
  1723. * The check in break_lease() is lockless. It's possible for another
  1724. * open to race in after we did the earlier check for a conflicting
  1725. * open but before the lease was inserted. Check again for a
  1726. * conflicting open and cancel the lease if there is one.
  1727. *
  1728. * We also add a barrier here to ensure that the insertion of the lock
  1729. * precedes these checks.
  1730. */
  1731. smp_mb();
  1732. error = check_conflicting_open(filp, arg, lease->fl_flags);
  1733. if (error) {
  1734. locks_unlink_lock_ctx(lease);
  1735. goto out;
  1736. }
  1737. out_setup:
  1738. if (lease->fl_lmops->lm_setup)
  1739. lease->fl_lmops->lm_setup(lease, priv);
  1740. out:
  1741. spin_unlock(&ctx->flc_lock);
  1742. percpu_up_read(&file_rwsem);
  1743. locks_dispose_list(&dispose);
  1744. if (is_deleg)
  1745. inode_unlock(inode);
  1746. if (!error && !my_fl)
  1747. *flp = NULL;
  1748. return error;
  1749. }
  1750. static int generic_delete_lease(struct file *filp, void *owner)
  1751. {
  1752. int error = -EAGAIN;
  1753. struct file_lock *fl, *victim = NULL;
  1754. struct inode *inode = locks_inode(filp);
  1755. struct file_lock_context *ctx;
  1756. LIST_HEAD(dispose);
  1757. ctx = smp_load_acquire(&inode->i_flctx);
  1758. if (!ctx) {
  1759. trace_generic_delete_lease(inode, NULL);
  1760. return error;
  1761. }
  1762. percpu_down_read(&file_rwsem);
  1763. spin_lock(&ctx->flc_lock);
  1764. list_for_each_entry(fl, &ctx->flc_lease, fl_list) {
  1765. if (fl->fl_file == filp &&
  1766. fl->fl_owner == owner) {
  1767. victim = fl;
  1768. break;
  1769. }
  1770. }
  1771. trace_generic_delete_lease(inode, victim);
  1772. if (victim)
  1773. error = fl->fl_lmops->lm_change(victim, F_UNLCK, &dispose);
  1774. spin_unlock(&ctx->flc_lock);
  1775. percpu_up_read(&file_rwsem);
  1776. locks_dispose_list(&dispose);
  1777. return error;
  1778. }
  1779. /**
  1780. * generic_setlease - sets a lease on an open file
  1781. * @filp: file pointer
  1782. * @arg: type of lease to obtain
  1783. * @flp: input - file_lock to use, output - file_lock inserted
  1784. * @priv: private data for lm_setup (may be NULL if lm_setup
  1785. * doesn't require it)
  1786. *
  1787. * The (input) flp->fl_lmops->lm_break function is required
  1788. * by break_lease().
  1789. */
  1790. int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
  1791. void **priv)
  1792. {
  1793. struct inode *inode = locks_inode(filp);
  1794. int error;
  1795. if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
  1796. return -EACCES;
  1797. if (!S_ISREG(inode->i_mode))
  1798. return -EINVAL;
  1799. error = security_file_lock(filp, arg);
  1800. if (error)
  1801. return error;
  1802. switch (arg) {
  1803. case F_UNLCK:
  1804. return generic_delete_lease(filp, *priv);
  1805. case F_RDLCK:
  1806. case F_WRLCK:
  1807. if (!(*flp)->fl_lmops->lm_break) {
  1808. WARN_ON_ONCE(1);
  1809. return -ENOLCK;
  1810. }
  1811. return generic_add_lease(filp, arg, flp, priv);
  1812. default:
  1813. return -EINVAL;
  1814. }
  1815. }
  1816. EXPORT_SYMBOL(generic_setlease);
  1817. #if IS_ENABLED(CONFIG_SRCU)
  1818. /*
  1819. * Kernel subsystems can register to be notified on any attempt to set
  1820. * a new lease with the lease_notifier_chain. This is used by (e.g.) nfsd
  1821. * to close files that it may have cached when there is an attempt to set a
  1822. * conflicting lease.
  1823. */
  1824. static struct srcu_notifier_head lease_notifier_chain;
  1825. static inline void
  1826. lease_notifier_chain_init(void)
  1827. {
  1828. srcu_init_notifier_head(&lease_notifier_chain);
  1829. }
  1830. static inline void
  1831. setlease_notifier(long arg, struct file_lock *lease)
  1832. {
  1833. if (arg != F_UNLCK)
  1834. srcu_notifier_call_chain(&lease_notifier_chain, arg, lease);
  1835. }
  1836. int lease_register_notifier(struct notifier_block *nb)
  1837. {
  1838. return srcu_notifier_chain_register(&lease_notifier_chain, nb);
  1839. }
  1840. EXPORT_SYMBOL_GPL(lease_register_notifier);
  1841. void lease_unregister_notifier(struct notifier_block *nb)
  1842. {
  1843. srcu_notifier_chain_unregister(&lease_notifier_chain, nb);
  1844. }
  1845. EXPORT_SYMBOL_GPL(lease_unregister_notifier);
  1846. #else /* !IS_ENABLED(CONFIG_SRCU) */
  1847. static inline void
  1848. lease_notifier_chain_init(void)
  1849. {
  1850. }
  1851. static inline void
  1852. setlease_notifier(long arg, struct file_lock *lease)
  1853. {
  1854. }
  1855. int lease_register_notifier(struct notifier_block *nb)
  1856. {
  1857. return 0;
  1858. }
  1859. EXPORT_SYMBOL_GPL(lease_register_notifier);
  1860. void lease_unregister_notifier(struct notifier_block *nb)
  1861. {
  1862. }
  1863. EXPORT_SYMBOL_GPL(lease_unregister_notifier);
  1864. #endif /* IS_ENABLED(CONFIG_SRCU) */
  1865. /**
  1866. * vfs_setlease - sets a lease on an open file
  1867. * @filp: file pointer
  1868. * @arg: type of lease to obtain
  1869. * @lease: file_lock to use when adding a lease
  1870. * @priv: private info for lm_setup when adding a lease (may be
  1871. * NULL if lm_setup doesn't require it)
  1872. *
  1873. * Call this to establish a lease on the file. The "lease" argument is not
  1874. * used for F_UNLCK requests and may be NULL. For commands that set or alter
  1875. * an existing lease, the ``(*lease)->fl_lmops->lm_break`` operation must be
  1876. * set; if not, this function will return -ENOLCK (and generate a scary-looking
  1877. * stack trace).
  1878. *
  1879. * The "priv" pointer is passed directly to the lm_setup function as-is. It
  1880. * may be NULL if the lm_setup operation doesn't require it.
  1881. */
  1882. int
  1883. vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
  1884. {
  1885. if (lease)
  1886. setlease_notifier(arg, *lease);
  1887. if (filp->f_op->setlease)
  1888. return filp->f_op->setlease(filp, arg, lease, priv);
  1889. else
  1890. return generic_setlease(filp, arg, lease, priv);
  1891. }
  1892. EXPORT_SYMBOL_GPL(vfs_setlease);
  1893. static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
  1894. {
  1895. struct file_lock *fl;
  1896. struct fasync_struct *new;
  1897. int error;
  1898. fl = lease_alloc(filp, arg);
  1899. if (IS_ERR(fl))
  1900. return PTR_ERR(fl);
  1901. new = fasync_alloc();
  1902. if (!new) {
  1903. locks_free_lock(fl);
  1904. return -ENOMEM;
  1905. }
  1906. new->fa_fd = fd;
  1907. error = vfs_setlease(filp, arg, &fl, (void **)&new);
  1908. if (fl)
  1909. locks_free_lock(fl);
  1910. if (new)
  1911. fasync_free(new);
  1912. return error;
  1913. }
  1914. /**
  1915. * fcntl_setlease - sets a lease on an open file
  1916. * @fd: open file descriptor
  1917. * @filp: file pointer
  1918. * @arg: type of lease to obtain
  1919. *
  1920. * Call this fcntl to establish a lease on the file.
  1921. * Note that you also need to call %F_SETSIG to
  1922. * receive a signal when the lease is broken.
  1923. */
  1924. int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
  1925. {
  1926. if (arg == F_UNLCK)
  1927. return vfs_setlease(filp, F_UNLCK, NULL, (void **)&filp);
  1928. return do_fcntl_add_lease(fd, filp, arg);
  1929. }
  1930. /**
  1931. * flock_lock_inode_wait - Apply a FLOCK-style lock to a file
  1932. * @inode: inode of the file to apply to
  1933. * @fl: The lock to be applied
  1934. *
  1935. * Apply a FLOCK style lock request to an inode.
  1936. */
  1937. static int flock_lock_inode_wait(struct inode *inode, struct file_lock *fl)
  1938. {
  1939. int error;
  1940. might_sleep();
  1941. for (;;) {
  1942. error = flock_lock_inode(inode, fl);
  1943. if (error != FILE_LOCK_DEFERRED)
  1944. break;
  1945. error = wait_event_interruptible(fl->fl_wait,
  1946. list_empty(&fl->fl_blocked_member));
  1947. if (error)
  1948. break;
  1949. }
  1950. locks_delete_block(fl);
  1951. return error;
  1952. }
  1953. /**
  1954. * locks_lock_inode_wait - Apply a lock to an inode
  1955. * @inode: inode of the file to apply to
  1956. * @fl: The lock to be applied
  1957. *
  1958. * Apply a POSIX or FLOCK style lock request to an inode.
  1959. */
  1960. int locks_lock_inode_wait(struct inode *inode, struct file_lock *fl)
  1961. {
  1962. int res = 0;
  1963. switch (fl->fl_flags & (FL_POSIX|FL_FLOCK)) {
  1964. case FL_POSIX:
  1965. res = posix_lock_inode_wait(inode, fl);
  1966. break;
  1967. case FL_FLOCK:
  1968. res = flock_lock_inode_wait(inode, fl);
  1969. break;
  1970. default:
  1971. BUG();
  1972. }
  1973. return res;
  1974. }
  1975. EXPORT_SYMBOL(locks_lock_inode_wait);
  1976. /**
  1977. * sys_flock: - flock() system call.
  1978. * @fd: the file descriptor to lock.
  1979. * @cmd: the type of lock to apply.
  1980. *
  1981. * Apply a %FL_FLOCK style lock to an open file descriptor.
  1982. * The @cmd can be one of:
  1983. *
  1984. * - %LOCK_SH -- a shared lock.
  1985. * - %LOCK_EX -- an exclusive lock.
  1986. * - %LOCK_UN -- remove an existing lock.
  1987. * - %LOCK_MAND -- a 'mandatory' flock.
  1988. * This exists to emulate Windows Share Modes.
  1989. *
  1990. * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
  1991. * processes read and write access respectively.
  1992. */
  1993. SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
  1994. {
  1995. struct fd f = fdget(fd);
  1996. struct file_lock *lock;
  1997. int can_sleep, unlock;
  1998. int error;
  1999. error = -EBADF;
  2000. if (!f.file)
  2001. goto out;
  2002. can_sleep = !(cmd & LOCK_NB);
  2003. cmd &= ~LOCK_NB;
  2004. unlock = (cmd == LOCK_UN);
  2005. if (!unlock && !(cmd & LOCK_MAND) &&
  2006. !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
  2007. goto out_putf;
  2008. lock = flock_make_lock(f.file, cmd, NULL);
  2009. if (IS_ERR(lock)) {
  2010. error = PTR_ERR(lock);
  2011. goto out_putf;
  2012. }
  2013. if (can_sleep)
  2014. lock->fl_flags |= FL_SLEEP;
  2015. error = security_file_lock(f.file, lock->fl_type);
  2016. if (error)
  2017. goto out_free;
  2018. if (f.file->f_op->flock)
  2019. error = f.file->f_op->flock(f.file,
  2020. (can_sleep) ? F_SETLKW : F_SETLK,
  2021. lock);
  2022. else
  2023. error = locks_lock_file_wait(f.file, lock);
  2024. out_free:
  2025. locks_free_lock(lock);
  2026. out_putf:
  2027. fdput(f);
  2028. out:
  2029. return error;
  2030. }
  2031. /**
  2032. * vfs_test_lock - test file byte range lock
  2033. * @filp: The file to test lock for
  2034. * @fl: The lock to test; also used to hold result
  2035. *
  2036. * Returns -ERRNO on failure. Indicates presence of conflicting lock by
  2037. * setting conf->fl_type to something other than F_UNLCK.
  2038. */
  2039. int vfs_test_lock(struct file *filp, struct file_lock *fl)
  2040. {
  2041. if (filp->f_op->lock)
  2042. return filp->f_op->lock(filp, F_GETLK, fl);
  2043. posix_test_lock(filp, fl);
  2044. return 0;
  2045. }
  2046. EXPORT_SYMBOL_GPL(vfs_test_lock);
  2047. /**
  2048. * locks_translate_pid - translate a file_lock's fl_pid number into a namespace
  2049. * @fl: The file_lock who's fl_pid should be translated
  2050. * @ns: The namespace into which the pid should be translated
  2051. *
  2052. * Used to tranlate a fl_pid into a namespace virtual pid number
  2053. */
  2054. static pid_t locks_translate_pid(struct file_lock *fl, struct pid_namespace *ns)
  2055. {
  2056. pid_t vnr;
  2057. struct pid *pid;
  2058. if (IS_OFDLCK(fl))
  2059. return -1;
  2060. if (IS_REMOTELCK(fl))
  2061. return fl->fl_pid;
  2062. /*
  2063. * If the flock owner process is dead and its pid has been already
  2064. * freed, the translation below won't work, but we still want to show
  2065. * flock owner pid number in init pidns.
  2066. */
  2067. if (ns == &init_pid_ns)
  2068. return (pid_t)fl->fl_pid;
  2069. rcu_read_lock();
  2070. pid = find_pid_ns(fl->fl_pid, &init_pid_ns);
  2071. vnr = pid_nr_ns(pid, ns);
  2072. rcu_read_unlock();
  2073. return vnr;
  2074. }
  2075. static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
  2076. {
  2077. flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
  2078. #if BITS_PER_LONG == 32
  2079. /*
  2080. * Make sure we can represent the posix lock via
  2081. * legacy 32bit flock.
  2082. */
  2083. if (fl->fl_start > OFFT_OFFSET_MAX)
  2084. return -EOVERFLOW;
  2085. if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
  2086. return -EOVERFLOW;
  2087. #endif
  2088. flock->l_start = fl->fl_start;
  2089. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  2090. fl->fl_end - fl->fl_start + 1;
  2091. flock->l_whence = 0;
  2092. flock->l_type = fl->fl_type;
  2093. return 0;
  2094. }
  2095. #if BITS_PER_LONG == 32
  2096. static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
  2097. {
  2098. flock->l_pid = locks_translate_pid(fl, task_active_pid_ns(current));
  2099. flock->l_start = fl->fl_start;
  2100. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  2101. fl->fl_end - fl->fl_start + 1;
  2102. flock->l_whence = 0;
  2103. flock->l_type = fl->fl_type;
  2104. }
  2105. #endif
  2106. /* Report the first existing lock that would conflict with l.
  2107. * This implements the F_GETLK command of fcntl().
  2108. */
  2109. int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock *flock)
  2110. {
  2111. struct file_lock *fl;
  2112. int error;
  2113. fl = locks_alloc_lock();
  2114. if (fl == NULL)
  2115. return -ENOMEM;
  2116. error = -EINVAL;
  2117. if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
  2118. goto out;
  2119. error = flock_to_posix_lock(filp, fl, flock);
  2120. if (error)
  2121. goto out;
  2122. if (cmd == F_OFD_GETLK) {
  2123. error = -EINVAL;
  2124. if (flock->l_pid != 0)
  2125. goto out;
  2126. cmd = F_GETLK;
  2127. fl->fl_flags |= FL_OFDLCK;
  2128. fl->fl_owner = filp;
  2129. }
  2130. error = vfs_test_lock(filp, fl);
  2131. if (error)
  2132. goto out;
  2133. flock->l_type = fl->fl_type;
  2134. if (fl->fl_type != F_UNLCK) {
  2135. error = posix_lock_to_flock(flock, fl);
  2136. if (error)
  2137. goto out;
  2138. }
  2139. out:
  2140. locks_free_lock(fl);
  2141. return error;
  2142. }
  2143. /**
  2144. * vfs_lock_file - file byte range lock
  2145. * @filp: The file to apply the lock to
  2146. * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
  2147. * @fl: The lock to be applied
  2148. * @conf: Place to return a copy of the conflicting lock, if found.
  2149. *
  2150. * A caller that doesn't care about the conflicting lock may pass NULL
  2151. * as the final argument.
  2152. *
  2153. * If the filesystem defines a private ->lock() method, then @conf will
  2154. * be left unchanged; so a caller that cares should initialize it to
  2155. * some acceptable default.
  2156. *
  2157. * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
  2158. * locks, the ->lock() interface may return asynchronously, before the lock has
  2159. * been granted or denied by the underlying filesystem, if (and only if)
  2160. * lm_grant is set. Callers expecting ->lock() to return asynchronously
  2161. * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
  2162. * the request is for a blocking lock. When ->lock() does return asynchronously,
  2163. * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
  2164. * request completes.
  2165. * If the request is for non-blocking lock the file system should return
  2166. * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
  2167. * with the result. If the request timed out the callback routine will return a
  2168. * nonzero return code and the file system should release the lock. The file
  2169. * system is also responsible to keep a corresponding posix lock when it
  2170. * grants a lock so the VFS can find out which locks are locally held and do
  2171. * the correct lock cleanup when required.
  2172. * The underlying filesystem must not drop the kernel lock or call
  2173. * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
  2174. * return code.
  2175. */
  2176. int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
  2177. {
  2178. if (filp->f_op->lock)
  2179. return filp->f_op->lock(filp, cmd, fl);
  2180. else
  2181. return posix_lock_file(filp, fl, conf);
  2182. }
  2183. EXPORT_SYMBOL_GPL(vfs_lock_file);
  2184. static int do_lock_file_wait(struct file *filp, unsigned int cmd,
  2185. struct file_lock *fl)
  2186. {
  2187. int error;
  2188. error = security_file_lock(filp, fl->fl_type);
  2189. if (error)
  2190. return error;
  2191. for (;;) {
  2192. error = vfs_lock_file(filp, cmd, fl, NULL);
  2193. if (error != FILE_LOCK_DEFERRED)
  2194. break;
  2195. error = wait_event_interruptible(fl->fl_wait,
  2196. list_empty(&fl->fl_blocked_member));
  2197. if (error)
  2198. break;
  2199. }
  2200. locks_delete_block(fl);
  2201. return error;
  2202. }
  2203. /* Ensure that fl->fl_file has compatible f_mode for F_SETLK calls */
  2204. static int
  2205. check_fmode_for_setlk(struct file_lock *fl)
  2206. {
  2207. switch (fl->fl_type) {
  2208. case F_RDLCK:
  2209. if (!(fl->fl_file->f_mode & FMODE_READ))
  2210. return -EBADF;
  2211. break;
  2212. case F_WRLCK:
  2213. if (!(fl->fl_file->f_mode & FMODE_WRITE))
  2214. return -EBADF;
  2215. }
  2216. return 0;
  2217. }
  2218. /* Apply the lock described by l to an open file descriptor.
  2219. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  2220. */
  2221. int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
  2222. struct flock *flock)
  2223. {
  2224. struct file_lock *file_lock = locks_alloc_lock();
  2225. struct inode *inode = locks_inode(filp);
  2226. struct file *f;
  2227. int error;
  2228. if (file_lock == NULL)
  2229. return -ENOLCK;
  2230. /* Don't allow mandatory locks on files that may be memory mapped
  2231. * and shared.
  2232. */
  2233. if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
  2234. error = -EAGAIN;
  2235. goto out;
  2236. }
  2237. error = flock_to_posix_lock(filp, file_lock, flock);
  2238. if (error)
  2239. goto out;
  2240. error = check_fmode_for_setlk(file_lock);
  2241. if (error)
  2242. goto out;
  2243. /*
  2244. * If the cmd is requesting file-private locks, then set the
  2245. * FL_OFDLCK flag and override the owner.
  2246. */
  2247. switch (cmd) {
  2248. case F_OFD_SETLK:
  2249. error = -EINVAL;
  2250. if (flock->l_pid != 0)
  2251. goto out;
  2252. cmd = F_SETLK;
  2253. file_lock->fl_flags |= FL_OFDLCK;
  2254. file_lock->fl_owner = filp;
  2255. break;
  2256. case F_OFD_SETLKW:
  2257. error = -EINVAL;
  2258. if (flock->l_pid != 0)
  2259. goto out;
  2260. cmd = F_SETLKW;
  2261. file_lock->fl_flags |= FL_OFDLCK;
  2262. file_lock->fl_owner = filp;
  2263. fallthrough;
  2264. case F_SETLKW:
  2265. file_lock->fl_flags |= FL_SLEEP;
  2266. }
  2267. error = do_lock_file_wait(filp, cmd, file_lock);
  2268. /*
  2269. * Attempt to detect a close/fcntl race and recover by releasing the
  2270. * lock that was just acquired. There is no need to do that when we're
  2271. * unlocking though, or for OFD locks.
  2272. */
  2273. if (!error && file_lock->fl_type != F_UNLCK &&
  2274. !(file_lock->fl_flags & FL_OFDLCK)) {
  2275. /*
  2276. * We need that spin_lock here - it prevents reordering between
  2277. * update of i_flctx->flc_posix and check for it done in
  2278. * close(). rcu_read_lock() wouldn't do.
  2279. */
  2280. spin_lock(&current->files->file_lock);
  2281. f = fcheck(fd);
  2282. spin_unlock(&current->files->file_lock);
  2283. if (f != filp) {
  2284. file_lock->fl_type = F_UNLCK;
  2285. error = do_lock_file_wait(filp, cmd, file_lock);
  2286. WARN_ON_ONCE(error);
  2287. error = -EBADF;
  2288. }
  2289. }
  2290. out:
  2291. trace_fcntl_setlk(inode, file_lock, error);
  2292. locks_free_lock(file_lock);
  2293. return error;
  2294. }
  2295. #if BITS_PER_LONG == 32
  2296. /* Report the first existing lock that would conflict with l.
  2297. * This implements the F_GETLK command of fcntl().
  2298. */
  2299. int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 *flock)
  2300. {
  2301. struct file_lock *fl;
  2302. int error;
  2303. fl = locks_alloc_lock();
  2304. if (fl == NULL)
  2305. return -ENOMEM;
  2306. error = -EINVAL;
  2307. if (flock->l_type != F_RDLCK && flock->l_type != F_WRLCK)
  2308. goto out;
  2309. error = flock64_to_posix_lock(filp, fl, flock);
  2310. if (error)
  2311. goto out;
  2312. if (cmd == F_OFD_GETLK) {
  2313. error = -EINVAL;
  2314. if (flock->l_pid != 0)
  2315. goto out;
  2316. cmd = F_GETLK64;
  2317. fl->fl_flags |= FL_OFDLCK;
  2318. fl->fl_owner = filp;
  2319. }
  2320. error = vfs_test_lock(filp, fl);
  2321. if (error)
  2322. goto out;
  2323. flock->l_type = fl->fl_type;
  2324. if (fl->fl_type != F_UNLCK)
  2325. posix_lock_to_flock64(flock, fl);
  2326. out:
  2327. locks_free_lock(fl);
  2328. return error;
  2329. }
  2330. /* Apply the lock described by l to an open file descriptor.
  2331. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  2332. */
  2333. int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
  2334. struct flock64 *flock)
  2335. {
  2336. struct file_lock *file_lock = locks_alloc_lock();
  2337. struct inode *inode = locks_inode(filp);
  2338. struct file *f;
  2339. int error;
  2340. if (file_lock == NULL)
  2341. return -ENOLCK;
  2342. /* Don't allow mandatory locks on files that may be memory mapped
  2343. * and shared.
  2344. */
  2345. if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
  2346. error = -EAGAIN;
  2347. goto out;
  2348. }
  2349. error = flock64_to_posix_lock(filp, file_lock, flock);
  2350. if (error)
  2351. goto out;
  2352. error = check_fmode_for_setlk(file_lock);
  2353. if (error)
  2354. goto out;
  2355. /*
  2356. * If the cmd is requesting file-private locks, then set the
  2357. * FL_OFDLCK flag and override the owner.
  2358. */
  2359. switch (cmd) {
  2360. case F_OFD_SETLK:
  2361. error = -EINVAL;
  2362. if (flock->l_pid != 0)
  2363. goto out;
  2364. cmd = F_SETLK64;
  2365. file_lock->fl_flags |= FL_OFDLCK;
  2366. file_lock->fl_owner = filp;
  2367. break;
  2368. case F_OFD_SETLKW:
  2369. error = -EINVAL;
  2370. if (flock->l_pid != 0)
  2371. goto out;
  2372. cmd = F_SETLKW64;
  2373. file_lock->fl_flags |= FL_OFDLCK;
  2374. file_lock->fl_owner = filp;
  2375. fallthrough;
  2376. case F_SETLKW64:
  2377. file_lock->fl_flags |= FL_SLEEP;
  2378. }
  2379. error = do_lock_file_wait(filp, cmd, file_lock);
  2380. /*
  2381. * Attempt to detect a close/fcntl race and recover by releasing the
  2382. * lock that was just acquired. There is no need to do that when we're
  2383. * unlocking though, or for OFD locks.
  2384. */
  2385. if (!error && file_lock->fl_type != F_UNLCK &&
  2386. !(file_lock->fl_flags & FL_OFDLCK)) {
  2387. /*
  2388. * We need that spin_lock here - it prevents reordering between
  2389. * update of i_flctx->flc_posix and check for it done in
  2390. * close(). rcu_read_lock() wouldn't do.
  2391. */
  2392. spin_lock(&current->files->file_lock);
  2393. f = fcheck(fd);
  2394. spin_unlock(&current->files->file_lock);
  2395. if (f != filp) {
  2396. file_lock->fl_type = F_UNLCK;
  2397. error = do_lock_file_wait(filp, cmd, file_lock);
  2398. WARN_ON_ONCE(error);
  2399. error = -EBADF;
  2400. }
  2401. }
  2402. out:
  2403. locks_free_lock(file_lock);
  2404. return error;
  2405. }
  2406. #endif /* BITS_PER_LONG == 32 */
  2407. /*
  2408. * This function is called when the file is being removed
  2409. * from the task's fd array. POSIX locks belonging to this task
  2410. * are deleted at this time.
  2411. */
  2412. void locks_remove_posix(struct file *filp, fl_owner_t owner)
  2413. {
  2414. int error;
  2415. struct inode *inode = locks_inode(filp);
  2416. struct file_lock lock;
  2417. struct file_lock_context *ctx;
  2418. /*
  2419. * If there are no locks held on this file, we don't need to call
  2420. * posix_lock_file(). Another process could be setting a lock on this
  2421. * file at the same time, but we wouldn't remove that lock anyway.
  2422. */
  2423. ctx = smp_load_acquire(&inode->i_flctx);
  2424. if (!ctx || list_empty(&ctx->flc_posix))
  2425. return;
  2426. locks_init_lock(&lock);
  2427. lock.fl_type = F_UNLCK;
  2428. lock.fl_flags = FL_POSIX | FL_CLOSE;
  2429. lock.fl_start = 0;
  2430. lock.fl_end = OFFSET_MAX;
  2431. lock.fl_owner = owner;
  2432. lock.fl_pid = current->tgid;
  2433. lock.fl_file = filp;
  2434. lock.fl_ops = NULL;
  2435. lock.fl_lmops = NULL;
  2436. error = vfs_lock_file(filp, F_SETLK, &lock, NULL);
  2437. if (lock.fl_ops && lock.fl_ops->fl_release_private)
  2438. lock.fl_ops->fl_release_private(&lock);
  2439. trace_locks_remove_posix(inode, &lock, error);
  2440. }
  2441. EXPORT_SYMBOL(locks_remove_posix);
  2442. /* The i_flctx must be valid when calling into here */
  2443. static void
  2444. locks_remove_flock(struct file *filp, struct file_lock_context *flctx)
  2445. {
  2446. struct file_lock fl;
  2447. struct inode *inode = locks_inode(filp);
  2448. if (list_empty(&flctx->flc_flock))
  2449. return;
  2450. flock_make_lock(filp, LOCK_UN, &fl);
  2451. fl.fl_flags |= FL_CLOSE;
  2452. if (filp->f_op->flock)
  2453. filp->f_op->flock(filp, F_SETLKW, &fl);
  2454. else
  2455. flock_lock_inode(inode, &fl);
  2456. if (fl.fl_ops && fl.fl_ops->fl_release_private)
  2457. fl.fl_ops->fl_release_private(&fl);
  2458. }
  2459. /* The i_flctx must be valid when calling into here */
  2460. static void
  2461. locks_remove_lease(struct file *filp, struct file_lock_context *ctx)
  2462. {
  2463. struct file_lock *fl, *tmp;
  2464. LIST_HEAD(dispose);
  2465. if (list_empty(&ctx->flc_lease))
  2466. return;
  2467. percpu_down_read(&file_rwsem);
  2468. spin_lock(&ctx->flc_lock);
  2469. list_for_each_entry_safe(fl, tmp, &ctx->flc_lease, fl_list)
  2470. if (filp == fl->fl_file)
  2471. lease_modify(fl, F_UNLCK, &dispose);
  2472. spin_unlock(&ctx->flc_lock);
  2473. percpu_up_read(&file_rwsem);
  2474. locks_dispose_list(&dispose);
  2475. }
  2476. /*
  2477. * This function is called on the last close of an open file.
  2478. */
  2479. void locks_remove_file(struct file *filp)
  2480. {
  2481. struct file_lock_context *ctx;
  2482. ctx = smp_load_acquire(&locks_inode(filp)->i_flctx);
  2483. if (!ctx)
  2484. return;
  2485. /* remove any OFD locks */
  2486. locks_remove_posix(filp, filp);
  2487. /* remove flock locks */
  2488. locks_remove_flock(filp, ctx);
  2489. /* remove any leases */
  2490. locks_remove_lease(filp, ctx);
  2491. spin_lock(&ctx->flc_lock);
  2492. locks_check_ctx_file_list(filp, &ctx->flc_posix, "POSIX");
  2493. locks_check_ctx_file_list(filp, &ctx->flc_flock, "FLOCK");
  2494. locks_check_ctx_file_list(filp, &ctx->flc_lease, "LEASE");
  2495. spin_unlock(&ctx->flc_lock);
  2496. }
  2497. /**
  2498. * vfs_cancel_lock - file byte range unblock lock
  2499. * @filp: The file to apply the unblock to
  2500. * @fl: The lock to be unblocked
  2501. *
  2502. * Used by lock managers to cancel blocked requests
  2503. */
  2504. int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
  2505. {
  2506. if (filp->f_op->lock)
  2507. return filp->f_op->lock(filp, F_CANCELLK, fl);
  2508. return 0;
  2509. }
  2510. EXPORT_SYMBOL_GPL(vfs_cancel_lock);
  2511. #ifdef CONFIG_PROC_FS
  2512. #include <linux/proc_fs.h>
  2513. #include <linux/seq_file.h>
  2514. struct locks_iterator {
  2515. int li_cpu;
  2516. loff_t li_pos;
  2517. };
  2518. static void lock_get_status(struct seq_file *f, struct file_lock *fl,
  2519. loff_t id, char *pfx)
  2520. {
  2521. struct inode *inode = NULL;
  2522. unsigned int fl_pid;
  2523. struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
  2524. fl_pid = locks_translate_pid(fl, proc_pidns);
  2525. /*
  2526. * If lock owner is dead (and pid is freed) or not visible in current
  2527. * pidns, zero is shown as a pid value. Check lock info from
  2528. * init_pid_ns to get saved lock pid value.
  2529. */
  2530. if (fl->fl_file != NULL)
  2531. inode = locks_inode(fl->fl_file);
  2532. seq_printf(f, "%lld:%s ", id, pfx);
  2533. if (IS_POSIX(fl)) {
  2534. if (fl->fl_flags & FL_ACCESS)
  2535. seq_puts(f, "ACCESS");
  2536. else if (IS_OFDLCK(fl))
  2537. seq_puts(f, "OFDLCK");
  2538. else
  2539. seq_puts(f, "POSIX ");
  2540. seq_printf(f, " %s ",
  2541. (inode == NULL) ? "*NOINODE*" :
  2542. mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
  2543. } else if (IS_FLOCK(fl)) {
  2544. if (fl->fl_type & LOCK_MAND) {
  2545. seq_puts(f, "FLOCK MSNFS ");
  2546. } else {
  2547. seq_puts(f, "FLOCK ADVISORY ");
  2548. }
  2549. } else if (IS_LEASE(fl)) {
  2550. if (fl->fl_flags & FL_DELEG)
  2551. seq_puts(f, "DELEG ");
  2552. else
  2553. seq_puts(f, "LEASE ");
  2554. if (lease_breaking(fl))
  2555. seq_puts(f, "BREAKING ");
  2556. else if (fl->fl_file)
  2557. seq_puts(f, "ACTIVE ");
  2558. else
  2559. seq_puts(f, "BREAKER ");
  2560. } else {
  2561. seq_puts(f, "UNKNOWN UNKNOWN ");
  2562. }
  2563. if (fl->fl_type & LOCK_MAND) {
  2564. seq_printf(f, "%s ",
  2565. (fl->fl_type & LOCK_READ)
  2566. ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
  2567. : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
  2568. } else {
  2569. int type = IS_LEASE(fl) ? target_leasetype(fl) : fl->fl_type;
  2570. seq_printf(f, "%s ", (type == F_WRLCK) ? "WRITE" :
  2571. (type == F_RDLCK) ? "READ" : "UNLCK");
  2572. }
  2573. if (inode) {
  2574. /* userspace relies on this representation of dev_t */
  2575. seq_printf(f, "%d %02x:%02x:%lu ", fl_pid,
  2576. MAJOR(inode->i_sb->s_dev),
  2577. MINOR(inode->i_sb->s_dev), inode->i_ino);
  2578. } else {
  2579. seq_printf(f, "%d <none>:0 ", fl_pid);
  2580. }
  2581. if (IS_POSIX(fl)) {
  2582. if (fl->fl_end == OFFSET_MAX)
  2583. seq_printf(f, "%Ld EOF\n", fl->fl_start);
  2584. else
  2585. seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
  2586. } else {
  2587. seq_puts(f, "0 EOF\n");
  2588. }
  2589. }
  2590. static int locks_show(struct seq_file *f, void *v)
  2591. {
  2592. struct locks_iterator *iter = f->private;
  2593. struct file_lock *fl, *bfl;
  2594. struct pid_namespace *proc_pidns = proc_pid_ns(file_inode(f->file)->i_sb);
  2595. fl = hlist_entry(v, struct file_lock, fl_link);
  2596. if (locks_translate_pid(fl, proc_pidns) == 0)
  2597. return 0;
  2598. lock_get_status(f, fl, iter->li_pos, "");
  2599. list_for_each_entry(bfl, &fl->fl_blocked_requests, fl_blocked_member)
  2600. lock_get_status(f, bfl, iter->li_pos, " ->");
  2601. return 0;
  2602. }
  2603. static void __show_fd_locks(struct seq_file *f,
  2604. struct list_head *head, int *id,
  2605. struct file *filp, struct files_struct *files)
  2606. {
  2607. struct file_lock *fl;
  2608. list_for_each_entry(fl, head, fl_list) {
  2609. if (filp != fl->fl_file)
  2610. continue;
  2611. if (fl->fl_owner != files &&
  2612. fl->fl_owner != filp)
  2613. continue;
  2614. (*id)++;
  2615. seq_puts(f, "lock:\t");
  2616. lock_get_status(f, fl, *id, "");
  2617. }
  2618. }
  2619. void show_fd_locks(struct seq_file *f,
  2620. struct file *filp, struct files_struct *files)
  2621. {
  2622. struct inode *inode = locks_inode(filp);
  2623. struct file_lock_context *ctx;
  2624. int id = 0;
  2625. ctx = smp_load_acquire(&inode->i_flctx);
  2626. if (!ctx)
  2627. return;
  2628. spin_lock(&ctx->flc_lock);
  2629. __show_fd_locks(f, &ctx->flc_flock, &id, filp, files);
  2630. __show_fd_locks(f, &ctx->flc_posix, &id, filp, files);
  2631. __show_fd_locks(f, &ctx->flc_lease, &id, filp, files);
  2632. spin_unlock(&ctx->flc_lock);
  2633. }
  2634. static void *locks_start(struct seq_file *f, loff_t *pos)
  2635. __acquires(&blocked_lock_lock)
  2636. {
  2637. struct locks_iterator *iter = f->private;
  2638. iter->li_pos = *pos + 1;
  2639. percpu_down_write(&file_rwsem);
  2640. spin_lock(&blocked_lock_lock);
  2641. return seq_hlist_start_percpu(&file_lock_list.hlist, &iter->li_cpu, *pos);
  2642. }
  2643. static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
  2644. {
  2645. struct locks_iterator *iter = f->private;
  2646. ++iter->li_pos;
  2647. return seq_hlist_next_percpu(v, &file_lock_list.hlist, &iter->li_cpu, pos);
  2648. }
  2649. static void locks_stop(struct seq_file *f, void *v)
  2650. __releases(&blocked_lock_lock)
  2651. {
  2652. spin_unlock(&blocked_lock_lock);
  2653. percpu_up_write(&file_rwsem);
  2654. }
  2655. static const struct seq_operations locks_seq_operations = {
  2656. .start = locks_start,
  2657. .next = locks_next,
  2658. .stop = locks_stop,
  2659. .show = locks_show,
  2660. };
  2661. static int __init proc_locks_init(void)
  2662. {
  2663. proc_create_seq_private("locks", 0, NULL, &locks_seq_operations,
  2664. sizeof(struct locks_iterator), NULL);
  2665. return 0;
  2666. }
  2667. fs_initcall(proc_locks_init);
  2668. #endif
  2669. static int __init filelock_init(void)
  2670. {
  2671. int i;
  2672. flctx_cache = kmem_cache_create("file_lock_ctx",
  2673. sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
  2674. filelock_cache = kmem_cache_create("file_lock_cache",
  2675. sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
  2676. for_each_possible_cpu(i) {
  2677. struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
  2678. spin_lock_init(&fll->lock);
  2679. INIT_HLIST_HEAD(&fll->hlist);
  2680. }
  2681. lease_notifier_chain_init();
  2682. return 0;
  2683. }
  2684. core_initcall(filelock_init);