locks.c 68 KB

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