locks.c 64 KB

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