locks.c 69 KB

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