locks.c 59 KB

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