locks.c 73 KB

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