locks.c 79 KB

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