locks.c 71 KB

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