locks.c 55 KB

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