locks.c 70 KB

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