locks.c 63 KB

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