locks.c 76 KB

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