locks.c 63 KB

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