locks.c 69 KB

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