locks.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623
  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/filesystems/mandatory-locking.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/fdtable.h>
  119. #include <linux/fs.h>
  120. #include <linux/init.h>
  121. #include <linux/module.h>
  122. #include <linux/security.h>
  123. #include <linux/slab.h>
  124. #include <linux/syscalls.h>
  125. #include <linux/time.h>
  126. #include <linux/rcupdate.h>
  127. #include <linux/pid_namespace.h>
  128. #include <linux/hashtable.h>
  129. #include <linux/percpu.h>
  130. #include <linux/lglock.h>
  131. #define CREATE_TRACE_POINTS
  132. #include <trace/events/filelock.h>
  133. #include <asm/uaccess.h>
  134. #define IS_POSIX(fl) (fl->fl_flags & FL_POSIX)
  135. #define IS_FLOCK(fl) (fl->fl_flags & FL_FLOCK)
  136. #define IS_LEASE(fl) (fl->fl_flags & (FL_LEASE|FL_DELEG))
  137. #define IS_OFDLCK(fl) (fl->fl_flags & FL_OFDLCK)
  138. static bool lease_breaking(struct file_lock *fl)
  139. {
  140. return fl->fl_flags & (FL_UNLOCK_PENDING | FL_DOWNGRADE_PENDING);
  141. }
  142. static int target_leasetype(struct file_lock *fl)
  143. {
  144. if (fl->fl_flags & FL_UNLOCK_PENDING)
  145. return F_UNLCK;
  146. if (fl->fl_flags & FL_DOWNGRADE_PENDING)
  147. return F_RDLCK;
  148. return fl->fl_type;
  149. }
  150. int leases_enable = 1;
  151. int lease_break_time = 45;
  152. #define for_each_lock(inode, lockp) \
  153. for (lockp = &inode->i_flock; *lockp != NULL; lockp = &(*lockp)->fl_next)
  154. /*
  155. * The global file_lock_list is only used for displaying /proc/locks, so we
  156. * keep a list on each CPU, with each list protected by its own spinlock via
  157. * the file_lock_lglock. Note that alterations to the list also require that
  158. * the relevant i_lock is held.
  159. */
  160. DEFINE_STATIC_LGLOCK(file_lock_lglock);
  161. static DEFINE_PER_CPU(struct hlist_head, file_lock_list);
  162. /*
  163. * The blocked_hash is used to find POSIX lock loops for deadlock detection.
  164. * It is protected by blocked_lock_lock.
  165. *
  166. * We hash locks by lockowner in order to optimize searching for the lock a
  167. * particular lockowner is waiting on.
  168. *
  169. * FIXME: make this value scale via some heuristic? We generally will want more
  170. * buckets when we have more lockowners holding locks, but that's a little
  171. * difficult to determine without knowing what the workload will look like.
  172. */
  173. #define BLOCKED_HASH_BITS 7
  174. static DEFINE_HASHTABLE(blocked_hash, BLOCKED_HASH_BITS);
  175. /*
  176. * This lock protects the blocked_hash. Generally, if you're accessing it, you
  177. * want to be holding this lock.
  178. *
  179. * In addition, it also protects the fl->fl_block list, and the fl->fl_next
  180. * pointer for file_lock structures that are acting as lock requests (in
  181. * contrast to those that are acting as records of acquired locks).
  182. *
  183. * Note that when we acquire this lock in order to change the above fields,
  184. * we often hold the i_lock as well. In certain cases, when reading the fields
  185. * protected by this lock, we can skip acquiring it iff we already hold the
  186. * i_lock.
  187. *
  188. * In particular, adding an entry to the fl_block list requires that you hold
  189. * both the i_lock and the blocked_lock_lock (acquired in that order). Deleting
  190. * an entry from the list however only requires the file_lock_lock.
  191. */
  192. static DEFINE_SPINLOCK(blocked_lock_lock);
  193. static struct kmem_cache *filelock_cache __read_mostly;
  194. static void locks_init_lock_heads(struct file_lock *fl)
  195. {
  196. INIT_HLIST_NODE(&fl->fl_link);
  197. INIT_LIST_HEAD(&fl->fl_block);
  198. init_waitqueue_head(&fl->fl_wait);
  199. }
  200. /* Allocate an empty lock structure. */
  201. struct file_lock *locks_alloc_lock(void)
  202. {
  203. struct file_lock *fl = kmem_cache_zalloc(filelock_cache, GFP_KERNEL);
  204. if (fl)
  205. locks_init_lock_heads(fl);
  206. return fl;
  207. }
  208. EXPORT_SYMBOL_GPL(locks_alloc_lock);
  209. void locks_release_private(struct file_lock *fl)
  210. {
  211. if (fl->fl_ops) {
  212. if (fl->fl_ops->fl_release_private)
  213. fl->fl_ops->fl_release_private(fl);
  214. fl->fl_ops = NULL;
  215. }
  216. if (fl->fl_lmops) {
  217. if (fl->fl_lmops->lm_put_owner)
  218. fl->fl_lmops->lm_put_owner(fl);
  219. fl->fl_lmops = NULL;
  220. }
  221. }
  222. EXPORT_SYMBOL_GPL(locks_release_private);
  223. /* Free a lock which is not in use. */
  224. void locks_free_lock(struct file_lock *fl)
  225. {
  226. BUG_ON(waitqueue_active(&fl->fl_wait));
  227. BUG_ON(!list_empty(&fl->fl_block));
  228. BUG_ON(!hlist_unhashed(&fl->fl_link));
  229. locks_release_private(fl);
  230. kmem_cache_free(filelock_cache, fl);
  231. }
  232. EXPORT_SYMBOL(locks_free_lock);
  233. static void
  234. locks_dispose_list(struct list_head *dispose)
  235. {
  236. struct file_lock *fl;
  237. while (!list_empty(dispose)) {
  238. fl = list_first_entry(dispose, struct file_lock, fl_block);
  239. list_del_init(&fl->fl_block);
  240. locks_free_lock(fl);
  241. }
  242. }
  243. void locks_init_lock(struct file_lock *fl)
  244. {
  245. memset(fl, 0, sizeof(struct file_lock));
  246. locks_init_lock_heads(fl);
  247. }
  248. EXPORT_SYMBOL(locks_init_lock);
  249. /*
  250. * Initialize a new lock from an existing file_lock structure.
  251. */
  252. void locks_copy_conflock(struct file_lock *new, struct file_lock *fl)
  253. {
  254. new->fl_owner = fl->fl_owner;
  255. new->fl_pid = fl->fl_pid;
  256. new->fl_file = NULL;
  257. new->fl_flags = fl->fl_flags;
  258. new->fl_type = fl->fl_type;
  259. new->fl_start = fl->fl_start;
  260. new->fl_end = fl->fl_end;
  261. new->fl_lmops = fl->fl_lmops;
  262. new->fl_ops = NULL;
  263. if (fl->fl_lmops) {
  264. if (fl->fl_lmops->lm_get_owner)
  265. fl->fl_lmops->lm_get_owner(new, fl);
  266. }
  267. }
  268. EXPORT_SYMBOL(locks_copy_conflock);
  269. void locks_copy_lock(struct file_lock *new, struct file_lock *fl)
  270. {
  271. /* "new" must be a freshly-initialized lock */
  272. WARN_ON_ONCE(new->fl_ops);
  273. locks_copy_conflock(new, fl);
  274. new->fl_file = fl->fl_file;
  275. new->fl_ops = fl->fl_ops;
  276. if (fl->fl_ops) {
  277. if (fl->fl_ops->fl_copy_lock)
  278. fl->fl_ops->fl_copy_lock(new, fl);
  279. }
  280. }
  281. EXPORT_SYMBOL(locks_copy_lock);
  282. static inline int flock_translate_cmd(int cmd) {
  283. if (cmd & LOCK_MAND)
  284. return cmd & (LOCK_MAND | LOCK_RW);
  285. switch (cmd) {
  286. case LOCK_SH:
  287. return F_RDLCK;
  288. case LOCK_EX:
  289. return F_WRLCK;
  290. case LOCK_UN:
  291. return F_UNLCK;
  292. }
  293. return -EINVAL;
  294. }
  295. /* Fill in a file_lock structure with an appropriate FLOCK lock. */
  296. static int flock_make_lock(struct file *filp, struct file_lock **lock,
  297. unsigned int cmd)
  298. {
  299. struct file_lock *fl;
  300. int type = flock_translate_cmd(cmd);
  301. if (type < 0)
  302. return type;
  303. fl = locks_alloc_lock();
  304. if (fl == NULL)
  305. return -ENOMEM;
  306. fl->fl_file = filp;
  307. fl->fl_owner = filp;
  308. fl->fl_pid = current->tgid;
  309. fl->fl_flags = FL_FLOCK;
  310. fl->fl_type = type;
  311. fl->fl_end = OFFSET_MAX;
  312. *lock = fl;
  313. return 0;
  314. }
  315. static int assign_type(struct file_lock *fl, long type)
  316. {
  317. switch (type) {
  318. case F_RDLCK:
  319. case F_WRLCK:
  320. case F_UNLCK:
  321. fl->fl_type = type;
  322. break;
  323. default:
  324. return -EINVAL;
  325. }
  326. return 0;
  327. }
  328. static int flock64_to_posix_lock(struct file *filp, struct file_lock *fl,
  329. struct flock64 *l)
  330. {
  331. switch (l->l_whence) {
  332. case SEEK_SET:
  333. fl->fl_start = 0;
  334. break;
  335. case SEEK_CUR:
  336. fl->fl_start = filp->f_pos;
  337. break;
  338. case SEEK_END:
  339. fl->fl_start = i_size_read(file_inode(filp));
  340. break;
  341. default:
  342. return -EINVAL;
  343. }
  344. if (l->l_start > OFFSET_MAX - fl->fl_start)
  345. return -EOVERFLOW;
  346. fl->fl_start += l->l_start;
  347. if (fl->fl_start < 0)
  348. return -EINVAL;
  349. /* POSIX-1996 leaves the case l->l_len < 0 undefined;
  350. POSIX-2001 defines it. */
  351. if (l->l_len > 0) {
  352. if (l->l_len - 1 > OFFSET_MAX - fl->fl_start)
  353. return -EOVERFLOW;
  354. fl->fl_end = fl->fl_start + l->l_len - 1;
  355. } else if (l->l_len < 0) {
  356. if (fl->fl_start + l->l_len < 0)
  357. return -EINVAL;
  358. fl->fl_end = fl->fl_start - 1;
  359. fl->fl_start += l->l_len;
  360. } else
  361. fl->fl_end = OFFSET_MAX;
  362. fl->fl_owner = current->files;
  363. fl->fl_pid = current->tgid;
  364. fl->fl_file = filp;
  365. fl->fl_flags = FL_POSIX;
  366. fl->fl_ops = NULL;
  367. fl->fl_lmops = NULL;
  368. return assign_type(fl, l->l_type);
  369. }
  370. /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  371. * style lock.
  372. */
  373. static int flock_to_posix_lock(struct file *filp, struct file_lock *fl,
  374. struct flock *l)
  375. {
  376. struct flock64 ll = {
  377. .l_type = l->l_type,
  378. .l_whence = l->l_whence,
  379. .l_start = l->l_start,
  380. .l_len = l->l_len,
  381. };
  382. return flock64_to_posix_lock(filp, fl, &ll);
  383. }
  384. /* default lease lock manager operations */
  385. static void lease_break_callback(struct file_lock *fl)
  386. {
  387. kill_fasync(&fl->fl_fasync, SIGIO, POLL_MSG);
  388. }
  389. static void
  390. lease_setup(struct file_lock *fl, void **priv)
  391. {
  392. struct file *filp = fl->fl_file;
  393. struct fasync_struct *fa = *priv;
  394. /*
  395. * fasync_insert_entry() returns the old entry if any. If there was no
  396. * old entry, then it used "priv" and inserted it into the fasync list.
  397. * Clear the pointer to indicate that it shouldn't be freed.
  398. */
  399. if (!fasync_insert_entry(fa->fa_fd, filp, &fl->fl_fasync, fa))
  400. *priv = NULL;
  401. __f_setown(filp, task_pid(current), PIDTYPE_PID, 0);
  402. }
  403. static const struct lock_manager_operations lease_manager_ops = {
  404. .lm_break = lease_break_callback,
  405. .lm_change = lease_modify,
  406. .lm_setup = lease_setup,
  407. };
  408. /*
  409. * Initialize a lease, use the default lock manager operations
  410. */
  411. static int lease_init(struct file *filp, long type, struct file_lock *fl)
  412. {
  413. if (assign_type(fl, type) != 0)
  414. return -EINVAL;
  415. fl->fl_owner = current->files;
  416. fl->fl_pid = current->tgid;
  417. fl->fl_file = filp;
  418. fl->fl_flags = FL_LEASE;
  419. fl->fl_start = 0;
  420. fl->fl_end = OFFSET_MAX;
  421. fl->fl_ops = NULL;
  422. fl->fl_lmops = &lease_manager_ops;
  423. return 0;
  424. }
  425. /* Allocate a file_lock initialised to this type of lease */
  426. static struct file_lock *lease_alloc(struct file *filp, long type)
  427. {
  428. struct file_lock *fl = locks_alloc_lock();
  429. int error = -ENOMEM;
  430. if (fl == NULL)
  431. return ERR_PTR(error);
  432. error = lease_init(filp, type, fl);
  433. if (error) {
  434. locks_free_lock(fl);
  435. return ERR_PTR(error);
  436. }
  437. return fl;
  438. }
  439. /* Check if two locks overlap each other.
  440. */
  441. static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
  442. {
  443. return ((fl1->fl_end >= fl2->fl_start) &&
  444. (fl2->fl_end >= fl1->fl_start));
  445. }
  446. /*
  447. * Check whether two locks have the same owner.
  448. */
  449. static int posix_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  450. {
  451. if (fl1->fl_lmops && fl1->fl_lmops->lm_compare_owner)
  452. return fl2->fl_lmops == fl1->fl_lmops &&
  453. fl1->fl_lmops->lm_compare_owner(fl1, fl2);
  454. return fl1->fl_owner == fl2->fl_owner;
  455. }
  456. /* Must be called with the i_lock held! */
  457. static void locks_insert_global_locks(struct file_lock *fl)
  458. {
  459. lg_local_lock(&file_lock_lglock);
  460. fl->fl_link_cpu = smp_processor_id();
  461. hlist_add_head(&fl->fl_link, this_cpu_ptr(&file_lock_list));
  462. lg_local_unlock(&file_lock_lglock);
  463. }
  464. /* Must be called with the i_lock held! */
  465. static void locks_delete_global_locks(struct file_lock *fl)
  466. {
  467. /*
  468. * Avoid taking lock if already unhashed. This is safe since this check
  469. * is done while holding the i_lock, and new insertions into the list
  470. * also require that it be held.
  471. */
  472. if (hlist_unhashed(&fl->fl_link))
  473. return;
  474. lg_local_lock_cpu(&file_lock_lglock, fl->fl_link_cpu);
  475. hlist_del_init(&fl->fl_link);
  476. lg_local_unlock_cpu(&file_lock_lglock, fl->fl_link_cpu);
  477. }
  478. static unsigned long
  479. posix_owner_key(struct file_lock *fl)
  480. {
  481. if (fl->fl_lmops && fl->fl_lmops->lm_owner_key)
  482. return fl->fl_lmops->lm_owner_key(fl);
  483. return (unsigned long)fl->fl_owner;
  484. }
  485. static void locks_insert_global_blocked(struct file_lock *waiter)
  486. {
  487. hash_add(blocked_hash, &waiter->fl_link, posix_owner_key(waiter));
  488. }
  489. static void locks_delete_global_blocked(struct file_lock *waiter)
  490. {
  491. hash_del(&waiter->fl_link);
  492. }
  493. /* Remove waiter from blocker's block list.
  494. * When blocker ends up pointing to itself then the list is empty.
  495. *
  496. * Must be called with blocked_lock_lock held.
  497. */
  498. static void __locks_delete_block(struct file_lock *waiter)
  499. {
  500. locks_delete_global_blocked(waiter);
  501. list_del_init(&waiter->fl_block);
  502. waiter->fl_next = NULL;
  503. }
  504. static void locks_delete_block(struct file_lock *waiter)
  505. {
  506. spin_lock(&blocked_lock_lock);
  507. __locks_delete_block(waiter);
  508. spin_unlock(&blocked_lock_lock);
  509. }
  510. /* Insert waiter into blocker's block list.
  511. * We use a circular list so that processes can be easily woken up in
  512. * the order they blocked. The documentation doesn't require this but
  513. * it seems like the reasonable thing to do.
  514. *
  515. * Must be called with both the i_lock and blocked_lock_lock held. The fl_block
  516. * list itself is protected by the blocked_lock_lock, but by ensuring that the
  517. * i_lock is also held on insertions we can avoid taking the blocked_lock_lock
  518. * in some cases when we see that the fl_block list is empty.
  519. */
  520. static void __locks_insert_block(struct file_lock *blocker,
  521. struct file_lock *waiter)
  522. {
  523. BUG_ON(!list_empty(&waiter->fl_block));
  524. waiter->fl_next = blocker;
  525. list_add_tail(&waiter->fl_block, &blocker->fl_block);
  526. if (IS_POSIX(blocker) && !IS_OFDLCK(blocker))
  527. locks_insert_global_blocked(waiter);
  528. }
  529. /* Must be called with i_lock held. */
  530. static void locks_insert_block(struct file_lock *blocker,
  531. struct file_lock *waiter)
  532. {
  533. spin_lock(&blocked_lock_lock);
  534. __locks_insert_block(blocker, waiter);
  535. spin_unlock(&blocked_lock_lock);
  536. }
  537. /*
  538. * Wake up processes blocked waiting for blocker.
  539. *
  540. * Must be called with the inode->i_lock held!
  541. */
  542. static void locks_wake_up_blocks(struct file_lock *blocker)
  543. {
  544. /*
  545. * Avoid taking global lock if list is empty. This is safe since new
  546. * blocked requests are only added to the list under the i_lock, and
  547. * the i_lock is always held here. Note that removal from the fl_block
  548. * list does not require the i_lock, so we must recheck list_empty()
  549. * after acquiring the blocked_lock_lock.
  550. */
  551. if (list_empty(&blocker->fl_block))
  552. return;
  553. spin_lock(&blocked_lock_lock);
  554. while (!list_empty(&blocker->fl_block)) {
  555. struct file_lock *waiter;
  556. waiter = list_first_entry(&blocker->fl_block,
  557. struct file_lock, fl_block);
  558. __locks_delete_block(waiter);
  559. if (waiter->fl_lmops && waiter->fl_lmops->lm_notify)
  560. waiter->fl_lmops->lm_notify(waiter);
  561. else
  562. wake_up(&waiter->fl_wait);
  563. }
  564. spin_unlock(&blocked_lock_lock);
  565. }
  566. /* Insert file lock fl into an inode's lock list at the position indicated
  567. * by pos. At the same time add the lock to the global file lock list.
  568. *
  569. * Must be called with the i_lock held!
  570. */
  571. static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
  572. {
  573. fl->fl_nspid = get_pid(task_tgid(current));
  574. /* insert into file's list */
  575. fl->fl_next = *pos;
  576. *pos = fl;
  577. locks_insert_global_locks(fl);
  578. }
  579. /**
  580. * locks_delete_lock - Delete a lock and then free it.
  581. * @thisfl_p: pointer that points to the fl_next field of the previous
  582. * inode->i_flock list entry
  583. *
  584. * Unlink a lock from all lists and free the namespace reference, but don't
  585. * free it yet. Wake up processes that are blocked waiting for this lock and
  586. * notify the FS that the lock has been cleared.
  587. *
  588. * Must be called with the i_lock held!
  589. */
  590. static void locks_unlink_lock(struct file_lock **thisfl_p)
  591. {
  592. struct file_lock *fl = *thisfl_p;
  593. locks_delete_global_locks(fl);
  594. *thisfl_p = fl->fl_next;
  595. fl->fl_next = NULL;
  596. if (fl->fl_nspid) {
  597. put_pid(fl->fl_nspid);
  598. fl->fl_nspid = NULL;
  599. }
  600. locks_wake_up_blocks(fl);
  601. }
  602. /*
  603. * Unlink a lock from all lists and free it.
  604. *
  605. * Must be called with i_lock held!
  606. */
  607. static void locks_delete_lock(struct file_lock **thisfl_p,
  608. struct list_head *dispose)
  609. {
  610. struct file_lock *fl = *thisfl_p;
  611. locks_unlink_lock(thisfl_p);
  612. if (dispose)
  613. list_add(&fl->fl_block, dispose);
  614. else
  615. locks_free_lock(fl);
  616. }
  617. /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
  618. * checks for shared/exclusive status of overlapping locks.
  619. */
  620. static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  621. {
  622. if (sys_fl->fl_type == F_WRLCK)
  623. return 1;
  624. if (caller_fl->fl_type == F_WRLCK)
  625. return 1;
  626. return 0;
  627. }
  628. /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
  629. * checking before calling the locks_conflict().
  630. */
  631. static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  632. {
  633. /* POSIX locks owned by the same process do not conflict with
  634. * each other.
  635. */
  636. if (!IS_POSIX(sys_fl) || posix_same_owner(caller_fl, sys_fl))
  637. return (0);
  638. /* Check whether they overlap */
  639. if (!locks_overlap(caller_fl, sys_fl))
  640. return 0;
  641. return (locks_conflict(caller_fl, sys_fl));
  642. }
  643. /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  644. * checking before calling the locks_conflict().
  645. */
  646. static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  647. {
  648. /* FLOCK locks referring to the same filp do not conflict with
  649. * each other.
  650. */
  651. if (!IS_FLOCK(sys_fl) || (caller_fl->fl_file == sys_fl->fl_file))
  652. return (0);
  653. if ((caller_fl->fl_type & LOCK_MAND) || (sys_fl->fl_type & LOCK_MAND))
  654. return 0;
  655. return (locks_conflict(caller_fl, sys_fl));
  656. }
  657. void
  658. posix_test_lock(struct file *filp, struct file_lock *fl)
  659. {
  660. struct file_lock *cfl;
  661. struct inode *inode = file_inode(filp);
  662. spin_lock(&inode->i_lock);
  663. for (cfl = file_inode(filp)->i_flock; cfl; cfl = cfl->fl_next) {
  664. if (!IS_POSIX(cfl))
  665. continue;
  666. if (posix_locks_conflict(fl, cfl))
  667. break;
  668. }
  669. if (cfl) {
  670. locks_copy_conflock(fl, cfl);
  671. if (cfl->fl_nspid)
  672. fl->fl_pid = pid_vnr(cfl->fl_nspid);
  673. } else
  674. fl->fl_type = F_UNLCK;
  675. spin_unlock(&inode->i_lock);
  676. return;
  677. }
  678. EXPORT_SYMBOL(posix_test_lock);
  679. /*
  680. * Deadlock detection:
  681. *
  682. * We attempt to detect deadlocks that are due purely to posix file
  683. * locks.
  684. *
  685. * We assume that a task can be waiting for at most one lock at a time.
  686. * So for any acquired lock, the process holding that lock may be
  687. * waiting on at most one other lock. That lock in turns may be held by
  688. * someone waiting for at most one other lock. Given a requested lock
  689. * caller_fl which is about to wait for a conflicting lock block_fl, we
  690. * follow this chain of waiters to ensure we are not about to create a
  691. * cycle.
  692. *
  693. * Since we do this before we ever put a process to sleep on a lock, we
  694. * are ensured that there is never a cycle; that is what guarantees that
  695. * the while() loop in posix_locks_deadlock() eventually completes.
  696. *
  697. * Note: the above assumption may not be true when handling lock
  698. * requests from a broken NFS client. It may also fail in the presence
  699. * of tasks (such as posix threads) sharing the same open file table.
  700. * To handle those cases, we just bail out after a few iterations.
  701. *
  702. * For FL_OFDLCK locks, the owner is the filp, not the files_struct.
  703. * Because the owner is not even nominally tied to a thread of
  704. * execution, the deadlock detection below can't reasonably work well. Just
  705. * skip it for those.
  706. *
  707. * In principle, we could do a more limited deadlock detection on FL_OFDLCK
  708. * locks that just checks for the case where two tasks are attempting to
  709. * upgrade from read to write locks on the same inode.
  710. */
  711. #define MAX_DEADLK_ITERATIONS 10
  712. /* Find a lock that the owner of the given block_fl is blocking on. */
  713. static struct file_lock *what_owner_is_waiting_for(struct file_lock *block_fl)
  714. {
  715. struct file_lock *fl;
  716. hash_for_each_possible(blocked_hash, fl, fl_link, posix_owner_key(block_fl)) {
  717. if (posix_same_owner(fl, block_fl))
  718. return fl->fl_next;
  719. }
  720. return NULL;
  721. }
  722. /* Must be called with the blocked_lock_lock held! */
  723. static int posix_locks_deadlock(struct file_lock *caller_fl,
  724. struct file_lock *block_fl)
  725. {
  726. int i = 0;
  727. /*
  728. * This deadlock detector can't reasonably detect deadlocks with
  729. * FL_OFDLCK locks, since they aren't owned by a process, per-se.
  730. */
  731. if (IS_OFDLCK(caller_fl))
  732. return 0;
  733. while ((block_fl = what_owner_is_waiting_for(block_fl))) {
  734. if (i++ > MAX_DEADLK_ITERATIONS)
  735. return 0;
  736. if (posix_same_owner(caller_fl, block_fl))
  737. return 1;
  738. }
  739. return 0;
  740. }
  741. /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks
  742. * after any leases, but before any posix locks.
  743. *
  744. * Note that if called with an FL_EXISTS argument, the caller may determine
  745. * whether or not a lock was successfully freed by testing the return
  746. * value for -ENOENT.
  747. */
  748. static int flock_lock_file(struct file *filp, struct file_lock *request)
  749. {
  750. struct file_lock *new_fl = NULL;
  751. struct file_lock **before;
  752. struct inode * inode = file_inode(filp);
  753. int error = 0;
  754. int found = 0;
  755. LIST_HEAD(dispose);
  756. if (!(request->fl_flags & FL_ACCESS) && (request->fl_type != F_UNLCK)) {
  757. new_fl = locks_alloc_lock();
  758. if (!new_fl)
  759. return -ENOMEM;
  760. }
  761. spin_lock(&inode->i_lock);
  762. if (request->fl_flags & FL_ACCESS)
  763. goto find_conflict;
  764. for_each_lock(inode, before) {
  765. struct file_lock *fl = *before;
  766. if (IS_POSIX(fl))
  767. break;
  768. if (IS_LEASE(fl))
  769. continue;
  770. if (filp != fl->fl_file)
  771. continue;
  772. if (request->fl_type == fl->fl_type)
  773. goto out;
  774. found = 1;
  775. locks_delete_lock(before, &dispose);
  776. break;
  777. }
  778. if (request->fl_type == F_UNLCK) {
  779. if ((request->fl_flags & FL_EXISTS) && !found)
  780. error = -ENOENT;
  781. goto out;
  782. }
  783. /*
  784. * If a higher-priority process was blocked on the old file lock,
  785. * give it the opportunity to lock the file.
  786. */
  787. if (found) {
  788. spin_unlock(&inode->i_lock);
  789. cond_resched();
  790. spin_lock(&inode->i_lock);
  791. }
  792. find_conflict:
  793. for_each_lock(inode, before) {
  794. struct file_lock *fl = *before;
  795. if (IS_POSIX(fl))
  796. break;
  797. if (IS_LEASE(fl))
  798. continue;
  799. if (!flock_locks_conflict(request, fl))
  800. continue;
  801. error = -EAGAIN;
  802. if (!(request->fl_flags & FL_SLEEP))
  803. goto out;
  804. error = FILE_LOCK_DEFERRED;
  805. locks_insert_block(fl, request);
  806. goto out;
  807. }
  808. if (request->fl_flags & FL_ACCESS)
  809. goto out;
  810. locks_copy_lock(new_fl, request);
  811. locks_insert_lock(before, new_fl);
  812. new_fl = NULL;
  813. error = 0;
  814. out:
  815. spin_unlock(&inode->i_lock);
  816. if (new_fl)
  817. locks_free_lock(new_fl);
  818. locks_dispose_list(&dispose);
  819. return error;
  820. }
  821. static int __posix_lock_file(struct inode *inode, struct file_lock *request, struct file_lock *conflock)
  822. {
  823. struct file_lock *fl;
  824. struct file_lock *new_fl = NULL;
  825. struct file_lock *new_fl2 = NULL;
  826. struct file_lock *left = NULL;
  827. struct file_lock *right = NULL;
  828. struct file_lock **before;
  829. int error;
  830. bool added = false;
  831. LIST_HEAD(dispose);
  832. /*
  833. * We may need two file_lock structures for this operation,
  834. * so we get them in advance to avoid races.
  835. *
  836. * In some cases we can be sure, that no new locks will be needed
  837. */
  838. if (!(request->fl_flags & FL_ACCESS) &&
  839. (request->fl_type != F_UNLCK ||
  840. request->fl_start != 0 || request->fl_end != OFFSET_MAX)) {
  841. new_fl = locks_alloc_lock();
  842. new_fl2 = locks_alloc_lock();
  843. }
  844. spin_lock(&inode->i_lock);
  845. /*
  846. * New lock request. Walk all POSIX locks and look for conflicts. If
  847. * there are any, either return error or put the request on the
  848. * blocker's list of waiters and the global blocked_hash.
  849. */
  850. if (request->fl_type != F_UNLCK) {
  851. for_each_lock(inode, before) {
  852. fl = *before;
  853. if (!IS_POSIX(fl))
  854. continue;
  855. if (!posix_locks_conflict(request, fl))
  856. continue;
  857. if (conflock)
  858. locks_copy_conflock(conflock, fl);
  859. error = -EAGAIN;
  860. if (!(request->fl_flags & FL_SLEEP))
  861. goto out;
  862. /*
  863. * Deadlock detection and insertion into the blocked
  864. * locks list must be done while holding the same lock!
  865. */
  866. error = -EDEADLK;
  867. spin_lock(&blocked_lock_lock);
  868. if (likely(!posix_locks_deadlock(request, fl))) {
  869. error = FILE_LOCK_DEFERRED;
  870. __locks_insert_block(fl, request);
  871. }
  872. spin_unlock(&blocked_lock_lock);
  873. goto out;
  874. }
  875. }
  876. /* If we're just looking for a conflict, we're done. */
  877. error = 0;
  878. if (request->fl_flags & FL_ACCESS)
  879. goto out;
  880. /*
  881. * Find the first old lock with the same owner as the new lock.
  882. */
  883. before = &inode->i_flock;
  884. /* First skip locks owned by other processes. */
  885. while ((fl = *before) && (!IS_POSIX(fl) ||
  886. !posix_same_owner(request, fl))) {
  887. before = &fl->fl_next;
  888. }
  889. /* Process locks with this owner. */
  890. while ((fl = *before) && posix_same_owner(request, fl)) {
  891. /* Detect adjacent or overlapping regions (if same lock type)
  892. */
  893. if (request->fl_type == fl->fl_type) {
  894. /* In all comparisons of start vs end, use
  895. * "start - 1" rather than "end + 1". If end
  896. * is OFFSET_MAX, end + 1 will become negative.
  897. */
  898. if (fl->fl_end < request->fl_start - 1)
  899. goto next_lock;
  900. /* If the next lock in the list has entirely bigger
  901. * addresses than the new one, insert the lock here.
  902. */
  903. if (fl->fl_start - 1 > request->fl_end)
  904. break;
  905. /* If we come here, the new and old lock are of the
  906. * same type and adjacent or overlapping. Make one
  907. * lock yielding from the lower start address of both
  908. * locks to the higher end address.
  909. */
  910. if (fl->fl_start > request->fl_start)
  911. fl->fl_start = request->fl_start;
  912. else
  913. request->fl_start = fl->fl_start;
  914. if (fl->fl_end < request->fl_end)
  915. fl->fl_end = request->fl_end;
  916. else
  917. request->fl_end = fl->fl_end;
  918. if (added) {
  919. locks_delete_lock(before, &dispose);
  920. continue;
  921. }
  922. request = fl;
  923. added = true;
  924. }
  925. else {
  926. /* Processing for different lock types is a bit
  927. * more complex.
  928. */
  929. if (fl->fl_end < request->fl_start)
  930. goto next_lock;
  931. if (fl->fl_start > request->fl_end)
  932. break;
  933. if (request->fl_type == F_UNLCK)
  934. added = true;
  935. if (fl->fl_start < request->fl_start)
  936. left = fl;
  937. /* If the next lock in the list has a higher end
  938. * address than the new one, insert the new one here.
  939. */
  940. if (fl->fl_end > request->fl_end) {
  941. right = fl;
  942. break;
  943. }
  944. if (fl->fl_start >= request->fl_start) {
  945. /* The new lock completely replaces an old
  946. * one (This may happen several times).
  947. */
  948. if (added) {
  949. locks_delete_lock(before, &dispose);
  950. continue;
  951. }
  952. /*
  953. * Replace the old lock with new_fl, and
  954. * remove the old one. It's safe to do the
  955. * insert here since we know that we won't be
  956. * using new_fl later, and that the lock is
  957. * just replacing an existing lock.
  958. */
  959. error = -ENOLCK;
  960. if (!new_fl)
  961. goto out;
  962. locks_copy_lock(new_fl, request);
  963. request = new_fl;
  964. new_fl = NULL;
  965. locks_delete_lock(before, &dispose);
  966. locks_insert_lock(before, request);
  967. added = true;
  968. }
  969. }
  970. /* Go on to next lock.
  971. */
  972. next_lock:
  973. before = &fl->fl_next;
  974. }
  975. /*
  976. * The above code only modifies existing locks in case of merging or
  977. * replacing. If new lock(s) need to be inserted all modifications are
  978. * done below this, so it's safe yet to bail out.
  979. */
  980. error = -ENOLCK; /* "no luck" */
  981. if (right && left == right && !new_fl2)
  982. goto out;
  983. error = 0;
  984. if (!added) {
  985. if (request->fl_type == F_UNLCK) {
  986. if (request->fl_flags & FL_EXISTS)
  987. error = -ENOENT;
  988. goto out;
  989. }
  990. if (!new_fl) {
  991. error = -ENOLCK;
  992. goto out;
  993. }
  994. locks_copy_lock(new_fl, request);
  995. locks_insert_lock(before, new_fl);
  996. new_fl = NULL;
  997. }
  998. if (right) {
  999. if (left == right) {
  1000. /* The new lock breaks the old one in two pieces,
  1001. * so we have to use the second new lock.
  1002. */
  1003. left = new_fl2;
  1004. new_fl2 = NULL;
  1005. locks_copy_lock(left, right);
  1006. locks_insert_lock(before, left);
  1007. }
  1008. right->fl_start = request->fl_end + 1;
  1009. locks_wake_up_blocks(right);
  1010. }
  1011. if (left) {
  1012. left->fl_end = request->fl_start - 1;
  1013. locks_wake_up_blocks(left);
  1014. }
  1015. out:
  1016. spin_unlock(&inode->i_lock);
  1017. /*
  1018. * Free any unused locks.
  1019. */
  1020. if (new_fl)
  1021. locks_free_lock(new_fl);
  1022. if (new_fl2)
  1023. locks_free_lock(new_fl2);
  1024. locks_dispose_list(&dispose);
  1025. return error;
  1026. }
  1027. /**
  1028. * posix_lock_file - Apply a POSIX-style lock to a file
  1029. * @filp: The file to apply the lock to
  1030. * @fl: The lock to be applied
  1031. * @conflock: Place to return a copy of the conflicting lock, if found.
  1032. *
  1033. * Add a POSIX style lock to a file.
  1034. * We merge adjacent & overlapping locks whenever possible.
  1035. * POSIX locks are sorted by owner task, then by starting address
  1036. *
  1037. * Note that if called with an FL_EXISTS argument, the caller may determine
  1038. * whether or not a lock was successfully freed by testing the return
  1039. * value for -ENOENT.
  1040. */
  1041. int posix_lock_file(struct file *filp, struct file_lock *fl,
  1042. struct file_lock *conflock)
  1043. {
  1044. return __posix_lock_file(file_inode(filp), fl, conflock);
  1045. }
  1046. EXPORT_SYMBOL(posix_lock_file);
  1047. /**
  1048. * posix_lock_file_wait - Apply a POSIX-style lock to a file
  1049. * @filp: The file to apply the lock to
  1050. * @fl: The lock to be applied
  1051. *
  1052. * Add a POSIX style lock to a file.
  1053. * We merge adjacent & overlapping locks whenever possible.
  1054. * POSIX locks are sorted by owner task, then by starting address
  1055. */
  1056. int posix_lock_file_wait(struct file *filp, struct file_lock *fl)
  1057. {
  1058. int error;
  1059. might_sleep ();
  1060. for (;;) {
  1061. error = posix_lock_file(filp, fl, NULL);
  1062. if (error != FILE_LOCK_DEFERRED)
  1063. break;
  1064. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  1065. if (!error)
  1066. continue;
  1067. locks_delete_block(fl);
  1068. break;
  1069. }
  1070. return error;
  1071. }
  1072. EXPORT_SYMBOL(posix_lock_file_wait);
  1073. /**
  1074. * locks_mandatory_locked - Check for an active lock
  1075. * @file: the file to check
  1076. *
  1077. * Searches the inode's list of locks to find any POSIX locks which conflict.
  1078. * This function is called from locks_verify_locked() only.
  1079. */
  1080. int locks_mandatory_locked(struct file *file)
  1081. {
  1082. struct inode *inode = file_inode(file);
  1083. struct file_lock *fl;
  1084. /*
  1085. * Search the lock list for this inode for any POSIX locks.
  1086. */
  1087. spin_lock(&inode->i_lock);
  1088. for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  1089. if (!IS_POSIX(fl))
  1090. continue;
  1091. if (fl->fl_owner != current->files &&
  1092. fl->fl_owner != file)
  1093. break;
  1094. }
  1095. spin_unlock(&inode->i_lock);
  1096. return fl ? -EAGAIN : 0;
  1097. }
  1098. /**
  1099. * locks_mandatory_area - Check for a conflicting lock
  1100. * @read_write: %FLOCK_VERIFY_WRITE for exclusive access, %FLOCK_VERIFY_READ
  1101. * for shared
  1102. * @inode: the file to check
  1103. * @filp: how the file was opened (if it was)
  1104. * @offset: start of area to check
  1105. * @count: length of area to check
  1106. *
  1107. * Searches the inode's list of locks to find any POSIX locks which conflict.
  1108. * This function is called from rw_verify_area() and
  1109. * locks_verify_truncate().
  1110. */
  1111. int locks_mandatory_area(int read_write, struct inode *inode,
  1112. struct file *filp, loff_t offset,
  1113. size_t count)
  1114. {
  1115. struct file_lock fl;
  1116. int error;
  1117. bool sleep = false;
  1118. locks_init_lock(&fl);
  1119. fl.fl_pid = current->tgid;
  1120. fl.fl_file = filp;
  1121. fl.fl_flags = FL_POSIX | FL_ACCESS;
  1122. if (filp && !(filp->f_flags & O_NONBLOCK))
  1123. sleep = true;
  1124. fl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
  1125. fl.fl_start = offset;
  1126. fl.fl_end = offset + count - 1;
  1127. for (;;) {
  1128. if (filp) {
  1129. fl.fl_owner = filp;
  1130. fl.fl_flags &= ~FL_SLEEP;
  1131. error = __posix_lock_file(inode, &fl, NULL);
  1132. if (!error)
  1133. break;
  1134. }
  1135. if (sleep)
  1136. fl.fl_flags |= FL_SLEEP;
  1137. fl.fl_owner = current->files;
  1138. error = __posix_lock_file(inode, &fl, NULL);
  1139. if (error != FILE_LOCK_DEFERRED)
  1140. break;
  1141. error = wait_event_interruptible(fl.fl_wait, !fl.fl_next);
  1142. if (!error) {
  1143. /*
  1144. * If we've been sleeping someone might have
  1145. * changed the permissions behind our back.
  1146. */
  1147. if (__mandatory_lock(inode))
  1148. continue;
  1149. }
  1150. locks_delete_block(&fl);
  1151. break;
  1152. }
  1153. return error;
  1154. }
  1155. EXPORT_SYMBOL(locks_mandatory_area);
  1156. static void lease_clear_pending(struct file_lock *fl, int arg)
  1157. {
  1158. switch (arg) {
  1159. case F_UNLCK:
  1160. fl->fl_flags &= ~FL_UNLOCK_PENDING;
  1161. /* fall through: */
  1162. case F_RDLCK:
  1163. fl->fl_flags &= ~FL_DOWNGRADE_PENDING;
  1164. }
  1165. }
  1166. /* We already had a lease on this file; just change its type */
  1167. int lease_modify(struct file_lock **before, int arg, struct list_head *dispose)
  1168. {
  1169. struct file_lock *fl = *before;
  1170. int error = assign_type(fl, arg);
  1171. if (error)
  1172. return error;
  1173. lease_clear_pending(fl, arg);
  1174. locks_wake_up_blocks(fl);
  1175. if (arg == F_UNLCK) {
  1176. struct file *filp = fl->fl_file;
  1177. f_delown(filp);
  1178. filp->f_owner.signum = 0;
  1179. fasync_helper(0, fl->fl_file, 0, &fl->fl_fasync);
  1180. if (fl->fl_fasync != NULL) {
  1181. printk(KERN_ERR "locks_delete_lock: fasync == %p\n", fl->fl_fasync);
  1182. fl->fl_fasync = NULL;
  1183. }
  1184. locks_delete_lock(before, dispose);
  1185. }
  1186. return 0;
  1187. }
  1188. EXPORT_SYMBOL(lease_modify);
  1189. static bool past_time(unsigned long then)
  1190. {
  1191. if (!then)
  1192. /* 0 is a special value meaning "this never expires": */
  1193. return false;
  1194. return time_after(jiffies, then);
  1195. }
  1196. static void time_out_leases(struct inode *inode, struct list_head *dispose)
  1197. {
  1198. struct file_lock **before;
  1199. struct file_lock *fl;
  1200. lockdep_assert_held(&inode->i_lock);
  1201. before = &inode->i_flock;
  1202. while ((fl = *before) && IS_LEASE(fl) && lease_breaking(fl)) {
  1203. trace_time_out_leases(inode, fl);
  1204. if (past_time(fl->fl_downgrade_time))
  1205. lease_modify(before, F_RDLCK, dispose);
  1206. if (past_time(fl->fl_break_time))
  1207. lease_modify(before, F_UNLCK, dispose);
  1208. if (fl == *before) /* lease_modify may have freed fl */
  1209. before = &fl->fl_next;
  1210. }
  1211. }
  1212. static bool leases_conflict(struct file_lock *lease, struct file_lock *breaker)
  1213. {
  1214. if ((breaker->fl_flags & FL_DELEG) && (lease->fl_flags & FL_LEASE))
  1215. return false;
  1216. return locks_conflict(breaker, lease);
  1217. }
  1218. static bool
  1219. any_leases_conflict(struct inode *inode, struct file_lock *breaker)
  1220. {
  1221. struct file_lock *fl;
  1222. lockdep_assert_held(&inode->i_lock);
  1223. for (fl = inode->i_flock ; fl && IS_LEASE(fl); fl = fl->fl_next) {
  1224. if (leases_conflict(fl, breaker))
  1225. return true;
  1226. }
  1227. return false;
  1228. }
  1229. /**
  1230. * __break_lease - revoke all outstanding leases on file
  1231. * @inode: the inode of the file to return
  1232. * @mode: O_RDONLY: break only write leases; O_WRONLY or O_RDWR:
  1233. * break all leases
  1234. * @type: FL_LEASE: break leases and delegations; FL_DELEG: break
  1235. * only delegations
  1236. *
  1237. * break_lease (inlined for speed) has checked there already is at least
  1238. * some kind of lock (maybe a lease) on this file. Leases are broken on
  1239. * a call to open() or truncate(). This function can sleep unless you
  1240. * specified %O_NONBLOCK to your open().
  1241. */
  1242. int __break_lease(struct inode *inode, unsigned int mode, unsigned int type)
  1243. {
  1244. int error = 0;
  1245. struct file_lock *new_fl;
  1246. struct file_lock *fl;
  1247. unsigned long break_time;
  1248. int want_write = (mode & O_ACCMODE) != O_RDONLY;
  1249. LIST_HEAD(dispose);
  1250. new_fl = lease_alloc(NULL, want_write ? F_WRLCK : F_RDLCK);
  1251. if (IS_ERR(new_fl))
  1252. return PTR_ERR(new_fl);
  1253. new_fl->fl_flags = type;
  1254. spin_lock(&inode->i_lock);
  1255. time_out_leases(inode, &dispose);
  1256. if (!any_leases_conflict(inode, new_fl))
  1257. goto out;
  1258. break_time = 0;
  1259. if (lease_break_time > 0) {
  1260. break_time = jiffies + lease_break_time * HZ;
  1261. if (break_time == 0)
  1262. break_time++; /* so that 0 means no break time */
  1263. }
  1264. for (fl = inode->i_flock; fl && IS_LEASE(fl); fl = fl->fl_next) {
  1265. if (!leases_conflict(fl, new_fl))
  1266. continue;
  1267. if (want_write) {
  1268. if (fl->fl_flags & FL_UNLOCK_PENDING)
  1269. continue;
  1270. fl->fl_flags |= FL_UNLOCK_PENDING;
  1271. fl->fl_break_time = break_time;
  1272. } else {
  1273. if (lease_breaking(inode->i_flock))
  1274. continue;
  1275. fl->fl_flags |= FL_DOWNGRADE_PENDING;
  1276. fl->fl_downgrade_time = break_time;
  1277. }
  1278. fl->fl_lmops->lm_break(fl);
  1279. }
  1280. if (mode & O_NONBLOCK) {
  1281. trace_break_lease_noblock(inode, new_fl);
  1282. error = -EWOULDBLOCK;
  1283. goto out;
  1284. }
  1285. restart:
  1286. break_time = inode->i_flock->fl_break_time;
  1287. if (break_time != 0)
  1288. break_time -= jiffies;
  1289. if (break_time == 0)
  1290. break_time++;
  1291. locks_insert_block(inode->i_flock, new_fl);
  1292. trace_break_lease_block(inode, new_fl);
  1293. spin_unlock(&inode->i_lock);
  1294. locks_dispose_list(&dispose);
  1295. error = wait_event_interruptible_timeout(new_fl->fl_wait,
  1296. !new_fl->fl_next, break_time);
  1297. spin_lock(&inode->i_lock);
  1298. trace_break_lease_unblock(inode, new_fl);
  1299. locks_delete_block(new_fl);
  1300. if (error >= 0) {
  1301. /*
  1302. * Wait for the next conflicting lease that has not been
  1303. * broken yet
  1304. */
  1305. if (error == 0)
  1306. time_out_leases(inode, &dispose);
  1307. if (any_leases_conflict(inode, new_fl))
  1308. goto restart;
  1309. error = 0;
  1310. }
  1311. out:
  1312. spin_unlock(&inode->i_lock);
  1313. locks_dispose_list(&dispose);
  1314. locks_free_lock(new_fl);
  1315. return error;
  1316. }
  1317. EXPORT_SYMBOL(__break_lease);
  1318. /**
  1319. * lease_get_mtime - get the last modified time of an inode
  1320. * @inode: the inode
  1321. * @time: pointer to a timespec which will contain the last modified time
  1322. *
  1323. * This is to force NFS clients to flush their caches for files with
  1324. * exclusive leases. The justification is that if someone has an
  1325. * exclusive lease, then they could be modifying it.
  1326. */
  1327. void lease_get_mtime(struct inode *inode, struct timespec *time)
  1328. {
  1329. bool has_lease = false;
  1330. struct file_lock *flock;
  1331. if (inode->i_flock) {
  1332. spin_lock(&inode->i_lock);
  1333. flock = inode->i_flock;
  1334. if (flock && IS_LEASE(flock) && (flock->fl_type == F_WRLCK))
  1335. has_lease = true;
  1336. spin_unlock(&inode->i_lock);
  1337. }
  1338. if (has_lease)
  1339. *time = current_fs_time(inode->i_sb);
  1340. else
  1341. *time = inode->i_mtime;
  1342. }
  1343. EXPORT_SYMBOL(lease_get_mtime);
  1344. /**
  1345. * fcntl_getlease - Enquire what lease is currently active
  1346. * @filp: the file
  1347. *
  1348. * The value returned by this function will be one of
  1349. * (if no lease break is pending):
  1350. *
  1351. * %F_RDLCK to indicate a shared lease is held.
  1352. *
  1353. * %F_WRLCK to indicate an exclusive lease is held.
  1354. *
  1355. * %F_UNLCK to indicate no lease is held.
  1356. *
  1357. * (if a lease break is pending):
  1358. *
  1359. * %F_RDLCK to indicate an exclusive lease needs to be
  1360. * changed to a shared lease (or removed).
  1361. *
  1362. * %F_UNLCK to indicate the lease needs to be removed.
  1363. *
  1364. * XXX: sfr & willy disagree over whether F_INPROGRESS
  1365. * should be returned to userspace.
  1366. */
  1367. int fcntl_getlease(struct file *filp)
  1368. {
  1369. struct file_lock *fl;
  1370. struct inode *inode = file_inode(filp);
  1371. int type = F_UNLCK;
  1372. LIST_HEAD(dispose);
  1373. spin_lock(&inode->i_lock);
  1374. time_out_leases(file_inode(filp), &dispose);
  1375. for (fl = file_inode(filp)->i_flock; fl && IS_LEASE(fl);
  1376. fl = fl->fl_next) {
  1377. if (fl->fl_file == filp) {
  1378. type = target_leasetype(fl);
  1379. break;
  1380. }
  1381. }
  1382. spin_unlock(&inode->i_lock);
  1383. locks_dispose_list(&dispose);
  1384. return type;
  1385. }
  1386. /**
  1387. * check_conflicting_open - see if the given dentry points to a file that has
  1388. * an existing open that would conflict with the
  1389. * desired lease.
  1390. * @dentry: dentry to check
  1391. * @arg: type of lease that we're trying to acquire
  1392. *
  1393. * Check to see if there's an existing open fd on this file that would
  1394. * conflict with the lease we're trying to set.
  1395. */
  1396. static int
  1397. check_conflicting_open(const struct dentry *dentry, const long arg)
  1398. {
  1399. int ret = 0;
  1400. struct inode *inode = dentry->d_inode;
  1401. if ((arg == F_RDLCK) && (atomic_read(&inode->i_writecount) > 0))
  1402. return -EAGAIN;
  1403. if ((arg == F_WRLCK) && ((d_count(dentry) > 1) ||
  1404. (atomic_read(&inode->i_count) > 1)))
  1405. ret = -EAGAIN;
  1406. return ret;
  1407. }
  1408. static int
  1409. generic_add_lease(struct file *filp, long arg, struct file_lock **flp, void **priv)
  1410. {
  1411. struct file_lock *fl, **before, **my_before = NULL, *lease;
  1412. struct dentry *dentry = filp->f_path.dentry;
  1413. struct inode *inode = dentry->d_inode;
  1414. bool is_deleg = (*flp)->fl_flags & FL_DELEG;
  1415. int error;
  1416. LIST_HEAD(dispose);
  1417. lease = *flp;
  1418. trace_generic_add_lease(inode, lease);
  1419. /*
  1420. * In the delegation case we need mutual exclusion with
  1421. * a number of operations that take the i_mutex. We trylock
  1422. * because delegations are an optional optimization, and if
  1423. * there's some chance of a conflict--we'd rather not
  1424. * bother, maybe that's a sign this just isn't a good file to
  1425. * hand out a delegation on.
  1426. */
  1427. if (is_deleg && !mutex_trylock(&inode->i_mutex))
  1428. return -EAGAIN;
  1429. if (is_deleg && arg == F_WRLCK) {
  1430. /* Write delegations are not currently supported: */
  1431. mutex_unlock(&inode->i_mutex);
  1432. WARN_ON_ONCE(1);
  1433. return -EINVAL;
  1434. }
  1435. spin_lock(&inode->i_lock);
  1436. time_out_leases(inode, &dispose);
  1437. error = check_conflicting_open(dentry, arg);
  1438. if (error)
  1439. goto out;
  1440. /*
  1441. * At this point, we know that if there is an exclusive
  1442. * lease on this file, then we hold it on this filp
  1443. * (otherwise our open of this file would have blocked).
  1444. * And if we are trying to acquire an exclusive lease,
  1445. * then the file is not open by anyone (including us)
  1446. * except for this filp.
  1447. */
  1448. error = -EAGAIN;
  1449. for (before = &inode->i_flock;
  1450. ((fl = *before) != NULL) && IS_LEASE(fl);
  1451. before = &fl->fl_next) {
  1452. if (fl->fl_file == filp) {
  1453. my_before = before;
  1454. continue;
  1455. }
  1456. /*
  1457. * No exclusive leases if someone else has a lease on
  1458. * this file:
  1459. */
  1460. if (arg == F_WRLCK)
  1461. goto out;
  1462. /*
  1463. * Modifying our existing lease is OK, but no getting a
  1464. * new lease if someone else is opening for write:
  1465. */
  1466. if (fl->fl_flags & FL_UNLOCK_PENDING)
  1467. goto out;
  1468. }
  1469. if (my_before != NULL) {
  1470. lease = *my_before;
  1471. error = lease->fl_lmops->lm_change(my_before, arg, &dispose);
  1472. if (error)
  1473. goto out;
  1474. goto out_setup;
  1475. }
  1476. error = -EINVAL;
  1477. if (!leases_enable)
  1478. goto out;
  1479. locks_insert_lock(before, lease);
  1480. /*
  1481. * The check in break_lease() is lockless. It's possible for another
  1482. * open to race in after we did the earlier check for a conflicting
  1483. * open but before the lease was inserted. Check again for a
  1484. * conflicting open and cancel the lease if there is one.
  1485. *
  1486. * We also add a barrier here to ensure that the insertion of the lock
  1487. * precedes these checks.
  1488. */
  1489. smp_mb();
  1490. error = check_conflicting_open(dentry, arg);
  1491. if (error)
  1492. goto out_unlink;
  1493. out_setup:
  1494. if (lease->fl_lmops->lm_setup)
  1495. lease->fl_lmops->lm_setup(lease, priv);
  1496. out:
  1497. spin_unlock(&inode->i_lock);
  1498. locks_dispose_list(&dispose);
  1499. if (is_deleg)
  1500. mutex_unlock(&inode->i_mutex);
  1501. if (!error && !my_before)
  1502. *flp = NULL;
  1503. return error;
  1504. out_unlink:
  1505. locks_unlink_lock(before);
  1506. goto out;
  1507. }
  1508. static int generic_delete_lease(struct file *filp)
  1509. {
  1510. int error = -EAGAIN;
  1511. struct file_lock *fl, **before;
  1512. struct dentry *dentry = filp->f_path.dentry;
  1513. struct inode *inode = dentry->d_inode;
  1514. LIST_HEAD(dispose);
  1515. spin_lock(&inode->i_lock);
  1516. time_out_leases(inode, &dispose);
  1517. for (before = &inode->i_flock;
  1518. ((fl = *before) != NULL) && IS_LEASE(fl);
  1519. before = &fl->fl_next) {
  1520. if (fl->fl_file == filp)
  1521. break;
  1522. }
  1523. trace_generic_delete_lease(inode, fl);
  1524. if (fl)
  1525. error = fl->fl_lmops->lm_change(before, F_UNLCK, &dispose);
  1526. spin_unlock(&inode->i_lock);
  1527. locks_dispose_list(&dispose);
  1528. return error;
  1529. }
  1530. /**
  1531. * generic_setlease - sets a lease on an open file
  1532. * @filp: file pointer
  1533. * @arg: type of lease to obtain
  1534. * @flp: input - file_lock to use, output - file_lock inserted
  1535. * @priv: private data for lm_setup (may be NULL if lm_setup
  1536. * doesn't require it)
  1537. *
  1538. * The (input) flp->fl_lmops->lm_break function is required
  1539. * by break_lease().
  1540. */
  1541. int generic_setlease(struct file *filp, long arg, struct file_lock **flp,
  1542. void **priv)
  1543. {
  1544. struct dentry *dentry = filp->f_path.dentry;
  1545. struct inode *inode = dentry->d_inode;
  1546. int error;
  1547. if ((!uid_eq(current_fsuid(), inode->i_uid)) && !capable(CAP_LEASE))
  1548. return -EACCES;
  1549. if (!S_ISREG(inode->i_mode))
  1550. return -EINVAL;
  1551. error = security_file_lock(filp, arg);
  1552. if (error)
  1553. return error;
  1554. switch (arg) {
  1555. case F_UNLCK:
  1556. return generic_delete_lease(filp);
  1557. case F_RDLCK:
  1558. case F_WRLCK:
  1559. if (!(*flp)->fl_lmops->lm_break) {
  1560. WARN_ON_ONCE(1);
  1561. return -ENOLCK;
  1562. }
  1563. return generic_add_lease(filp, arg, flp, priv);
  1564. default:
  1565. return -EINVAL;
  1566. }
  1567. }
  1568. EXPORT_SYMBOL(generic_setlease);
  1569. /**
  1570. * vfs_setlease - sets a lease on an open file
  1571. * @filp: file pointer
  1572. * @arg: type of lease to obtain
  1573. * @lease: file_lock to use when adding a lease
  1574. * @priv: private info for lm_setup when adding a lease (may be
  1575. * NULL if lm_setup doesn't require it)
  1576. *
  1577. * Call this to establish a lease on the file. The "lease" argument is not
  1578. * used for F_UNLCK requests and may be NULL. For commands that set or alter
  1579. * an existing lease, the (*lease)->fl_lmops->lm_break operation must be set;
  1580. * if not, this function will return -ENOLCK (and generate a scary-looking
  1581. * stack trace).
  1582. *
  1583. * The "priv" pointer is passed directly to the lm_setup function as-is. It
  1584. * may be NULL if the lm_setup operation doesn't require it.
  1585. */
  1586. int
  1587. vfs_setlease(struct file *filp, long arg, struct file_lock **lease, void **priv)
  1588. {
  1589. if (filp->f_op->setlease)
  1590. return filp->f_op->setlease(filp, arg, lease, priv);
  1591. else
  1592. return generic_setlease(filp, arg, lease, priv);
  1593. }
  1594. EXPORT_SYMBOL_GPL(vfs_setlease);
  1595. static int do_fcntl_add_lease(unsigned int fd, struct file *filp, long arg)
  1596. {
  1597. struct file_lock *fl;
  1598. struct fasync_struct *new;
  1599. int error;
  1600. fl = lease_alloc(filp, arg);
  1601. if (IS_ERR(fl))
  1602. return PTR_ERR(fl);
  1603. new = fasync_alloc();
  1604. if (!new) {
  1605. locks_free_lock(fl);
  1606. return -ENOMEM;
  1607. }
  1608. new->fa_fd = fd;
  1609. error = vfs_setlease(filp, arg, &fl, (void **)&new);
  1610. if (fl)
  1611. locks_free_lock(fl);
  1612. if (new)
  1613. fasync_free(new);
  1614. return error;
  1615. }
  1616. /**
  1617. * fcntl_setlease - sets a lease on an open file
  1618. * @fd: open file descriptor
  1619. * @filp: file pointer
  1620. * @arg: type of lease to obtain
  1621. *
  1622. * Call this fcntl to establish a lease on the file.
  1623. * Note that you also need to call %F_SETSIG to
  1624. * receive a signal when the lease is broken.
  1625. */
  1626. int fcntl_setlease(unsigned int fd, struct file *filp, long arg)
  1627. {
  1628. if (arg == F_UNLCK)
  1629. return vfs_setlease(filp, F_UNLCK, NULL, NULL);
  1630. return do_fcntl_add_lease(fd, filp, arg);
  1631. }
  1632. /**
  1633. * flock_lock_file_wait - Apply a FLOCK-style lock to a file
  1634. * @filp: The file to apply the lock to
  1635. * @fl: The lock to be applied
  1636. *
  1637. * Add a FLOCK style lock to a file.
  1638. */
  1639. int flock_lock_file_wait(struct file *filp, struct file_lock *fl)
  1640. {
  1641. int error;
  1642. might_sleep();
  1643. for (;;) {
  1644. error = flock_lock_file(filp, fl);
  1645. if (error != FILE_LOCK_DEFERRED)
  1646. break;
  1647. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  1648. if (!error)
  1649. continue;
  1650. locks_delete_block(fl);
  1651. break;
  1652. }
  1653. return error;
  1654. }
  1655. EXPORT_SYMBOL(flock_lock_file_wait);
  1656. /**
  1657. * sys_flock: - flock() system call.
  1658. * @fd: the file descriptor to lock.
  1659. * @cmd: the type of lock to apply.
  1660. *
  1661. * Apply a %FL_FLOCK style lock to an open file descriptor.
  1662. * The @cmd can be one of
  1663. *
  1664. * %LOCK_SH -- a shared lock.
  1665. *
  1666. * %LOCK_EX -- an exclusive lock.
  1667. *
  1668. * %LOCK_UN -- remove an existing lock.
  1669. *
  1670. * %LOCK_MAND -- a `mandatory' flock. This exists to emulate Windows Share Modes.
  1671. *
  1672. * %LOCK_MAND can be combined with %LOCK_READ or %LOCK_WRITE to allow other
  1673. * processes read and write access respectively.
  1674. */
  1675. SYSCALL_DEFINE2(flock, unsigned int, fd, unsigned int, cmd)
  1676. {
  1677. struct fd f = fdget(fd);
  1678. struct file_lock *lock;
  1679. int can_sleep, unlock;
  1680. int error;
  1681. error = -EBADF;
  1682. if (!f.file)
  1683. goto out;
  1684. can_sleep = !(cmd & LOCK_NB);
  1685. cmd &= ~LOCK_NB;
  1686. unlock = (cmd == LOCK_UN);
  1687. if (!unlock && !(cmd & LOCK_MAND) &&
  1688. !(f.file->f_mode & (FMODE_READ|FMODE_WRITE)))
  1689. goto out_putf;
  1690. error = flock_make_lock(f.file, &lock, cmd);
  1691. if (error)
  1692. goto out_putf;
  1693. if (can_sleep)
  1694. lock->fl_flags |= FL_SLEEP;
  1695. error = security_file_lock(f.file, lock->fl_type);
  1696. if (error)
  1697. goto out_free;
  1698. if (f.file->f_op->flock)
  1699. error = f.file->f_op->flock(f.file,
  1700. (can_sleep) ? F_SETLKW : F_SETLK,
  1701. lock);
  1702. else
  1703. error = flock_lock_file_wait(f.file, lock);
  1704. out_free:
  1705. locks_free_lock(lock);
  1706. out_putf:
  1707. fdput(f);
  1708. out:
  1709. return error;
  1710. }
  1711. /**
  1712. * vfs_test_lock - test file byte range lock
  1713. * @filp: The file to test lock for
  1714. * @fl: The lock to test; also used to hold result
  1715. *
  1716. * Returns -ERRNO on failure. Indicates presence of conflicting lock by
  1717. * setting conf->fl_type to something other than F_UNLCK.
  1718. */
  1719. int vfs_test_lock(struct file *filp, struct file_lock *fl)
  1720. {
  1721. if (filp->f_op->lock)
  1722. return filp->f_op->lock(filp, F_GETLK, fl);
  1723. posix_test_lock(filp, fl);
  1724. return 0;
  1725. }
  1726. EXPORT_SYMBOL_GPL(vfs_test_lock);
  1727. static int posix_lock_to_flock(struct flock *flock, struct file_lock *fl)
  1728. {
  1729. flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
  1730. #if BITS_PER_LONG == 32
  1731. /*
  1732. * Make sure we can represent the posix lock via
  1733. * legacy 32bit flock.
  1734. */
  1735. if (fl->fl_start > OFFT_OFFSET_MAX)
  1736. return -EOVERFLOW;
  1737. if (fl->fl_end != OFFSET_MAX && fl->fl_end > OFFT_OFFSET_MAX)
  1738. return -EOVERFLOW;
  1739. #endif
  1740. flock->l_start = fl->fl_start;
  1741. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1742. fl->fl_end - fl->fl_start + 1;
  1743. flock->l_whence = 0;
  1744. flock->l_type = fl->fl_type;
  1745. return 0;
  1746. }
  1747. #if BITS_PER_LONG == 32
  1748. static void posix_lock_to_flock64(struct flock64 *flock, struct file_lock *fl)
  1749. {
  1750. flock->l_pid = IS_OFDLCK(fl) ? -1 : fl->fl_pid;
  1751. flock->l_start = fl->fl_start;
  1752. flock->l_len = fl->fl_end == OFFSET_MAX ? 0 :
  1753. fl->fl_end - fl->fl_start + 1;
  1754. flock->l_whence = 0;
  1755. flock->l_type = fl->fl_type;
  1756. }
  1757. #endif
  1758. /* Report the first existing lock that would conflict with l.
  1759. * This implements the F_GETLK command of fcntl().
  1760. */
  1761. int fcntl_getlk(struct file *filp, unsigned int cmd, struct flock __user *l)
  1762. {
  1763. struct file_lock file_lock;
  1764. struct flock flock;
  1765. int error;
  1766. error = -EFAULT;
  1767. if (copy_from_user(&flock, l, sizeof(flock)))
  1768. goto out;
  1769. error = -EINVAL;
  1770. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1771. goto out;
  1772. error = flock_to_posix_lock(filp, &file_lock, &flock);
  1773. if (error)
  1774. goto out;
  1775. if (cmd == F_OFD_GETLK) {
  1776. error = -EINVAL;
  1777. if (flock.l_pid != 0)
  1778. goto out;
  1779. cmd = F_GETLK;
  1780. file_lock.fl_flags |= FL_OFDLCK;
  1781. file_lock.fl_owner = filp;
  1782. }
  1783. error = vfs_test_lock(filp, &file_lock);
  1784. if (error)
  1785. goto out;
  1786. flock.l_type = file_lock.fl_type;
  1787. if (file_lock.fl_type != F_UNLCK) {
  1788. error = posix_lock_to_flock(&flock, &file_lock);
  1789. if (error)
  1790. goto rel_priv;
  1791. }
  1792. error = -EFAULT;
  1793. if (!copy_to_user(l, &flock, sizeof(flock)))
  1794. error = 0;
  1795. rel_priv:
  1796. locks_release_private(&file_lock);
  1797. out:
  1798. return error;
  1799. }
  1800. /**
  1801. * vfs_lock_file - file byte range lock
  1802. * @filp: The file to apply the lock to
  1803. * @cmd: type of locking operation (F_SETLK, F_GETLK, etc.)
  1804. * @fl: The lock to be applied
  1805. * @conf: Place to return a copy of the conflicting lock, if found.
  1806. *
  1807. * A caller that doesn't care about the conflicting lock may pass NULL
  1808. * as the final argument.
  1809. *
  1810. * If the filesystem defines a private ->lock() method, then @conf will
  1811. * be left unchanged; so a caller that cares should initialize it to
  1812. * some acceptable default.
  1813. *
  1814. * To avoid blocking kernel daemons, such as lockd, that need to acquire POSIX
  1815. * locks, the ->lock() interface may return asynchronously, before the lock has
  1816. * been granted or denied by the underlying filesystem, if (and only if)
  1817. * lm_grant is set. Callers expecting ->lock() to return asynchronously
  1818. * will only use F_SETLK, not F_SETLKW; they will set FL_SLEEP if (and only if)
  1819. * the request is for a blocking lock. When ->lock() does return asynchronously,
  1820. * it must return FILE_LOCK_DEFERRED, and call ->lm_grant() when the lock
  1821. * request completes.
  1822. * If the request is for non-blocking lock the file system should return
  1823. * FILE_LOCK_DEFERRED then try to get the lock and call the callback routine
  1824. * with the result. If the request timed out the callback routine will return a
  1825. * nonzero return code and the file system should release the lock. The file
  1826. * system is also responsible to keep a corresponding posix lock when it
  1827. * grants a lock so the VFS can find out which locks are locally held and do
  1828. * the correct lock cleanup when required.
  1829. * The underlying filesystem must not drop the kernel lock or call
  1830. * ->lm_grant() before returning to the caller with a FILE_LOCK_DEFERRED
  1831. * return code.
  1832. */
  1833. int vfs_lock_file(struct file *filp, unsigned int cmd, struct file_lock *fl, struct file_lock *conf)
  1834. {
  1835. if (filp->f_op->lock)
  1836. return filp->f_op->lock(filp, cmd, fl);
  1837. else
  1838. return posix_lock_file(filp, fl, conf);
  1839. }
  1840. EXPORT_SYMBOL_GPL(vfs_lock_file);
  1841. static int do_lock_file_wait(struct file *filp, unsigned int cmd,
  1842. struct file_lock *fl)
  1843. {
  1844. int error;
  1845. error = security_file_lock(filp, fl->fl_type);
  1846. if (error)
  1847. return error;
  1848. for (;;) {
  1849. error = vfs_lock_file(filp, cmd, fl, NULL);
  1850. if (error != FILE_LOCK_DEFERRED)
  1851. break;
  1852. error = wait_event_interruptible(fl->fl_wait, !fl->fl_next);
  1853. if (!error)
  1854. continue;
  1855. locks_delete_block(fl);
  1856. break;
  1857. }
  1858. return error;
  1859. }
  1860. /* Ensure that fl->fl_filp has compatible f_mode for F_SETLK calls */
  1861. static int
  1862. check_fmode_for_setlk(struct file_lock *fl)
  1863. {
  1864. switch (fl->fl_type) {
  1865. case F_RDLCK:
  1866. if (!(fl->fl_file->f_mode & FMODE_READ))
  1867. return -EBADF;
  1868. break;
  1869. case F_WRLCK:
  1870. if (!(fl->fl_file->f_mode & FMODE_WRITE))
  1871. return -EBADF;
  1872. }
  1873. return 0;
  1874. }
  1875. /* Apply the lock described by l to an open file descriptor.
  1876. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1877. */
  1878. int fcntl_setlk(unsigned int fd, struct file *filp, unsigned int cmd,
  1879. struct flock __user *l)
  1880. {
  1881. struct file_lock *file_lock = locks_alloc_lock();
  1882. struct flock flock;
  1883. struct inode *inode;
  1884. struct file *f;
  1885. int error;
  1886. if (file_lock == NULL)
  1887. return -ENOLCK;
  1888. /*
  1889. * This might block, so we do it before checking the inode.
  1890. */
  1891. error = -EFAULT;
  1892. if (copy_from_user(&flock, l, sizeof(flock)))
  1893. goto out;
  1894. inode = file_inode(filp);
  1895. /* Don't allow mandatory locks on files that may be memory mapped
  1896. * and shared.
  1897. */
  1898. if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
  1899. error = -EAGAIN;
  1900. goto out;
  1901. }
  1902. again:
  1903. error = flock_to_posix_lock(filp, file_lock, &flock);
  1904. if (error)
  1905. goto out;
  1906. error = check_fmode_for_setlk(file_lock);
  1907. if (error)
  1908. goto out;
  1909. /*
  1910. * If the cmd is requesting file-private locks, then set the
  1911. * FL_OFDLCK flag and override the owner.
  1912. */
  1913. switch (cmd) {
  1914. case F_OFD_SETLK:
  1915. error = -EINVAL;
  1916. if (flock.l_pid != 0)
  1917. goto out;
  1918. cmd = F_SETLK;
  1919. file_lock->fl_flags |= FL_OFDLCK;
  1920. file_lock->fl_owner = filp;
  1921. break;
  1922. case F_OFD_SETLKW:
  1923. error = -EINVAL;
  1924. if (flock.l_pid != 0)
  1925. goto out;
  1926. cmd = F_SETLKW;
  1927. file_lock->fl_flags |= FL_OFDLCK;
  1928. file_lock->fl_owner = filp;
  1929. /* Fallthrough */
  1930. case F_SETLKW:
  1931. file_lock->fl_flags |= FL_SLEEP;
  1932. }
  1933. error = do_lock_file_wait(filp, cmd, file_lock);
  1934. /*
  1935. * Attempt to detect a close/fcntl race and recover by
  1936. * releasing the lock that was just acquired.
  1937. */
  1938. /*
  1939. * we need that spin_lock here - it prevents reordering between
  1940. * update of inode->i_flock and check for it done in close().
  1941. * rcu_read_lock() wouldn't do.
  1942. */
  1943. spin_lock(&current->files->file_lock);
  1944. f = fcheck(fd);
  1945. spin_unlock(&current->files->file_lock);
  1946. if (!error && f != filp && flock.l_type != F_UNLCK) {
  1947. flock.l_type = F_UNLCK;
  1948. goto again;
  1949. }
  1950. out:
  1951. locks_free_lock(file_lock);
  1952. return error;
  1953. }
  1954. #if BITS_PER_LONG == 32
  1955. /* Report the first existing lock that would conflict with l.
  1956. * This implements the F_GETLK command of fcntl().
  1957. */
  1958. int fcntl_getlk64(struct file *filp, unsigned int cmd, struct flock64 __user *l)
  1959. {
  1960. struct file_lock file_lock;
  1961. struct flock64 flock;
  1962. int error;
  1963. error = -EFAULT;
  1964. if (copy_from_user(&flock, l, sizeof(flock)))
  1965. goto out;
  1966. error = -EINVAL;
  1967. if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  1968. goto out;
  1969. error = flock64_to_posix_lock(filp, &file_lock, &flock);
  1970. if (error)
  1971. goto out;
  1972. if (cmd == F_OFD_GETLK) {
  1973. error = -EINVAL;
  1974. if (flock.l_pid != 0)
  1975. goto out;
  1976. cmd = F_GETLK64;
  1977. file_lock.fl_flags |= FL_OFDLCK;
  1978. file_lock.fl_owner = filp;
  1979. }
  1980. error = vfs_test_lock(filp, &file_lock);
  1981. if (error)
  1982. goto out;
  1983. flock.l_type = file_lock.fl_type;
  1984. if (file_lock.fl_type != F_UNLCK)
  1985. posix_lock_to_flock64(&flock, &file_lock);
  1986. error = -EFAULT;
  1987. if (!copy_to_user(l, &flock, sizeof(flock)))
  1988. error = 0;
  1989. locks_release_private(&file_lock);
  1990. out:
  1991. return error;
  1992. }
  1993. /* Apply the lock described by l to an open file descriptor.
  1994. * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  1995. */
  1996. int fcntl_setlk64(unsigned int fd, struct file *filp, unsigned int cmd,
  1997. struct flock64 __user *l)
  1998. {
  1999. struct file_lock *file_lock = locks_alloc_lock();
  2000. struct flock64 flock;
  2001. struct inode *inode;
  2002. struct file *f;
  2003. int error;
  2004. if (file_lock == NULL)
  2005. return -ENOLCK;
  2006. /*
  2007. * This might block, so we do it before checking the inode.
  2008. */
  2009. error = -EFAULT;
  2010. if (copy_from_user(&flock, l, sizeof(flock)))
  2011. goto out;
  2012. inode = file_inode(filp);
  2013. /* Don't allow mandatory locks on files that may be memory mapped
  2014. * and shared.
  2015. */
  2016. if (mandatory_lock(inode) && mapping_writably_mapped(filp->f_mapping)) {
  2017. error = -EAGAIN;
  2018. goto out;
  2019. }
  2020. again:
  2021. error = flock64_to_posix_lock(filp, file_lock, &flock);
  2022. if (error)
  2023. goto out;
  2024. error = check_fmode_for_setlk(file_lock);
  2025. if (error)
  2026. goto out;
  2027. /*
  2028. * If the cmd is requesting file-private locks, then set the
  2029. * FL_OFDLCK flag and override the owner.
  2030. */
  2031. switch (cmd) {
  2032. case F_OFD_SETLK:
  2033. error = -EINVAL;
  2034. if (flock.l_pid != 0)
  2035. goto out;
  2036. cmd = F_SETLK64;
  2037. file_lock->fl_flags |= FL_OFDLCK;
  2038. file_lock->fl_owner = filp;
  2039. break;
  2040. case F_OFD_SETLKW:
  2041. error = -EINVAL;
  2042. if (flock.l_pid != 0)
  2043. goto out;
  2044. cmd = F_SETLKW64;
  2045. file_lock->fl_flags |= FL_OFDLCK;
  2046. file_lock->fl_owner = filp;
  2047. /* Fallthrough */
  2048. case F_SETLKW64:
  2049. file_lock->fl_flags |= FL_SLEEP;
  2050. }
  2051. error = do_lock_file_wait(filp, cmd, file_lock);
  2052. /*
  2053. * Attempt to detect a close/fcntl race and recover by
  2054. * releasing the lock that was just acquired.
  2055. */
  2056. spin_lock(&current->files->file_lock);
  2057. f = fcheck(fd);
  2058. spin_unlock(&current->files->file_lock);
  2059. if (!error && f != filp && flock.l_type != F_UNLCK) {
  2060. flock.l_type = F_UNLCK;
  2061. goto again;
  2062. }
  2063. out:
  2064. locks_free_lock(file_lock);
  2065. return error;
  2066. }
  2067. #endif /* BITS_PER_LONG == 32 */
  2068. /*
  2069. * This function is called when the file is being removed
  2070. * from the task's fd array. POSIX locks belonging to this task
  2071. * are deleted at this time.
  2072. */
  2073. void locks_remove_posix(struct file *filp, fl_owner_t owner)
  2074. {
  2075. struct file_lock lock;
  2076. /*
  2077. * If there are no locks held on this file, we don't need to call
  2078. * posix_lock_file(). Another process could be setting a lock on this
  2079. * file at the same time, but we wouldn't remove that lock anyway.
  2080. */
  2081. if (!file_inode(filp)->i_flock)
  2082. return;
  2083. lock.fl_type = F_UNLCK;
  2084. lock.fl_flags = FL_POSIX | FL_CLOSE;
  2085. lock.fl_start = 0;
  2086. lock.fl_end = OFFSET_MAX;
  2087. lock.fl_owner = owner;
  2088. lock.fl_pid = current->tgid;
  2089. lock.fl_file = filp;
  2090. lock.fl_ops = NULL;
  2091. lock.fl_lmops = NULL;
  2092. vfs_lock_file(filp, F_SETLK, &lock, NULL);
  2093. if (lock.fl_ops && lock.fl_ops->fl_release_private)
  2094. lock.fl_ops->fl_release_private(&lock);
  2095. }
  2096. EXPORT_SYMBOL(locks_remove_posix);
  2097. /*
  2098. * This function is called on the last close of an open file.
  2099. */
  2100. void locks_remove_file(struct file *filp)
  2101. {
  2102. struct inode * inode = file_inode(filp);
  2103. struct file_lock *fl;
  2104. struct file_lock **before;
  2105. LIST_HEAD(dispose);
  2106. if (!inode->i_flock)
  2107. return;
  2108. locks_remove_posix(filp, filp);
  2109. if (filp->f_op->flock) {
  2110. struct file_lock fl = {
  2111. .fl_owner = filp,
  2112. .fl_pid = current->tgid,
  2113. .fl_file = filp,
  2114. .fl_flags = FL_FLOCK,
  2115. .fl_type = F_UNLCK,
  2116. .fl_end = OFFSET_MAX,
  2117. };
  2118. filp->f_op->flock(filp, F_SETLKW, &fl);
  2119. if (fl.fl_ops && fl.fl_ops->fl_release_private)
  2120. fl.fl_ops->fl_release_private(&fl);
  2121. }
  2122. spin_lock(&inode->i_lock);
  2123. before = &inode->i_flock;
  2124. while ((fl = *before) != NULL) {
  2125. if (fl->fl_file == filp) {
  2126. if (IS_LEASE(fl)) {
  2127. lease_modify(before, F_UNLCK, &dispose);
  2128. continue;
  2129. }
  2130. /*
  2131. * There's a leftover lock on the list of a type that
  2132. * we didn't expect to see. Most likely a classic
  2133. * POSIX lock that ended up not getting released
  2134. * properly, or that raced onto the list somehow. Log
  2135. * some info about it and then just remove it from
  2136. * the list.
  2137. */
  2138. WARN(!IS_FLOCK(fl),
  2139. "leftover lock: dev=%u:%u ino=%lu type=%hhd flags=0x%x start=%lld end=%lld\n",
  2140. MAJOR(inode->i_sb->s_dev),
  2141. MINOR(inode->i_sb->s_dev), inode->i_ino,
  2142. fl->fl_type, fl->fl_flags,
  2143. fl->fl_start, fl->fl_end);
  2144. locks_delete_lock(before, &dispose);
  2145. continue;
  2146. }
  2147. before = &fl->fl_next;
  2148. }
  2149. spin_unlock(&inode->i_lock);
  2150. locks_dispose_list(&dispose);
  2151. }
  2152. /**
  2153. * posix_unblock_lock - stop waiting for a file lock
  2154. * @waiter: the lock which was waiting
  2155. *
  2156. * lockd needs to block waiting for locks.
  2157. */
  2158. int
  2159. posix_unblock_lock(struct file_lock *waiter)
  2160. {
  2161. int status = 0;
  2162. spin_lock(&blocked_lock_lock);
  2163. if (waiter->fl_next)
  2164. __locks_delete_block(waiter);
  2165. else
  2166. status = -ENOENT;
  2167. spin_unlock(&blocked_lock_lock);
  2168. return status;
  2169. }
  2170. EXPORT_SYMBOL(posix_unblock_lock);
  2171. /**
  2172. * vfs_cancel_lock - file byte range unblock lock
  2173. * @filp: The file to apply the unblock to
  2174. * @fl: The lock to be unblocked
  2175. *
  2176. * Used by lock managers to cancel blocked requests
  2177. */
  2178. int vfs_cancel_lock(struct file *filp, struct file_lock *fl)
  2179. {
  2180. if (filp->f_op->lock)
  2181. return filp->f_op->lock(filp, F_CANCELLK, fl);
  2182. return 0;
  2183. }
  2184. EXPORT_SYMBOL_GPL(vfs_cancel_lock);
  2185. #ifdef CONFIG_PROC_FS
  2186. #include <linux/proc_fs.h>
  2187. #include <linux/seq_file.h>
  2188. struct locks_iterator {
  2189. int li_cpu;
  2190. loff_t li_pos;
  2191. };
  2192. static void lock_get_status(struct seq_file *f, struct file_lock *fl,
  2193. loff_t id, char *pfx)
  2194. {
  2195. struct inode *inode = NULL;
  2196. unsigned int fl_pid;
  2197. if (fl->fl_nspid)
  2198. fl_pid = pid_vnr(fl->fl_nspid);
  2199. else
  2200. fl_pid = fl->fl_pid;
  2201. if (fl->fl_file != NULL)
  2202. inode = file_inode(fl->fl_file);
  2203. seq_printf(f, "%lld:%s ", id, pfx);
  2204. if (IS_POSIX(fl)) {
  2205. if (fl->fl_flags & FL_ACCESS)
  2206. seq_puts(f, "ACCESS");
  2207. else if (IS_OFDLCK(fl))
  2208. seq_puts(f, "OFDLCK");
  2209. else
  2210. seq_puts(f, "POSIX ");
  2211. seq_printf(f, " %s ",
  2212. (inode == NULL) ? "*NOINODE*" :
  2213. mandatory_lock(inode) ? "MANDATORY" : "ADVISORY ");
  2214. } else if (IS_FLOCK(fl)) {
  2215. if (fl->fl_type & LOCK_MAND) {
  2216. seq_puts(f, "FLOCK MSNFS ");
  2217. } else {
  2218. seq_puts(f, "FLOCK ADVISORY ");
  2219. }
  2220. } else if (IS_LEASE(fl)) {
  2221. if (fl->fl_flags & FL_DELEG)
  2222. seq_puts(f, "DELEG ");
  2223. else
  2224. seq_puts(f, "LEASE ");
  2225. if (lease_breaking(fl))
  2226. seq_puts(f, "BREAKING ");
  2227. else if (fl->fl_file)
  2228. seq_puts(f, "ACTIVE ");
  2229. else
  2230. seq_puts(f, "BREAKER ");
  2231. } else {
  2232. seq_puts(f, "UNKNOWN UNKNOWN ");
  2233. }
  2234. if (fl->fl_type & LOCK_MAND) {
  2235. seq_printf(f, "%s ",
  2236. (fl->fl_type & LOCK_READ)
  2237. ? (fl->fl_type & LOCK_WRITE) ? "RW " : "READ "
  2238. : (fl->fl_type & LOCK_WRITE) ? "WRITE" : "NONE ");
  2239. } else {
  2240. seq_printf(f, "%s ",
  2241. (lease_breaking(fl))
  2242. ? (fl->fl_type == F_UNLCK) ? "UNLCK" : "READ "
  2243. : (fl->fl_type == F_WRLCK) ? "WRITE" : "READ ");
  2244. }
  2245. if (inode) {
  2246. #ifdef WE_CAN_BREAK_LSLK_NOW
  2247. seq_printf(f, "%d %s:%ld ", fl_pid,
  2248. inode->i_sb->s_id, inode->i_ino);
  2249. #else
  2250. /* userspace relies on this representation of dev_t ;-( */
  2251. seq_printf(f, "%d %02x:%02x:%ld ", fl_pid,
  2252. MAJOR(inode->i_sb->s_dev),
  2253. MINOR(inode->i_sb->s_dev), inode->i_ino);
  2254. #endif
  2255. } else {
  2256. seq_printf(f, "%d <none>:0 ", fl_pid);
  2257. }
  2258. if (IS_POSIX(fl)) {
  2259. if (fl->fl_end == OFFSET_MAX)
  2260. seq_printf(f, "%Ld EOF\n", fl->fl_start);
  2261. else
  2262. seq_printf(f, "%Ld %Ld\n", fl->fl_start, fl->fl_end);
  2263. } else {
  2264. seq_puts(f, "0 EOF\n");
  2265. }
  2266. }
  2267. static int locks_show(struct seq_file *f, void *v)
  2268. {
  2269. struct locks_iterator *iter = f->private;
  2270. struct file_lock *fl, *bfl;
  2271. fl = hlist_entry(v, struct file_lock, fl_link);
  2272. lock_get_status(f, fl, iter->li_pos, "");
  2273. list_for_each_entry(bfl, &fl->fl_block, fl_block)
  2274. lock_get_status(f, bfl, iter->li_pos, " ->");
  2275. return 0;
  2276. }
  2277. static void *locks_start(struct seq_file *f, loff_t *pos)
  2278. __acquires(&blocked_lock_lock)
  2279. {
  2280. struct locks_iterator *iter = f->private;
  2281. iter->li_pos = *pos + 1;
  2282. lg_global_lock(&file_lock_lglock);
  2283. spin_lock(&blocked_lock_lock);
  2284. return seq_hlist_start_percpu(&file_lock_list, &iter->li_cpu, *pos);
  2285. }
  2286. static void *locks_next(struct seq_file *f, void *v, loff_t *pos)
  2287. {
  2288. struct locks_iterator *iter = f->private;
  2289. ++iter->li_pos;
  2290. return seq_hlist_next_percpu(v, &file_lock_list, &iter->li_cpu, pos);
  2291. }
  2292. static void locks_stop(struct seq_file *f, void *v)
  2293. __releases(&blocked_lock_lock)
  2294. {
  2295. spin_unlock(&blocked_lock_lock);
  2296. lg_global_unlock(&file_lock_lglock);
  2297. }
  2298. static const struct seq_operations locks_seq_operations = {
  2299. .start = locks_start,
  2300. .next = locks_next,
  2301. .stop = locks_stop,
  2302. .show = locks_show,
  2303. };
  2304. static int locks_open(struct inode *inode, struct file *filp)
  2305. {
  2306. return seq_open_private(filp, &locks_seq_operations,
  2307. sizeof(struct locks_iterator));
  2308. }
  2309. static const struct file_operations proc_locks_operations = {
  2310. .open = locks_open,
  2311. .read = seq_read,
  2312. .llseek = seq_lseek,
  2313. .release = seq_release_private,
  2314. };
  2315. static int __init proc_locks_init(void)
  2316. {
  2317. proc_create("locks", 0, NULL, &proc_locks_operations);
  2318. return 0;
  2319. }
  2320. module_init(proc_locks_init);
  2321. #endif
  2322. static int __init filelock_init(void)
  2323. {
  2324. int i;
  2325. filelock_cache = kmem_cache_create("file_lock_cache",
  2326. sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
  2327. lg_lock_init(&file_lock_lglock, "file_lock_lglock");
  2328. for_each_possible_cpu(i)
  2329. INIT_HLIST_HEAD(per_cpu_ptr(&file_lock_list, i));
  2330. return 0;
  2331. }
  2332. core_initcall(filelock_init);