locks.c 60 KB

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