locks.c 55 KB

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