dquot.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. /*
  2. * Implementation of the diskquota system for the LINUX operating system. QUOTA
  3. * is implemented using the BSD system call interface as the means of
  4. * communication with the user level. This file contains the generic routines
  5. * called by the different filesystems on allocation of an inode or block.
  6. * These routines take care of the administration needed to have a consistent
  7. * diskquota tracking system. The ideas of both user and group quotas are based
  8. * on the Melbourne quota system as used on BSD derived systems. The internal
  9. * implementation is based on one of the several variants of the LINUX
  10. * inode-subsystem with added complexity of the diskquota system.
  11. *
  12. * Version: $Id: dquot.c,v 6.3 1996/11/17 18:35:34 mvw Exp mvw $
  13. *
  14. * Author: Marco van Wieringen <mvw@planets.elm.net>
  15. *
  16. * Fixes: Dmitry Gorodchanin <pgmdsg@ibi.com>, 11 Feb 96
  17. *
  18. * Revised list management to avoid races
  19. * -- Bill Hawes, <whawes@star.net>, 9/98
  20. *
  21. * Fixed races in dquot_transfer(), dqget() and dquot_alloc_...().
  22. * As the consequence the locking was moved from dquot_decr_...(),
  23. * dquot_incr_...() to calling functions.
  24. * invalidate_dquots() now writes modified dquots.
  25. * Serialized quota_off() and quota_on() for mount point.
  26. * Fixed a few bugs in grow_dquots().
  27. * Fixed deadlock in write_dquot() - we no longer account quotas on
  28. * quota files
  29. * remove_dquot_ref() moved to inode.c - it now traverses through inodes
  30. * add_dquot_ref() restarts after blocking
  31. * Added check for bogus uid and fixed check for group in quotactl.
  32. * Jan Kara, <jack@suse.cz>, sponsored by SuSE CR, 10-11/99
  33. *
  34. * Used struct list_head instead of own list struct
  35. * Invalidation of referenced dquots is no longer possible
  36. * Improved free_dquots list management
  37. * Quota and i_blocks are now updated in one place to avoid races
  38. * Warnings are now delayed so we won't block in critical section
  39. * Write updated not to require dquot lock
  40. * Jan Kara, <jack@suse.cz>, 9/2000
  41. *
  42. * Added dynamic quota structure allocation
  43. * Jan Kara <jack@suse.cz> 12/2000
  44. *
  45. * Rewritten quota interface. Implemented new quota format and
  46. * formats registering.
  47. * Jan Kara, <jack@suse.cz>, 2001,2002
  48. *
  49. * New SMP locking.
  50. * Jan Kara, <jack@suse.cz>, 10/2002
  51. *
  52. * Added journalled quota support, fix lock inversion problems
  53. * Jan Kara, <jack@suse.cz>, 2003,2004
  54. *
  55. * (C) Copyright 1994 - 1997 Marco van Wieringen
  56. */
  57. #include <linux/errno.h>
  58. #include <linux/kernel.h>
  59. #include <linux/fs.h>
  60. #include <linux/mount.h>
  61. #include <linux/mm.h>
  62. #include <linux/time.h>
  63. #include <linux/types.h>
  64. #include <linux/string.h>
  65. #include <linux/fcntl.h>
  66. #include <linux/stat.h>
  67. #include <linux/tty.h>
  68. #include <linux/file.h>
  69. #include <linux/slab.h>
  70. #include <linux/sysctl.h>
  71. #include <linux/init.h>
  72. #include <linux/module.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/security.h>
  75. #include <linux/kmod.h>
  76. #include <linux/namei.h>
  77. #include <linux/buffer_head.h>
  78. #include <linux/capability.h>
  79. #include <linux/quotaops.h>
  80. #include <linux/writeback.h> /* for inode_lock, oddly enough.. */
  81. #include <asm/uaccess.h>
  82. #define __DQUOT_PARANOIA
  83. /*
  84. * There are two quota SMP locks. dq_list_lock protects all lists with quotas
  85. * and quota formats and also dqstats structure containing statistics about the
  86. * lists. dq_data_lock protects data from dq_dqb and also mem_dqinfo structures
  87. * and also guards consistency of dquot->dq_dqb with inode->i_blocks, i_bytes.
  88. * i_blocks and i_bytes updates itself are guarded by i_lock acquired directly
  89. * in inode_add_bytes() and inode_sub_bytes().
  90. *
  91. * The spinlock ordering is hence: dq_data_lock > dq_list_lock > i_lock
  92. *
  93. * Note that some things (eg. sb pointer, type, id) doesn't change during
  94. * the life of the dquot structure and so needn't to be protected by a lock
  95. *
  96. * Any operation working on dquots via inode pointers must hold dqptr_sem. If
  97. * operation is just reading pointers from inode (or not using them at all) the
  98. * read lock is enough. If pointers are altered function must hold write lock
  99. * (these locking rules also apply for S_NOQUOTA flag in the inode - note that
  100. * for altering the flag i_mutex is also needed). If operation is holding
  101. * reference to dquot in other way (e.g. quotactl ops) it must be guarded by
  102. * dqonoff_mutex.
  103. * This locking assures that:
  104. * a) update/access to dquot pointers in inode is serialized
  105. * b) everyone is guarded against invalidate_dquots()
  106. *
  107. * Each dquot has its dq_lock mutex. Locked dquots might not be referenced
  108. * from inodes (dquot_alloc_space() and such don't check the dq_lock).
  109. * Currently dquot is locked only when it is being read to memory (or space for
  110. * it is being allocated) on the first dqget() and when it is being released on
  111. * the last dqput(). The allocation and release oparations are serialized by
  112. * the dq_lock and by checking the use count in dquot_release(). Write
  113. * operations on dquots don't hold dq_lock as they copy data under dq_data_lock
  114. * spinlock to internal buffers before writing.
  115. *
  116. * Lock ordering (including related VFS locks) is the following:
  117. * i_mutex > dqonoff_sem > journal_lock > dqptr_sem > dquot->dq_lock >
  118. * dqio_mutex
  119. * i_mutex on quota files is special (it's below dqio_mutex)
  120. */
  121. static DEFINE_SPINLOCK(dq_list_lock);
  122. DEFINE_SPINLOCK(dq_data_lock);
  123. static char *quotatypes[] = INITQFNAMES;
  124. static struct quota_format_type *quota_formats; /* List of registered formats */
  125. static struct quota_module_name module_names[] = INIT_QUOTA_MODULE_NAMES;
  126. /* SLAB cache for dquot structures */
  127. static struct kmem_cache *dquot_cachep;
  128. int register_quota_format(struct quota_format_type *fmt)
  129. {
  130. spin_lock(&dq_list_lock);
  131. fmt->qf_next = quota_formats;
  132. quota_formats = fmt;
  133. spin_unlock(&dq_list_lock);
  134. return 0;
  135. }
  136. void unregister_quota_format(struct quota_format_type *fmt)
  137. {
  138. struct quota_format_type **actqf;
  139. spin_lock(&dq_list_lock);
  140. for (actqf = &quota_formats; *actqf && *actqf != fmt; actqf = &(*actqf)->qf_next);
  141. if (*actqf)
  142. *actqf = (*actqf)->qf_next;
  143. spin_unlock(&dq_list_lock);
  144. }
  145. static struct quota_format_type *find_quota_format(int id)
  146. {
  147. struct quota_format_type *actqf;
  148. spin_lock(&dq_list_lock);
  149. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  150. if (!actqf || !try_module_get(actqf->qf_owner)) {
  151. int qm;
  152. spin_unlock(&dq_list_lock);
  153. for (qm = 0; module_names[qm].qm_fmt_id && module_names[qm].qm_fmt_id != id; qm++);
  154. if (!module_names[qm].qm_fmt_id || request_module(module_names[qm].qm_mod_name))
  155. return NULL;
  156. spin_lock(&dq_list_lock);
  157. for (actqf = quota_formats; actqf && actqf->qf_fmt_id != id; actqf = actqf->qf_next);
  158. if (actqf && !try_module_get(actqf->qf_owner))
  159. actqf = NULL;
  160. }
  161. spin_unlock(&dq_list_lock);
  162. return actqf;
  163. }
  164. static void put_quota_format(struct quota_format_type *fmt)
  165. {
  166. module_put(fmt->qf_owner);
  167. }
  168. /*
  169. * Dquot List Management:
  170. * The quota code uses three lists for dquot management: the inuse_list,
  171. * free_dquots, and dquot_hash[] array. A single dquot structure may be
  172. * on all three lists, depending on its current state.
  173. *
  174. * All dquots are placed to the end of inuse_list when first created, and this
  175. * list is used for invalidate operation, which must look at every dquot.
  176. *
  177. * Unused dquots (dq_count == 0) are added to the free_dquots list when freed,
  178. * and this list is searched whenever we need an available dquot. Dquots are
  179. * removed from the list as soon as they are used again, and
  180. * dqstats.free_dquots gives the number of dquots on the list. When
  181. * dquot is invalidated it's completely released from memory.
  182. *
  183. * Dquots with a specific identity (device, type and id) are placed on
  184. * one of the dquot_hash[] hash chains. The provides an efficient search
  185. * mechanism to locate a specific dquot.
  186. */
  187. static LIST_HEAD(inuse_list);
  188. static LIST_HEAD(free_dquots);
  189. static unsigned int dq_hash_bits, dq_hash_mask;
  190. static struct hlist_head *dquot_hash;
  191. struct dqstats dqstats;
  192. static void dqput(struct dquot *dquot);
  193. static inline unsigned int
  194. hashfn(const struct super_block *sb, unsigned int id, int type)
  195. {
  196. unsigned long tmp;
  197. tmp = (((unsigned long)sb>>L1_CACHE_SHIFT) ^ id) * (MAXQUOTAS - type);
  198. return (tmp + (tmp >> dq_hash_bits)) & dq_hash_mask;
  199. }
  200. /*
  201. * Following list functions expect dq_list_lock to be held
  202. */
  203. static inline void insert_dquot_hash(struct dquot *dquot)
  204. {
  205. struct hlist_head *head = dquot_hash + hashfn(dquot->dq_sb, dquot->dq_id, dquot->dq_type);
  206. hlist_add_head(&dquot->dq_hash, head);
  207. }
  208. static inline void remove_dquot_hash(struct dquot *dquot)
  209. {
  210. hlist_del_init(&dquot->dq_hash);
  211. }
  212. static inline struct dquot *find_dquot(unsigned int hashent, struct super_block *sb, unsigned int id, int type)
  213. {
  214. struct hlist_node *node;
  215. struct dquot *dquot;
  216. hlist_for_each (node, dquot_hash+hashent) {
  217. dquot = hlist_entry(node, struct dquot, dq_hash);
  218. if (dquot->dq_sb == sb && dquot->dq_id == id && dquot->dq_type == type)
  219. return dquot;
  220. }
  221. return NODQUOT;
  222. }
  223. /* Add a dquot to the tail of the free list */
  224. static inline void put_dquot_last(struct dquot *dquot)
  225. {
  226. list_add_tail(&dquot->dq_free, &free_dquots);
  227. dqstats.free_dquots++;
  228. }
  229. static inline void remove_free_dquot(struct dquot *dquot)
  230. {
  231. if (list_empty(&dquot->dq_free))
  232. return;
  233. list_del_init(&dquot->dq_free);
  234. dqstats.free_dquots--;
  235. }
  236. static inline void put_inuse(struct dquot *dquot)
  237. {
  238. /* We add to the back of inuse list so we don't have to restart
  239. * when traversing this list and we block */
  240. list_add_tail(&dquot->dq_inuse, &inuse_list);
  241. dqstats.allocated_dquots++;
  242. }
  243. static inline void remove_inuse(struct dquot *dquot)
  244. {
  245. dqstats.allocated_dquots--;
  246. list_del(&dquot->dq_inuse);
  247. }
  248. /*
  249. * End of list functions needing dq_list_lock
  250. */
  251. static void wait_on_dquot(struct dquot *dquot)
  252. {
  253. mutex_lock(&dquot->dq_lock);
  254. mutex_unlock(&dquot->dq_lock);
  255. }
  256. #define mark_dquot_dirty(dquot) ((dquot)->dq_sb->dq_op->mark_dirty(dquot))
  257. int dquot_mark_dquot_dirty(struct dquot *dquot)
  258. {
  259. spin_lock(&dq_list_lock);
  260. if (!test_and_set_bit(DQ_MOD_B, &dquot->dq_flags))
  261. list_add(&dquot->dq_dirty, &sb_dqopt(dquot->dq_sb)->
  262. info[dquot->dq_type].dqi_dirty_list);
  263. spin_unlock(&dq_list_lock);
  264. return 0;
  265. }
  266. /* This function needs dq_list_lock */
  267. static inline int clear_dquot_dirty(struct dquot *dquot)
  268. {
  269. if (!test_and_clear_bit(DQ_MOD_B, &dquot->dq_flags))
  270. return 0;
  271. list_del_init(&dquot->dq_dirty);
  272. return 1;
  273. }
  274. void mark_info_dirty(struct super_block *sb, int type)
  275. {
  276. set_bit(DQF_INFO_DIRTY_B, &sb_dqopt(sb)->info[type].dqi_flags);
  277. }
  278. EXPORT_SYMBOL(mark_info_dirty);
  279. /*
  280. * Read dquot from disk and alloc space for it
  281. */
  282. int dquot_acquire(struct dquot *dquot)
  283. {
  284. int ret = 0, ret2 = 0;
  285. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  286. mutex_lock(&dquot->dq_lock);
  287. mutex_lock(&dqopt->dqio_mutex);
  288. if (!test_bit(DQ_READ_B, &dquot->dq_flags))
  289. ret = dqopt->ops[dquot->dq_type]->read_dqblk(dquot);
  290. if (ret < 0)
  291. goto out_iolock;
  292. set_bit(DQ_READ_B, &dquot->dq_flags);
  293. /* Instantiate dquot if needed */
  294. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && !dquot->dq_off) {
  295. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  296. /* Write the info if needed */
  297. if (info_dirty(&dqopt->info[dquot->dq_type]))
  298. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  299. if (ret < 0)
  300. goto out_iolock;
  301. if (ret2 < 0) {
  302. ret = ret2;
  303. goto out_iolock;
  304. }
  305. }
  306. set_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  307. out_iolock:
  308. mutex_unlock(&dqopt->dqio_mutex);
  309. mutex_unlock(&dquot->dq_lock);
  310. return ret;
  311. }
  312. /*
  313. * Write dquot to disk
  314. */
  315. int dquot_commit(struct dquot *dquot)
  316. {
  317. int ret = 0, ret2 = 0;
  318. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  319. mutex_lock(&dqopt->dqio_mutex);
  320. spin_lock(&dq_list_lock);
  321. if (!clear_dquot_dirty(dquot)) {
  322. spin_unlock(&dq_list_lock);
  323. goto out_sem;
  324. }
  325. spin_unlock(&dq_list_lock);
  326. /* Inactive dquot can be only if there was error during read/init
  327. * => we have better not writing it */
  328. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  329. ret = dqopt->ops[dquot->dq_type]->commit_dqblk(dquot);
  330. if (info_dirty(&dqopt->info[dquot->dq_type]))
  331. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  332. if (ret >= 0)
  333. ret = ret2;
  334. }
  335. out_sem:
  336. mutex_unlock(&dqopt->dqio_mutex);
  337. return ret;
  338. }
  339. /*
  340. * Release dquot
  341. */
  342. int dquot_release(struct dquot *dquot)
  343. {
  344. int ret = 0, ret2 = 0;
  345. struct quota_info *dqopt = sb_dqopt(dquot->dq_sb);
  346. mutex_lock(&dquot->dq_lock);
  347. /* Check whether we are not racing with some other dqget() */
  348. if (atomic_read(&dquot->dq_count) > 1)
  349. goto out_dqlock;
  350. mutex_lock(&dqopt->dqio_mutex);
  351. if (dqopt->ops[dquot->dq_type]->release_dqblk) {
  352. ret = dqopt->ops[dquot->dq_type]->release_dqblk(dquot);
  353. /* Write the info */
  354. if (info_dirty(&dqopt->info[dquot->dq_type]))
  355. ret2 = dqopt->ops[dquot->dq_type]->write_file_info(dquot->dq_sb, dquot->dq_type);
  356. if (ret >= 0)
  357. ret = ret2;
  358. }
  359. clear_bit(DQ_ACTIVE_B, &dquot->dq_flags);
  360. mutex_unlock(&dqopt->dqio_mutex);
  361. out_dqlock:
  362. mutex_unlock(&dquot->dq_lock);
  363. return ret;
  364. }
  365. /* Invalidate all dquots on the list. Note that this function is called after
  366. * quota is disabled and pointers from inodes removed so there cannot be new
  367. * quota users. There can still be some users of quotas due to inodes being
  368. * just deleted or pruned by prune_icache() (those are not attached to any
  369. * list). We have to wait for such users.
  370. */
  371. static void invalidate_dquots(struct super_block *sb, int type)
  372. {
  373. struct dquot *dquot, *tmp;
  374. restart:
  375. spin_lock(&dq_list_lock);
  376. list_for_each_entry_safe(dquot, tmp, &inuse_list, dq_inuse) {
  377. if (dquot->dq_sb != sb)
  378. continue;
  379. if (dquot->dq_type != type)
  380. continue;
  381. /* Wait for dquot users */
  382. if (atomic_read(&dquot->dq_count)) {
  383. DEFINE_WAIT(wait);
  384. atomic_inc(&dquot->dq_count);
  385. prepare_to_wait(&dquot->dq_wait_unused, &wait,
  386. TASK_UNINTERRUPTIBLE);
  387. spin_unlock(&dq_list_lock);
  388. /* Once dqput() wakes us up, we know it's time to free
  389. * the dquot.
  390. * IMPORTANT: we rely on the fact that there is always
  391. * at most one process waiting for dquot to free.
  392. * Otherwise dq_count would be > 1 and we would never
  393. * wake up.
  394. */
  395. if (atomic_read(&dquot->dq_count) > 1)
  396. schedule();
  397. finish_wait(&dquot->dq_wait_unused, &wait);
  398. dqput(dquot);
  399. /* At this moment dquot() need not exist (it could be
  400. * reclaimed by prune_dqcache(). Hence we must
  401. * restart. */
  402. goto restart;
  403. }
  404. /*
  405. * Quota now has no users and it has been written on last
  406. * dqput()
  407. */
  408. remove_dquot_hash(dquot);
  409. remove_free_dquot(dquot);
  410. remove_inuse(dquot);
  411. kmem_cache_free(dquot_cachep, dquot);
  412. }
  413. spin_unlock(&dq_list_lock);
  414. }
  415. int vfs_quota_sync(struct super_block *sb, int type)
  416. {
  417. struct list_head *dirty;
  418. struct dquot *dquot;
  419. struct quota_info *dqopt = sb_dqopt(sb);
  420. int cnt;
  421. mutex_lock(&dqopt->dqonoff_mutex);
  422. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  423. if (type != -1 && cnt != type)
  424. continue;
  425. if (!sb_has_quota_enabled(sb, cnt))
  426. continue;
  427. spin_lock(&dq_list_lock);
  428. dirty = &dqopt->info[cnt].dqi_dirty_list;
  429. while (!list_empty(dirty)) {
  430. dquot = list_first_entry(dirty, struct dquot, dq_dirty);
  431. /* Dirty and inactive can be only bad dquot... */
  432. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  433. clear_dquot_dirty(dquot);
  434. continue;
  435. }
  436. /* Now we have active dquot from which someone is
  437. * holding reference so we can safely just increase
  438. * use count */
  439. atomic_inc(&dquot->dq_count);
  440. dqstats.lookups++;
  441. spin_unlock(&dq_list_lock);
  442. sb->dq_op->write_dquot(dquot);
  443. dqput(dquot);
  444. spin_lock(&dq_list_lock);
  445. }
  446. spin_unlock(&dq_list_lock);
  447. }
  448. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  449. if ((cnt == type || type == -1) && sb_has_quota_enabled(sb, cnt)
  450. && info_dirty(&dqopt->info[cnt]))
  451. sb->dq_op->write_info(sb, cnt);
  452. spin_lock(&dq_list_lock);
  453. dqstats.syncs++;
  454. spin_unlock(&dq_list_lock);
  455. mutex_unlock(&dqopt->dqonoff_mutex);
  456. return 0;
  457. }
  458. /* Free unused dquots from cache */
  459. static void prune_dqcache(int count)
  460. {
  461. struct list_head *head;
  462. struct dquot *dquot;
  463. head = free_dquots.prev;
  464. while (head != &free_dquots && count) {
  465. dquot = list_entry(head, struct dquot, dq_free);
  466. remove_dquot_hash(dquot);
  467. remove_free_dquot(dquot);
  468. remove_inuse(dquot);
  469. kmem_cache_free(dquot_cachep, dquot);
  470. count--;
  471. head = free_dquots.prev;
  472. }
  473. }
  474. /*
  475. * This is called from kswapd when we think we need some
  476. * more memory
  477. */
  478. static int shrink_dqcache_memory(int nr, gfp_t gfp_mask)
  479. {
  480. if (nr) {
  481. spin_lock(&dq_list_lock);
  482. prune_dqcache(nr);
  483. spin_unlock(&dq_list_lock);
  484. }
  485. return (dqstats.free_dquots / 100) * sysctl_vfs_cache_pressure;
  486. }
  487. static struct shrinker dqcache_shrinker = {
  488. .shrink = shrink_dqcache_memory,
  489. .seeks = DEFAULT_SEEKS,
  490. };
  491. /*
  492. * Put reference to dquot
  493. * NOTE: If you change this function please check whether dqput_blocks() works right...
  494. * MUST be called with either dqptr_sem or dqonoff_mutex held
  495. */
  496. static void dqput(struct dquot *dquot)
  497. {
  498. if (!dquot)
  499. return;
  500. #ifdef __DQUOT_PARANOIA
  501. if (!atomic_read(&dquot->dq_count)) {
  502. printk("VFS: dqput: trying to free free dquot\n");
  503. printk("VFS: device %s, dquot of %s %d\n",
  504. dquot->dq_sb->s_id,
  505. quotatypes[dquot->dq_type],
  506. dquot->dq_id);
  507. BUG();
  508. }
  509. #endif
  510. spin_lock(&dq_list_lock);
  511. dqstats.drops++;
  512. spin_unlock(&dq_list_lock);
  513. we_slept:
  514. spin_lock(&dq_list_lock);
  515. if (atomic_read(&dquot->dq_count) > 1) {
  516. /* We have more than one user... nothing to do */
  517. atomic_dec(&dquot->dq_count);
  518. /* Releasing dquot during quotaoff phase? */
  519. if (!sb_has_quota_enabled(dquot->dq_sb, dquot->dq_type) &&
  520. atomic_read(&dquot->dq_count) == 1)
  521. wake_up(&dquot->dq_wait_unused);
  522. spin_unlock(&dq_list_lock);
  523. return;
  524. }
  525. /* Need to release dquot? */
  526. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && dquot_dirty(dquot)) {
  527. spin_unlock(&dq_list_lock);
  528. /* Commit dquot before releasing */
  529. dquot->dq_sb->dq_op->write_dquot(dquot);
  530. goto we_slept;
  531. }
  532. /* Clear flag in case dquot was inactive (something bad happened) */
  533. clear_dquot_dirty(dquot);
  534. if (test_bit(DQ_ACTIVE_B, &dquot->dq_flags)) {
  535. spin_unlock(&dq_list_lock);
  536. dquot->dq_sb->dq_op->release_dquot(dquot);
  537. goto we_slept;
  538. }
  539. atomic_dec(&dquot->dq_count);
  540. #ifdef __DQUOT_PARANOIA
  541. /* sanity check */
  542. BUG_ON(!list_empty(&dquot->dq_free));
  543. #endif
  544. put_dquot_last(dquot);
  545. spin_unlock(&dq_list_lock);
  546. }
  547. static struct dquot *get_empty_dquot(struct super_block *sb, int type)
  548. {
  549. struct dquot *dquot;
  550. dquot = kmem_cache_zalloc(dquot_cachep, GFP_NOFS);
  551. if(!dquot)
  552. return NODQUOT;
  553. mutex_init(&dquot->dq_lock);
  554. INIT_LIST_HEAD(&dquot->dq_free);
  555. INIT_LIST_HEAD(&dquot->dq_inuse);
  556. INIT_HLIST_NODE(&dquot->dq_hash);
  557. INIT_LIST_HEAD(&dquot->dq_dirty);
  558. init_waitqueue_head(&dquot->dq_wait_unused);
  559. dquot->dq_sb = sb;
  560. dquot->dq_type = type;
  561. atomic_set(&dquot->dq_count, 1);
  562. return dquot;
  563. }
  564. /*
  565. * Get reference to dquot
  566. * MUST be called with either dqptr_sem or dqonoff_mutex held
  567. */
  568. static struct dquot *dqget(struct super_block *sb, unsigned int id, int type)
  569. {
  570. unsigned int hashent = hashfn(sb, id, type);
  571. struct dquot *dquot, *empty = NODQUOT;
  572. if (!sb_has_quota_enabled(sb, type))
  573. return NODQUOT;
  574. we_slept:
  575. spin_lock(&dq_list_lock);
  576. if ((dquot = find_dquot(hashent, sb, id, type)) == NODQUOT) {
  577. if (empty == NODQUOT) {
  578. spin_unlock(&dq_list_lock);
  579. if ((empty = get_empty_dquot(sb, type)) == NODQUOT)
  580. schedule(); /* Try to wait for a moment... */
  581. goto we_slept;
  582. }
  583. dquot = empty;
  584. dquot->dq_id = id;
  585. /* all dquots go on the inuse_list */
  586. put_inuse(dquot);
  587. /* hash it first so it can be found */
  588. insert_dquot_hash(dquot);
  589. dqstats.lookups++;
  590. spin_unlock(&dq_list_lock);
  591. } else {
  592. if (!atomic_read(&dquot->dq_count))
  593. remove_free_dquot(dquot);
  594. atomic_inc(&dquot->dq_count);
  595. dqstats.cache_hits++;
  596. dqstats.lookups++;
  597. spin_unlock(&dq_list_lock);
  598. if (empty)
  599. kmem_cache_free(dquot_cachep, empty);
  600. }
  601. /* Wait for dq_lock - after this we know that either dquot_release() is already
  602. * finished or it will be canceled due to dq_count > 1 test */
  603. wait_on_dquot(dquot);
  604. /* Read the dquot and instantiate it (everything done only if needed) */
  605. if (!test_bit(DQ_ACTIVE_B, &dquot->dq_flags) && sb->dq_op->acquire_dquot(dquot) < 0) {
  606. dqput(dquot);
  607. return NODQUOT;
  608. }
  609. #ifdef __DQUOT_PARANOIA
  610. BUG_ON(!dquot->dq_sb); /* Has somebody invalidated entry under us? */
  611. #endif
  612. return dquot;
  613. }
  614. static int dqinit_needed(struct inode *inode, int type)
  615. {
  616. int cnt;
  617. if (IS_NOQUOTA(inode))
  618. return 0;
  619. if (type != -1)
  620. return inode->i_dquot[type] == NODQUOT;
  621. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  622. if (inode->i_dquot[cnt] == NODQUOT)
  623. return 1;
  624. return 0;
  625. }
  626. /* This routine is guarded by dqonoff_mutex mutex */
  627. static void add_dquot_ref(struct super_block *sb, int type)
  628. {
  629. struct inode *inode;
  630. restart:
  631. spin_lock(&inode_lock);
  632. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  633. if (!atomic_read(&inode->i_writecount))
  634. continue;
  635. if (!dqinit_needed(inode, type))
  636. continue;
  637. if (inode->i_state & (I_FREEING|I_WILL_FREE))
  638. continue;
  639. __iget(inode);
  640. spin_unlock(&inode_lock);
  641. sb->dq_op->initialize(inode, type);
  642. iput(inode);
  643. /* As we may have blocked we had better restart... */
  644. goto restart;
  645. }
  646. spin_unlock(&inode_lock);
  647. }
  648. /* Return 0 if dqput() won't block (note that 1 doesn't necessarily mean blocking) */
  649. static inline int dqput_blocks(struct dquot *dquot)
  650. {
  651. if (atomic_read(&dquot->dq_count) <= 1)
  652. return 1;
  653. return 0;
  654. }
  655. /* Remove references to dquots from inode - add dquot to list for freeing if needed */
  656. /* We can't race with anybody because we hold dqptr_sem for writing... */
  657. static int remove_inode_dquot_ref(struct inode *inode, int type,
  658. struct list_head *tofree_head)
  659. {
  660. struct dquot *dquot = inode->i_dquot[type];
  661. inode->i_dquot[type] = NODQUOT;
  662. if (dquot != NODQUOT) {
  663. if (dqput_blocks(dquot)) {
  664. #ifdef __DQUOT_PARANOIA
  665. if (atomic_read(&dquot->dq_count) != 1)
  666. printk(KERN_WARNING "VFS: Adding dquot with dq_count %d to dispose list.\n", atomic_read(&dquot->dq_count));
  667. #endif
  668. spin_lock(&dq_list_lock);
  669. list_add(&dquot->dq_free, tofree_head); /* As dquot must have currently users it can't be on the free list... */
  670. spin_unlock(&dq_list_lock);
  671. return 1;
  672. }
  673. else
  674. dqput(dquot); /* We have guaranteed we won't block */
  675. }
  676. return 0;
  677. }
  678. /* Free list of dquots - called from inode.c */
  679. /* dquots are removed from inodes, no new references can be got so we are the only ones holding reference */
  680. static void put_dquot_list(struct list_head *tofree_head)
  681. {
  682. struct list_head *act_head;
  683. struct dquot *dquot;
  684. act_head = tofree_head->next;
  685. /* So now we have dquots on the list... Just free them */
  686. while (act_head != tofree_head) {
  687. dquot = list_entry(act_head, struct dquot, dq_free);
  688. act_head = act_head->next;
  689. list_del_init(&dquot->dq_free); /* Remove dquot from the list so we won't have problems... */
  690. dqput(dquot);
  691. }
  692. }
  693. static void remove_dquot_ref(struct super_block *sb, int type,
  694. struct list_head *tofree_head)
  695. {
  696. struct inode *inode;
  697. spin_lock(&inode_lock);
  698. list_for_each_entry(inode, &sb->s_inodes, i_sb_list) {
  699. if (!IS_NOQUOTA(inode))
  700. remove_inode_dquot_ref(inode, type, tofree_head);
  701. }
  702. spin_unlock(&inode_lock);
  703. }
  704. /* Gather all references from inodes and drop them */
  705. static void drop_dquot_ref(struct super_block *sb, int type)
  706. {
  707. LIST_HEAD(tofree_head);
  708. if (sb->dq_op) {
  709. down_write(&sb_dqopt(sb)->dqptr_sem);
  710. remove_dquot_ref(sb, type, &tofree_head);
  711. up_write(&sb_dqopt(sb)->dqptr_sem);
  712. put_dquot_list(&tofree_head);
  713. }
  714. }
  715. static inline void dquot_incr_inodes(struct dquot *dquot, unsigned long number)
  716. {
  717. dquot->dq_dqb.dqb_curinodes += number;
  718. }
  719. static inline void dquot_incr_space(struct dquot *dquot, qsize_t number)
  720. {
  721. dquot->dq_dqb.dqb_curspace += number;
  722. }
  723. static inline void dquot_decr_inodes(struct dquot *dquot, unsigned long number)
  724. {
  725. if (dquot->dq_dqb.dqb_curinodes > number)
  726. dquot->dq_dqb.dqb_curinodes -= number;
  727. else
  728. dquot->dq_dqb.dqb_curinodes = 0;
  729. if (dquot->dq_dqb.dqb_curinodes <= dquot->dq_dqb.dqb_isoftlimit)
  730. dquot->dq_dqb.dqb_itime = (time_t) 0;
  731. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  732. }
  733. static inline void dquot_decr_space(struct dquot *dquot, qsize_t number)
  734. {
  735. if (dquot->dq_dqb.dqb_curspace > number)
  736. dquot->dq_dqb.dqb_curspace -= number;
  737. else
  738. dquot->dq_dqb.dqb_curspace = 0;
  739. if (toqb(dquot->dq_dqb.dqb_curspace) <= dquot->dq_dqb.dqb_bsoftlimit)
  740. dquot->dq_dqb.dqb_btime = (time_t) 0;
  741. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  742. }
  743. static int flag_print_warnings = 1;
  744. static inline int need_print_warning(struct dquot *dquot)
  745. {
  746. if (!flag_print_warnings)
  747. return 0;
  748. switch (dquot->dq_type) {
  749. case USRQUOTA:
  750. return current->fsuid == dquot->dq_id;
  751. case GRPQUOTA:
  752. return in_group_p(dquot->dq_id);
  753. }
  754. return 0;
  755. }
  756. /* Values of warnings */
  757. #define NOWARN 0
  758. #define IHARDWARN 1
  759. #define ISOFTLONGWARN 2
  760. #define ISOFTWARN 3
  761. #define BHARDWARN 4
  762. #define BSOFTLONGWARN 5
  763. #define BSOFTWARN 6
  764. /* Print warning to user which exceeded quota */
  765. static void print_warning(struct dquot *dquot, const char warntype)
  766. {
  767. char *msg = NULL;
  768. struct tty_struct *tty;
  769. int flag = (warntype == BHARDWARN || warntype == BSOFTLONGWARN) ? DQ_BLKS_B :
  770. ((warntype == IHARDWARN || warntype == ISOFTLONGWARN) ? DQ_INODES_B : 0);
  771. if (!need_print_warning(dquot) || (flag && test_and_set_bit(flag, &dquot->dq_flags)))
  772. return;
  773. mutex_lock(&tty_mutex);
  774. tty = get_current_tty();
  775. if (!tty)
  776. goto out_lock;
  777. tty_write_message(tty, dquot->dq_sb->s_id);
  778. if (warntype == ISOFTWARN || warntype == BSOFTWARN)
  779. tty_write_message(tty, ": warning, ");
  780. else
  781. tty_write_message(tty, ": write failed, ");
  782. tty_write_message(tty, quotatypes[dquot->dq_type]);
  783. switch (warntype) {
  784. case IHARDWARN:
  785. msg = " file limit reached.\r\n";
  786. break;
  787. case ISOFTLONGWARN:
  788. msg = " file quota exceeded too long.\r\n";
  789. break;
  790. case ISOFTWARN:
  791. msg = " file quota exceeded.\r\n";
  792. break;
  793. case BHARDWARN:
  794. msg = " block limit reached.\r\n";
  795. break;
  796. case BSOFTLONGWARN:
  797. msg = " block quota exceeded too long.\r\n";
  798. break;
  799. case BSOFTWARN:
  800. msg = " block quota exceeded.\r\n";
  801. break;
  802. }
  803. tty_write_message(tty, msg);
  804. out_lock:
  805. mutex_unlock(&tty_mutex);
  806. }
  807. static inline void flush_warnings(struct dquot **dquots, char *warntype)
  808. {
  809. int i;
  810. for (i = 0; i < MAXQUOTAS; i++)
  811. if (dquots[i] != NODQUOT && warntype[i] != NOWARN)
  812. print_warning(dquots[i], warntype[i]);
  813. }
  814. static inline char ignore_hardlimit(struct dquot *dquot)
  815. {
  816. struct mem_dqinfo *info = &sb_dqopt(dquot->dq_sb)->info[dquot->dq_type];
  817. return capable(CAP_SYS_RESOURCE) &&
  818. (info->dqi_format->qf_fmt_id != QFMT_VFS_OLD || !(info->dqi_flags & V1_DQF_RSQUASH));
  819. }
  820. /* needs dq_data_lock */
  821. static int check_idq(struct dquot *dquot, ulong inodes, char *warntype)
  822. {
  823. *warntype = NOWARN;
  824. if (inodes <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  825. return QUOTA_OK;
  826. if (dquot->dq_dqb.dqb_ihardlimit &&
  827. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_ihardlimit &&
  828. !ignore_hardlimit(dquot)) {
  829. *warntype = IHARDWARN;
  830. return NO_QUOTA;
  831. }
  832. if (dquot->dq_dqb.dqb_isoftlimit &&
  833. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  834. dquot->dq_dqb.dqb_itime && get_seconds() >= dquot->dq_dqb.dqb_itime &&
  835. !ignore_hardlimit(dquot)) {
  836. *warntype = ISOFTLONGWARN;
  837. return NO_QUOTA;
  838. }
  839. if (dquot->dq_dqb.dqb_isoftlimit &&
  840. (dquot->dq_dqb.dqb_curinodes + inodes) > dquot->dq_dqb.dqb_isoftlimit &&
  841. dquot->dq_dqb.dqb_itime == 0) {
  842. *warntype = ISOFTWARN;
  843. dquot->dq_dqb.dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  844. }
  845. return QUOTA_OK;
  846. }
  847. /* needs dq_data_lock */
  848. static int check_bdq(struct dquot *dquot, qsize_t space, int prealloc, char *warntype)
  849. {
  850. *warntype = 0;
  851. if (space <= 0 || test_bit(DQ_FAKE_B, &dquot->dq_flags))
  852. return QUOTA_OK;
  853. if (dquot->dq_dqb.dqb_bhardlimit &&
  854. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bhardlimit &&
  855. !ignore_hardlimit(dquot)) {
  856. if (!prealloc)
  857. *warntype = BHARDWARN;
  858. return NO_QUOTA;
  859. }
  860. if (dquot->dq_dqb.dqb_bsoftlimit &&
  861. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  862. dquot->dq_dqb.dqb_btime && get_seconds() >= dquot->dq_dqb.dqb_btime &&
  863. !ignore_hardlimit(dquot)) {
  864. if (!prealloc)
  865. *warntype = BSOFTLONGWARN;
  866. return NO_QUOTA;
  867. }
  868. if (dquot->dq_dqb.dqb_bsoftlimit &&
  869. toqb(dquot->dq_dqb.dqb_curspace + space) > dquot->dq_dqb.dqb_bsoftlimit &&
  870. dquot->dq_dqb.dqb_btime == 0) {
  871. if (!prealloc) {
  872. *warntype = BSOFTWARN;
  873. dquot->dq_dqb.dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  874. }
  875. else
  876. /*
  877. * We don't allow preallocation to exceed softlimit so exceeding will
  878. * be always printed
  879. */
  880. return NO_QUOTA;
  881. }
  882. return QUOTA_OK;
  883. }
  884. /*
  885. * Initialize quota pointers in inode
  886. * Transaction must be started at entry
  887. */
  888. int dquot_initialize(struct inode *inode, int type)
  889. {
  890. unsigned int id = 0;
  891. int cnt, ret = 0;
  892. /* First test before acquiring mutex - solves deadlocks when we
  893. * re-enter the quota code and are already holding the mutex */
  894. if (IS_NOQUOTA(inode))
  895. return 0;
  896. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  897. /* Having dqptr_sem we know NOQUOTA flags can't be altered... */
  898. if (IS_NOQUOTA(inode))
  899. goto out_err;
  900. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  901. if (type != -1 && cnt != type)
  902. continue;
  903. if (inode->i_dquot[cnt] == NODQUOT) {
  904. switch (cnt) {
  905. case USRQUOTA:
  906. id = inode->i_uid;
  907. break;
  908. case GRPQUOTA:
  909. id = inode->i_gid;
  910. break;
  911. }
  912. inode->i_dquot[cnt] = dqget(inode->i_sb, id, cnt);
  913. }
  914. }
  915. out_err:
  916. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  917. return ret;
  918. }
  919. /*
  920. * Release all quotas referenced by inode
  921. * Transaction must be started at an entry
  922. */
  923. int dquot_drop(struct inode *inode)
  924. {
  925. int cnt;
  926. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  927. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  928. if (inode->i_dquot[cnt] != NODQUOT) {
  929. dqput(inode->i_dquot[cnt]);
  930. inode->i_dquot[cnt] = NODQUOT;
  931. }
  932. }
  933. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  934. return 0;
  935. }
  936. /*
  937. * Following four functions update i_blocks+i_bytes fields and
  938. * quota information (together with appropriate checks)
  939. * NOTE: We absolutely rely on the fact that caller dirties
  940. * the inode (usually macros in quotaops.h care about this) and
  941. * holds a handle for the current transaction so that dquot write and
  942. * inode write go into the same transaction.
  943. */
  944. /*
  945. * This operation can block, but only after everything is updated
  946. */
  947. int dquot_alloc_space(struct inode *inode, qsize_t number, int warn)
  948. {
  949. int cnt, ret = NO_QUOTA;
  950. char warntype[MAXQUOTAS];
  951. /* First test before acquiring mutex - solves deadlocks when we
  952. * re-enter the quota code and are already holding the mutex */
  953. if (IS_NOQUOTA(inode)) {
  954. out_add:
  955. inode_add_bytes(inode, number);
  956. return QUOTA_OK;
  957. }
  958. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  959. warntype[cnt] = NOWARN;
  960. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  961. if (IS_NOQUOTA(inode)) { /* Now we can do reliable test... */
  962. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  963. goto out_add;
  964. }
  965. spin_lock(&dq_data_lock);
  966. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  967. if (inode->i_dquot[cnt] == NODQUOT)
  968. continue;
  969. if (check_bdq(inode->i_dquot[cnt], number, warn, warntype+cnt) == NO_QUOTA)
  970. goto warn_put_all;
  971. }
  972. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  973. if (inode->i_dquot[cnt] == NODQUOT)
  974. continue;
  975. dquot_incr_space(inode->i_dquot[cnt], number);
  976. }
  977. inode_add_bytes(inode, number);
  978. ret = QUOTA_OK;
  979. warn_put_all:
  980. spin_unlock(&dq_data_lock);
  981. if (ret == QUOTA_OK)
  982. /* Dirtify all the dquots - this can block when journalling */
  983. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  984. if (inode->i_dquot[cnt])
  985. mark_dquot_dirty(inode->i_dquot[cnt]);
  986. flush_warnings(inode->i_dquot, warntype);
  987. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  988. return ret;
  989. }
  990. /*
  991. * This operation can block, but only after everything is updated
  992. */
  993. int dquot_alloc_inode(const struct inode *inode, unsigned long number)
  994. {
  995. int cnt, ret = NO_QUOTA;
  996. char warntype[MAXQUOTAS];
  997. /* First test before acquiring mutex - solves deadlocks when we
  998. * re-enter the quota code and are already holding the mutex */
  999. if (IS_NOQUOTA(inode))
  1000. return QUOTA_OK;
  1001. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1002. warntype[cnt] = NOWARN;
  1003. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1004. if (IS_NOQUOTA(inode)) {
  1005. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1006. return QUOTA_OK;
  1007. }
  1008. spin_lock(&dq_data_lock);
  1009. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1010. if (inode->i_dquot[cnt] == NODQUOT)
  1011. continue;
  1012. if (check_idq(inode->i_dquot[cnt], number, warntype+cnt) == NO_QUOTA)
  1013. goto warn_put_all;
  1014. }
  1015. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1016. if (inode->i_dquot[cnt] == NODQUOT)
  1017. continue;
  1018. dquot_incr_inodes(inode->i_dquot[cnt], number);
  1019. }
  1020. ret = QUOTA_OK;
  1021. warn_put_all:
  1022. spin_unlock(&dq_data_lock);
  1023. if (ret == QUOTA_OK)
  1024. /* Dirtify all the dquots - this can block when journalling */
  1025. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1026. if (inode->i_dquot[cnt])
  1027. mark_dquot_dirty(inode->i_dquot[cnt]);
  1028. flush_warnings((struct dquot **)inode->i_dquot, warntype);
  1029. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1030. return ret;
  1031. }
  1032. /*
  1033. * This operation can block, but only after everything is updated
  1034. */
  1035. int dquot_free_space(struct inode *inode, qsize_t number)
  1036. {
  1037. unsigned int cnt;
  1038. /* First test before acquiring mutex - solves deadlocks when we
  1039. * re-enter the quota code and are already holding the mutex */
  1040. if (IS_NOQUOTA(inode)) {
  1041. out_sub:
  1042. inode_sub_bytes(inode, number);
  1043. return QUOTA_OK;
  1044. }
  1045. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1046. /* Now recheck reliably when holding dqptr_sem */
  1047. if (IS_NOQUOTA(inode)) {
  1048. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1049. goto out_sub;
  1050. }
  1051. spin_lock(&dq_data_lock);
  1052. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1053. if (inode->i_dquot[cnt] == NODQUOT)
  1054. continue;
  1055. dquot_decr_space(inode->i_dquot[cnt], number);
  1056. }
  1057. inode_sub_bytes(inode, number);
  1058. spin_unlock(&dq_data_lock);
  1059. /* Dirtify all the dquots - this can block when journalling */
  1060. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1061. if (inode->i_dquot[cnt])
  1062. mark_dquot_dirty(inode->i_dquot[cnt]);
  1063. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1064. return QUOTA_OK;
  1065. }
  1066. /*
  1067. * This operation can block, but only after everything is updated
  1068. */
  1069. int dquot_free_inode(const struct inode *inode, unsigned long number)
  1070. {
  1071. unsigned int cnt;
  1072. /* First test before acquiring mutex - solves deadlocks when we
  1073. * re-enter the quota code and are already holding the mutex */
  1074. if (IS_NOQUOTA(inode))
  1075. return QUOTA_OK;
  1076. down_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1077. /* Now recheck reliably when holding dqptr_sem */
  1078. if (IS_NOQUOTA(inode)) {
  1079. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1080. return QUOTA_OK;
  1081. }
  1082. spin_lock(&dq_data_lock);
  1083. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1084. if (inode->i_dquot[cnt] == NODQUOT)
  1085. continue;
  1086. dquot_decr_inodes(inode->i_dquot[cnt], number);
  1087. }
  1088. spin_unlock(&dq_data_lock);
  1089. /* Dirtify all the dquots - this can block when journalling */
  1090. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1091. if (inode->i_dquot[cnt])
  1092. mark_dquot_dirty(inode->i_dquot[cnt]);
  1093. up_read(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1094. return QUOTA_OK;
  1095. }
  1096. /*
  1097. * Transfer the number of inode and blocks from one diskquota to an other.
  1098. *
  1099. * This operation can block, but only after everything is updated
  1100. * A transaction must be started when entering this function.
  1101. */
  1102. int dquot_transfer(struct inode *inode, struct iattr *iattr)
  1103. {
  1104. qsize_t space;
  1105. struct dquot *transfer_from[MAXQUOTAS];
  1106. struct dquot *transfer_to[MAXQUOTAS];
  1107. int cnt, ret = NO_QUOTA, chuid = (iattr->ia_valid & ATTR_UID) && inode->i_uid != iattr->ia_uid,
  1108. chgid = (iattr->ia_valid & ATTR_GID) && inode->i_gid != iattr->ia_gid;
  1109. char warntype[MAXQUOTAS];
  1110. /* First test before acquiring mutex - solves deadlocks when we
  1111. * re-enter the quota code and are already holding the mutex */
  1112. if (IS_NOQUOTA(inode))
  1113. return QUOTA_OK;
  1114. /* Clear the arrays */
  1115. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1116. transfer_to[cnt] = transfer_from[cnt] = NODQUOT;
  1117. warntype[cnt] = NOWARN;
  1118. }
  1119. down_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1120. /* Now recheck reliably when holding dqptr_sem */
  1121. if (IS_NOQUOTA(inode)) { /* File without quota accounting? */
  1122. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1123. return QUOTA_OK;
  1124. }
  1125. /* First build the transfer_to list - here we can block on
  1126. * reading/instantiating of dquots. We know that the transaction for
  1127. * us was already started so we don't violate lock ranking here */
  1128. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1129. switch (cnt) {
  1130. case USRQUOTA:
  1131. if (!chuid)
  1132. continue;
  1133. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_uid, cnt);
  1134. break;
  1135. case GRPQUOTA:
  1136. if (!chgid)
  1137. continue;
  1138. transfer_to[cnt] = dqget(inode->i_sb, iattr->ia_gid, cnt);
  1139. break;
  1140. }
  1141. }
  1142. spin_lock(&dq_data_lock);
  1143. space = inode_get_bytes(inode);
  1144. /* Build the transfer_from list and check the limits */
  1145. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1146. if (transfer_to[cnt] == NODQUOT)
  1147. continue;
  1148. transfer_from[cnt] = inode->i_dquot[cnt];
  1149. if (check_idq(transfer_to[cnt], 1, warntype+cnt) == NO_QUOTA ||
  1150. check_bdq(transfer_to[cnt], space, 0, warntype+cnt) == NO_QUOTA)
  1151. goto warn_put_all;
  1152. }
  1153. /*
  1154. * Finally perform the needed transfer from transfer_from to transfer_to
  1155. */
  1156. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1157. /*
  1158. * Skip changes for same uid or gid or for turned off quota-type.
  1159. */
  1160. if (transfer_to[cnt] == NODQUOT)
  1161. continue;
  1162. /* Due to IO error we might not have transfer_from[] structure */
  1163. if (transfer_from[cnt]) {
  1164. dquot_decr_inodes(transfer_from[cnt], 1);
  1165. dquot_decr_space(transfer_from[cnt], space);
  1166. }
  1167. dquot_incr_inodes(transfer_to[cnt], 1);
  1168. dquot_incr_space(transfer_to[cnt], space);
  1169. inode->i_dquot[cnt] = transfer_to[cnt];
  1170. }
  1171. ret = QUOTA_OK;
  1172. warn_put_all:
  1173. spin_unlock(&dq_data_lock);
  1174. /* Dirtify all the dquots - this can block when journalling */
  1175. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1176. if (transfer_from[cnt])
  1177. mark_dquot_dirty(transfer_from[cnt]);
  1178. if (transfer_to[cnt])
  1179. mark_dquot_dirty(transfer_to[cnt]);
  1180. }
  1181. flush_warnings(transfer_to, warntype);
  1182. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1183. if (ret == QUOTA_OK && transfer_from[cnt] != NODQUOT)
  1184. dqput(transfer_from[cnt]);
  1185. if (ret == NO_QUOTA && transfer_to[cnt] != NODQUOT)
  1186. dqput(transfer_to[cnt]);
  1187. }
  1188. up_write(&sb_dqopt(inode->i_sb)->dqptr_sem);
  1189. return ret;
  1190. }
  1191. /*
  1192. * Write info of quota file to disk
  1193. */
  1194. int dquot_commit_info(struct super_block *sb, int type)
  1195. {
  1196. int ret;
  1197. struct quota_info *dqopt = sb_dqopt(sb);
  1198. mutex_lock(&dqopt->dqio_mutex);
  1199. ret = dqopt->ops[type]->write_file_info(sb, type);
  1200. mutex_unlock(&dqopt->dqio_mutex);
  1201. return ret;
  1202. }
  1203. /*
  1204. * Definitions of diskquota operations.
  1205. */
  1206. struct dquot_operations dquot_operations = {
  1207. .initialize = dquot_initialize,
  1208. .drop = dquot_drop,
  1209. .alloc_space = dquot_alloc_space,
  1210. .alloc_inode = dquot_alloc_inode,
  1211. .free_space = dquot_free_space,
  1212. .free_inode = dquot_free_inode,
  1213. .transfer = dquot_transfer,
  1214. .write_dquot = dquot_commit,
  1215. .acquire_dquot = dquot_acquire,
  1216. .release_dquot = dquot_release,
  1217. .mark_dirty = dquot_mark_dquot_dirty,
  1218. .write_info = dquot_commit_info
  1219. };
  1220. static inline void set_enable_flags(struct quota_info *dqopt, int type)
  1221. {
  1222. switch (type) {
  1223. case USRQUOTA:
  1224. dqopt->flags |= DQUOT_USR_ENABLED;
  1225. break;
  1226. case GRPQUOTA:
  1227. dqopt->flags |= DQUOT_GRP_ENABLED;
  1228. break;
  1229. }
  1230. }
  1231. static inline void reset_enable_flags(struct quota_info *dqopt, int type)
  1232. {
  1233. switch (type) {
  1234. case USRQUOTA:
  1235. dqopt->flags &= ~DQUOT_USR_ENABLED;
  1236. break;
  1237. case GRPQUOTA:
  1238. dqopt->flags &= ~DQUOT_GRP_ENABLED;
  1239. break;
  1240. }
  1241. }
  1242. /*
  1243. * Turn quota off on a device. type == -1 ==> quotaoff for all types (umount)
  1244. */
  1245. int vfs_quota_off(struct super_block *sb, int type)
  1246. {
  1247. int cnt;
  1248. struct quota_info *dqopt = sb_dqopt(sb);
  1249. struct inode *toputinode[MAXQUOTAS];
  1250. /* We need to serialize quota_off() for device */
  1251. mutex_lock(&dqopt->dqonoff_mutex);
  1252. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  1253. toputinode[cnt] = NULL;
  1254. if (type != -1 && cnt != type)
  1255. continue;
  1256. if (!sb_has_quota_enabled(sb, cnt))
  1257. continue;
  1258. reset_enable_flags(dqopt, cnt);
  1259. /* Note: these are blocking operations */
  1260. drop_dquot_ref(sb, cnt);
  1261. invalidate_dquots(sb, cnt);
  1262. /*
  1263. * Now all dquots should be invalidated, all writes done so we should be only
  1264. * users of the info. No locks needed.
  1265. */
  1266. if (info_dirty(&dqopt->info[cnt]))
  1267. sb->dq_op->write_info(sb, cnt);
  1268. if (dqopt->ops[cnt]->free_file_info)
  1269. dqopt->ops[cnt]->free_file_info(sb, cnt);
  1270. put_quota_format(dqopt->info[cnt].dqi_format);
  1271. toputinode[cnt] = dqopt->files[cnt];
  1272. dqopt->files[cnt] = NULL;
  1273. dqopt->info[cnt].dqi_flags = 0;
  1274. dqopt->info[cnt].dqi_igrace = 0;
  1275. dqopt->info[cnt].dqi_bgrace = 0;
  1276. dqopt->ops[cnt] = NULL;
  1277. }
  1278. mutex_unlock(&dqopt->dqonoff_mutex);
  1279. /* Sync the superblock so that buffers with quota data are written to
  1280. * disk (and so userspace sees correct data afterwards). */
  1281. if (sb->s_op->sync_fs)
  1282. sb->s_op->sync_fs(sb, 1);
  1283. sync_blockdev(sb->s_bdev);
  1284. /* Now the quota files are just ordinary files and we can set the
  1285. * inode flags back. Moreover we discard the pagecache so that
  1286. * userspace sees the writes we did bypassing the pagecache. We
  1287. * must also discard the blockdev buffers so that we see the
  1288. * changes done by userspace on the next quotaon() */
  1289. for (cnt = 0; cnt < MAXQUOTAS; cnt++)
  1290. if (toputinode[cnt]) {
  1291. mutex_lock(&dqopt->dqonoff_mutex);
  1292. /* If quota was reenabled in the meantime, we have
  1293. * nothing to do */
  1294. if (!sb_has_quota_enabled(sb, cnt)) {
  1295. mutex_lock_nested(&toputinode[cnt]->i_mutex, I_MUTEX_QUOTA);
  1296. toputinode[cnt]->i_flags &= ~(S_IMMUTABLE |
  1297. S_NOATIME | S_NOQUOTA);
  1298. truncate_inode_pages(&toputinode[cnt]->i_data, 0);
  1299. mutex_unlock(&toputinode[cnt]->i_mutex);
  1300. mark_inode_dirty(toputinode[cnt]);
  1301. iput(toputinode[cnt]);
  1302. }
  1303. mutex_unlock(&dqopt->dqonoff_mutex);
  1304. }
  1305. if (sb->s_bdev)
  1306. invalidate_bdev(sb->s_bdev);
  1307. return 0;
  1308. }
  1309. /*
  1310. * Turn quotas on on a device
  1311. */
  1312. /* Helper function when we already have the inode */
  1313. static int vfs_quota_on_inode(struct inode *inode, int type, int format_id)
  1314. {
  1315. struct quota_format_type *fmt = find_quota_format(format_id);
  1316. struct super_block *sb = inode->i_sb;
  1317. struct quota_info *dqopt = sb_dqopt(sb);
  1318. int error;
  1319. int oldflags = -1;
  1320. if (!fmt)
  1321. return -ESRCH;
  1322. if (!S_ISREG(inode->i_mode)) {
  1323. error = -EACCES;
  1324. goto out_fmt;
  1325. }
  1326. if (IS_RDONLY(inode)) {
  1327. error = -EROFS;
  1328. goto out_fmt;
  1329. }
  1330. if (!sb->s_op->quota_write || !sb->s_op->quota_read) {
  1331. error = -EINVAL;
  1332. goto out_fmt;
  1333. }
  1334. /* As we bypass the pagecache we must now flush the inode so that
  1335. * we see all the changes from userspace... */
  1336. write_inode_now(inode, 1);
  1337. /* And now flush the block cache so that kernel sees the changes */
  1338. invalidate_bdev(sb->s_bdev);
  1339. mutex_lock(&inode->i_mutex);
  1340. mutex_lock(&dqopt->dqonoff_mutex);
  1341. if (sb_has_quota_enabled(sb, type)) {
  1342. error = -EBUSY;
  1343. goto out_lock;
  1344. }
  1345. /* We don't want quota and atime on quota files (deadlocks possible)
  1346. * Also nobody should write to the file - we use special IO operations
  1347. * which ignore the immutable bit. */
  1348. down_write(&dqopt->dqptr_sem);
  1349. oldflags = inode->i_flags & (S_NOATIME | S_IMMUTABLE | S_NOQUOTA);
  1350. inode->i_flags |= S_NOQUOTA | S_NOATIME | S_IMMUTABLE;
  1351. up_write(&dqopt->dqptr_sem);
  1352. sb->dq_op->drop(inode);
  1353. error = -EIO;
  1354. dqopt->files[type] = igrab(inode);
  1355. if (!dqopt->files[type])
  1356. goto out_lock;
  1357. error = -EINVAL;
  1358. if (!fmt->qf_ops->check_quota_file(sb, type))
  1359. goto out_file_init;
  1360. dqopt->ops[type] = fmt->qf_ops;
  1361. dqopt->info[type].dqi_format = fmt;
  1362. INIT_LIST_HEAD(&dqopt->info[type].dqi_dirty_list);
  1363. mutex_lock(&dqopt->dqio_mutex);
  1364. if ((error = dqopt->ops[type]->read_file_info(sb, type)) < 0) {
  1365. mutex_unlock(&dqopt->dqio_mutex);
  1366. goto out_file_init;
  1367. }
  1368. mutex_unlock(&dqopt->dqio_mutex);
  1369. mutex_unlock(&inode->i_mutex);
  1370. set_enable_flags(dqopt, type);
  1371. add_dquot_ref(sb, type);
  1372. mutex_unlock(&dqopt->dqonoff_mutex);
  1373. return 0;
  1374. out_file_init:
  1375. dqopt->files[type] = NULL;
  1376. iput(inode);
  1377. out_lock:
  1378. mutex_unlock(&dqopt->dqonoff_mutex);
  1379. if (oldflags != -1) {
  1380. down_write(&dqopt->dqptr_sem);
  1381. /* Set the flags back (in the case of accidental quotaon()
  1382. * on a wrong file we don't want to mess up the flags) */
  1383. inode->i_flags &= ~(S_NOATIME | S_NOQUOTA | S_IMMUTABLE);
  1384. inode->i_flags |= oldflags;
  1385. up_write(&dqopt->dqptr_sem);
  1386. }
  1387. mutex_unlock(&inode->i_mutex);
  1388. out_fmt:
  1389. put_quota_format(fmt);
  1390. return error;
  1391. }
  1392. /* Actual function called from quotactl() */
  1393. int vfs_quota_on(struct super_block *sb, int type, int format_id, char *path)
  1394. {
  1395. struct nameidata nd;
  1396. int error;
  1397. error = path_lookup(path, LOOKUP_FOLLOW, &nd);
  1398. if (error < 0)
  1399. return error;
  1400. error = security_quota_on(nd.dentry);
  1401. if (error)
  1402. goto out_path;
  1403. /* Quota file not on the same filesystem? */
  1404. if (nd.mnt->mnt_sb != sb)
  1405. error = -EXDEV;
  1406. else
  1407. error = vfs_quota_on_inode(nd.dentry->d_inode, type, format_id);
  1408. out_path:
  1409. path_release(&nd);
  1410. return error;
  1411. }
  1412. /*
  1413. * This function is used when filesystem needs to initialize quotas
  1414. * during mount time.
  1415. */
  1416. int vfs_quota_on_mount(struct super_block *sb, char *qf_name,
  1417. int format_id, int type)
  1418. {
  1419. struct dentry *dentry;
  1420. int error;
  1421. dentry = lookup_one_len(qf_name, sb->s_root, strlen(qf_name));
  1422. if (IS_ERR(dentry))
  1423. return PTR_ERR(dentry);
  1424. if (!dentry->d_inode) {
  1425. error = -ENOENT;
  1426. goto out;
  1427. }
  1428. error = security_quota_on(dentry);
  1429. if (!error)
  1430. error = vfs_quota_on_inode(dentry->d_inode, type, format_id);
  1431. out:
  1432. dput(dentry);
  1433. return error;
  1434. }
  1435. /* Generic routine for getting common part of quota structure */
  1436. static void do_get_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1437. {
  1438. struct mem_dqblk *dm = &dquot->dq_dqb;
  1439. spin_lock(&dq_data_lock);
  1440. di->dqb_bhardlimit = dm->dqb_bhardlimit;
  1441. di->dqb_bsoftlimit = dm->dqb_bsoftlimit;
  1442. di->dqb_curspace = dm->dqb_curspace;
  1443. di->dqb_ihardlimit = dm->dqb_ihardlimit;
  1444. di->dqb_isoftlimit = dm->dqb_isoftlimit;
  1445. di->dqb_curinodes = dm->dqb_curinodes;
  1446. di->dqb_btime = dm->dqb_btime;
  1447. di->dqb_itime = dm->dqb_itime;
  1448. di->dqb_valid = QIF_ALL;
  1449. spin_unlock(&dq_data_lock);
  1450. }
  1451. int vfs_get_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1452. {
  1453. struct dquot *dquot;
  1454. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1455. if (!(dquot = dqget(sb, id, type))) {
  1456. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1457. return -ESRCH;
  1458. }
  1459. do_get_dqblk(dquot, di);
  1460. dqput(dquot);
  1461. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1462. return 0;
  1463. }
  1464. /* Generic routine for setting common part of quota structure */
  1465. static void do_set_dqblk(struct dquot *dquot, struct if_dqblk *di)
  1466. {
  1467. struct mem_dqblk *dm = &dquot->dq_dqb;
  1468. int check_blim = 0, check_ilim = 0;
  1469. spin_lock(&dq_data_lock);
  1470. if (di->dqb_valid & QIF_SPACE) {
  1471. dm->dqb_curspace = di->dqb_curspace;
  1472. check_blim = 1;
  1473. }
  1474. if (di->dqb_valid & QIF_BLIMITS) {
  1475. dm->dqb_bsoftlimit = di->dqb_bsoftlimit;
  1476. dm->dqb_bhardlimit = di->dqb_bhardlimit;
  1477. check_blim = 1;
  1478. }
  1479. if (di->dqb_valid & QIF_INODES) {
  1480. dm->dqb_curinodes = di->dqb_curinodes;
  1481. check_ilim = 1;
  1482. }
  1483. if (di->dqb_valid & QIF_ILIMITS) {
  1484. dm->dqb_isoftlimit = di->dqb_isoftlimit;
  1485. dm->dqb_ihardlimit = di->dqb_ihardlimit;
  1486. check_ilim = 1;
  1487. }
  1488. if (di->dqb_valid & QIF_BTIME)
  1489. dm->dqb_btime = di->dqb_btime;
  1490. if (di->dqb_valid & QIF_ITIME)
  1491. dm->dqb_itime = di->dqb_itime;
  1492. if (check_blim) {
  1493. if (!dm->dqb_bsoftlimit || toqb(dm->dqb_curspace) < dm->dqb_bsoftlimit) {
  1494. dm->dqb_btime = 0;
  1495. clear_bit(DQ_BLKS_B, &dquot->dq_flags);
  1496. }
  1497. else if (!(di->dqb_valid & QIF_BTIME)) /* Set grace only if user hasn't provided his own... */
  1498. dm->dqb_btime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_bgrace;
  1499. }
  1500. if (check_ilim) {
  1501. if (!dm->dqb_isoftlimit || dm->dqb_curinodes < dm->dqb_isoftlimit) {
  1502. dm->dqb_itime = 0;
  1503. clear_bit(DQ_INODES_B, &dquot->dq_flags);
  1504. }
  1505. else if (!(di->dqb_valid & QIF_ITIME)) /* Set grace only if user hasn't provided his own... */
  1506. dm->dqb_itime = get_seconds() + sb_dqopt(dquot->dq_sb)->info[dquot->dq_type].dqi_igrace;
  1507. }
  1508. if (dm->dqb_bhardlimit || dm->dqb_bsoftlimit || dm->dqb_ihardlimit || dm->dqb_isoftlimit)
  1509. clear_bit(DQ_FAKE_B, &dquot->dq_flags);
  1510. else
  1511. set_bit(DQ_FAKE_B, &dquot->dq_flags);
  1512. spin_unlock(&dq_data_lock);
  1513. mark_dquot_dirty(dquot);
  1514. }
  1515. int vfs_set_dqblk(struct super_block *sb, int type, qid_t id, struct if_dqblk *di)
  1516. {
  1517. struct dquot *dquot;
  1518. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1519. if (!(dquot = dqget(sb, id, type))) {
  1520. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1521. return -ESRCH;
  1522. }
  1523. do_set_dqblk(dquot, di);
  1524. dqput(dquot);
  1525. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1526. return 0;
  1527. }
  1528. /* Generic routine for getting common part of quota file information */
  1529. int vfs_get_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1530. {
  1531. struct mem_dqinfo *mi;
  1532. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1533. if (!sb_has_quota_enabled(sb, type)) {
  1534. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1535. return -ESRCH;
  1536. }
  1537. mi = sb_dqopt(sb)->info + type;
  1538. spin_lock(&dq_data_lock);
  1539. ii->dqi_bgrace = mi->dqi_bgrace;
  1540. ii->dqi_igrace = mi->dqi_igrace;
  1541. ii->dqi_flags = mi->dqi_flags & DQF_MASK;
  1542. ii->dqi_valid = IIF_ALL;
  1543. spin_unlock(&dq_data_lock);
  1544. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1545. return 0;
  1546. }
  1547. /* Generic routine for setting common part of quota file information */
  1548. int vfs_set_dqinfo(struct super_block *sb, int type, struct if_dqinfo *ii)
  1549. {
  1550. struct mem_dqinfo *mi;
  1551. mutex_lock(&sb_dqopt(sb)->dqonoff_mutex);
  1552. if (!sb_has_quota_enabled(sb, type)) {
  1553. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1554. return -ESRCH;
  1555. }
  1556. mi = sb_dqopt(sb)->info + type;
  1557. spin_lock(&dq_data_lock);
  1558. if (ii->dqi_valid & IIF_BGRACE)
  1559. mi->dqi_bgrace = ii->dqi_bgrace;
  1560. if (ii->dqi_valid & IIF_IGRACE)
  1561. mi->dqi_igrace = ii->dqi_igrace;
  1562. if (ii->dqi_valid & IIF_FLAGS)
  1563. mi->dqi_flags = (mi->dqi_flags & ~DQF_MASK) | (ii->dqi_flags & DQF_MASK);
  1564. spin_unlock(&dq_data_lock);
  1565. mark_info_dirty(sb, type);
  1566. /* Force write to disk */
  1567. sb->dq_op->write_info(sb, type);
  1568. mutex_unlock(&sb_dqopt(sb)->dqonoff_mutex);
  1569. return 0;
  1570. }
  1571. struct quotactl_ops vfs_quotactl_ops = {
  1572. .quota_on = vfs_quota_on,
  1573. .quota_off = vfs_quota_off,
  1574. .quota_sync = vfs_quota_sync,
  1575. .get_info = vfs_get_dqinfo,
  1576. .set_info = vfs_set_dqinfo,
  1577. .get_dqblk = vfs_get_dqblk,
  1578. .set_dqblk = vfs_set_dqblk
  1579. };
  1580. static ctl_table fs_dqstats_table[] = {
  1581. {
  1582. .ctl_name = FS_DQ_LOOKUPS,
  1583. .procname = "lookups",
  1584. .data = &dqstats.lookups,
  1585. .maxlen = sizeof(int),
  1586. .mode = 0444,
  1587. .proc_handler = &proc_dointvec,
  1588. },
  1589. {
  1590. .ctl_name = FS_DQ_DROPS,
  1591. .procname = "drops",
  1592. .data = &dqstats.drops,
  1593. .maxlen = sizeof(int),
  1594. .mode = 0444,
  1595. .proc_handler = &proc_dointvec,
  1596. },
  1597. {
  1598. .ctl_name = FS_DQ_READS,
  1599. .procname = "reads",
  1600. .data = &dqstats.reads,
  1601. .maxlen = sizeof(int),
  1602. .mode = 0444,
  1603. .proc_handler = &proc_dointvec,
  1604. },
  1605. {
  1606. .ctl_name = FS_DQ_WRITES,
  1607. .procname = "writes",
  1608. .data = &dqstats.writes,
  1609. .maxlen = sizeof(int),
  1610. .mode = 0444,
  1611. .proc_handler = &proc_dointvec,
  1612. },
  1613. {
  1614. .ctl_name = FS_DQ_CACHE_HITS,
  1615. .procname = "cache_hits",
  1616. .data = &dqstats.cache_hits,
  1617. .maxlen = sizeof(int),
  1618. .mode = 0444,
  1619. .proc_handler = &proc_dointvec,
  1620. },
  1621. {
  1622. .ctl_name = FS_DQ_ALLOCATED,
  1623. .procname = "allocated_dquots",
  1624. .data = &dqstats.allocated_dquots,
  1625. .maxlen = sizeof(int),
  1626. .mode = 0444,
  1627. .proc_handler = &proc_dointvec,
  1628. },
  1629. {
  1630. .ctl_name = FS_DQ_FREE,
  1631. .procname = "free_dquots",
  1632. .data = &dqstats.free_dquots,
  1633. .maxlen = sizeof(int),
  1634. .mode = 0444,
  1635. .proc_handler = &proc_dointvec,
  1636. },
  1637. {
  1638. .ctl_name = FS_DQ_SYNCS,
  1639. .procname = "syncs",
  1640. .data = &dqstats.syncs,
  1641. .maxlen = sizeof(int),
  1642. .mode = 0444,
  1643. .proc_handler = &proc_dointvec,
  1644. },
  1645. {
  1646. .ctl_name = FS_DQ_WARNINGS,
  1647. .procname = "warnings",
  1648. .data = &flag_print_warnings,
  1649. .maxlen = sizeof(int),
  1650. .mode = 0644,
  1651. .proc_handler = &proc_dointvec,
  1652. },
  1653. { .ctl_name = 0 },
  1654. };
  1655. static ctl_table fs_table[] = {
  1656. {
  1657. .ctl_name = FS_DQSTATS,
  1658. .procname = "quota",
  1659. .mode = 0555,
  1660. .child = fs_dqstats_table,
  1661. },
  1662. { .ctl_name = 0 },
  1663. };
  1664. static ctl_table sys_table[] = {
  1665. {
  1666. .ctl_name = CTL_FS,
  1667. .procname = "fs",
  1668. .mode = 0555,
  1669. .child = fs_table,
  1670. },
  1671. { .ctl_name = 0 },
  1672. };
  1673. static int __init dquot_init(void)
  1674. {
  1675. int i;
  1676. unsigned long nr_hash, order;
  1677. printk(KERN_NOTICE "VFS: Disk quotas %s\n", __DQUOT_VERSION__);
  1678. register_sysctl_table(sys_table);
  1679. dquot_cachep = kmem_cache_create("dquot",
  1680. sizeof(struct dquot), sizeof(unsigned long) * 4,
  1681. (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
  1682. SLAB_MEM_SPREAD|SLAB_PANIC),
  1683. NULL, NULL);
  1684. order = 0;
  1685. dquot_hash = (struct hlist_head *)__get_free_pages(GFP_ATOMIC, order);
  1686. if (!dquot_hash)
  1687. panic("Cannot create dquot hash table");
  1688. /* Find power-of-two hlist_heads which can fit into allocation */
  1689. nr_hash = (1UL << order) * PAGE_SIZE / sizeof(struct hlist_head);
  1690. dq_hash_bits = 0;
  1691. do {
  1692. dq_hash_bits++;
  1693. } while (nr_hash >> dq_hash_bits);
  1694. dq_hash_bits--;
  1695. nr_hash = 1UL << dq_hash_bits;
  1696. dq_hash_mask = nr_hash - 1;
  1697. for (i = 0; i < nr_hash; i++)
  1698. INIT_HLIST_HEAD(dquot_hash + i);
  1699. printk("Dquot-cache hash table entries: %ld (order %ld, %ld bytes)\n",
  1700. nr_hash, order, (PAGE_SIZE << order));
  1701. register_shrinker(&dqcache_shrinker);
  1702. return 0;
  1703. }
  1704. module_init(dquot_init);
  1705. EXPORT_SYMBOL(register_quota_format);
  1706. EXPORT_SYMBOL(unregister_quota_format);
  1707. EXPORT_SYMBOL(dqstats);
  1708. EXPORT_SYMBOL(dq_data_lock);
  1709. EXPORT_SYMBOL(vfs_quota_on);
  1710. EXPORT_SYMBOL(vfs_quota_on_mount);
  1711. EXPORT_SYMBOL(vfs_quota_off);
  1712. EXPORT_SYMBOL(vfs_quota_sync);
  1713. EXPORT_SYMBOL(vfs_get_dqinfo);
  1714. EXPORT_SYMBOL(vfs_set_dqinfo);
  1715. EXPORT_SYMBOL(vfs_get_dqblk);
  1716. EXPORT_SYMBOL(vfs_set_dqblk);
  1717. EXPORT_SYMBOL(dquot_commit);
  1718. EXPORT_SYMBOL(dquot_commit_info);
  1719. EXPORT_SYMBOL(dquot_acquire);
  1720. EXPORT_SYMBOL(dquot_release);
  1721. EXPORT_SYMBOL(dquot_mark_dquot_dirty);
  1722. EXPORT_SYMBOL(dquot_initialize);
  1723. EXPORT_SYMBOL(dquot_drop);
  1724. EXPORT_SYMBOL(dquot_alloc_space);
  1725. EXPORT_SYMBOL(dquot_alloc_inode);
  1726. EXPORT_SYMBOL(dquot_free_space);
  1727. EXPORT_SYMBOL(dquot_free_inode);
  1728. EXPORT_SYMBOL(dquot_transfer);