locks.c 58 KB

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