locks.c 68 KB

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