locks.c 56 KB

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