locks.c 57 KB

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