locks.c 69 KB

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