locks.c 68 KB

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