dquot.c 62 KB

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