locks.c 81 KB

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