locks.c 72 KB

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